
SQL Interview Cheat Sheet: The Ultimate Guide for Data Professionals
- ๐ Sample Data
- ๐ Querying Tables with SELECT
- ๐ Filtering Output with WHERE
- ๐ Combining Multiple Tables with Joins
- ๐ Aggregation and Grouping
- ๐ Window Functions
- ๐ Subqueries and CTEs
- ๐ Set Operations
- ๐ CASE Statements
- ๐ Other Useful SQL Commands
- ๐ Recommended Free SQL & Data Science Courses
Structured Query Language (SQL) is one of the most essential skills for data analysts, data scientists, and backend developers. Whether you’re preparing for an interview or brushing up on your SQL fundamentals, this cheat sheet will give you a clear, practical reference to master key concepts.
📌 Sample Data
Before diving into SQL commands, let’s look at a simple dataset we’ll use throughout the examples.
Customers Table
cust_id | name | age | city | country | has_subscription |
---|---|---|---|---|---|
1 | Adam | 20 | New York | USA | TRUE |
2 | Nelson | 40 | Toronto | Canada | FALSE |
3 | Sam | 35 | New Delhi | India | TRUE |
Orders Table
order_id | cust_id | order_date | cost | discount | status |
---|---|---|---|---|---|
101 | 1 | 2021-01-01 | 200.0 | 10.0 | Shipped |
102 | 2 | 2021-02-01 | 150.0 | 0.0 | Shipped |
103 | 1 | 2021-03-01 | 300.0 | 20.0 | Pending |
📌 Querying Tables with SELECT
- Fetch all columns from a table:
SELECT * FROM customers;
- Fetch specific columns:
SELECT name, age FROM customers;
- Sort results using ORDER BY:
SELECT * FROM customers ORDER BY age DESC;
- Use Aliases:
SELECT name AS customer_name FROM customers;
📌 Filtering Output with WHERE
- Comparison Operators:
SELECT * FROM customers WHERE age > 30;
- BETWEEN and IN:
SELECT * FROM customers WHERE age BETWEEN 25 AND 40; SELECT * FROM customers WHERE country IN ('USA', 'Canada');
- LIKE and NULL checks:
SELECT * FROM customers WHERE city LIKE 'New%'; SELECT * FROM customers WHERE has_subscription IS NOT NULL;
📌 Combining Multiple Tables with Joins
- INNER JOIN:
SELECT c.name, o.order_date FROM customers c INNER JOIN orders o ON c.cust_id = o.cust_id;
- LEFT JOIN:
SELECT c.name, o.order_date FROM customers c LEFT JOIN orders o ON c.cust_id = o.cust_id;
- RIGHT JOIN and FULL JOIN:
SELECT * FROM customers c RIGHT JOIN orders o ON c.cust_id = o.cust_id; SELECT * FROM customers c FULL OUTER JOIN orders o ON c.cust_id = o.cust_id;
- CROSS JOIN:
SELECT * FROM customers CROSS JOIN orders;
📌 Aggregation and Grouping
- GROUP BY with Aggregations:
SELECT country, COUNT(*) FROM customers GROUP BY country;
- HAVING to filter aggregated results:
SELECT country, COUNT(*) FROM customers GROUP BY country HAVING COUNT(*) > 1;
📌 Window Functions
- Ranking and Aggregate Examples:
SELECT name, RANK() OVER (ORDER BY age DESC) AS rank FROM customers;
- LAG/LEAD:
SELECT name, LAG(age,1) OVER (ORDER BY age) AS prev_age, LEAD(age,1) OVER (ORDER BY age) AS next_age FROM customers;
📌 Subqueries and CTEs
- Subquery Example:
SELECT * FROM customers WHERE age > (SELECT AVG(age) FROM customers);
- CTE Example:
WITH customer_orders AS ( SELECT cust_id, COUNT(*) AS order_count FROM orders GROUP BY cust_id ) SELECT * FROM customer_orders WHERE order_count > 1;
📌 Set Operations
- UNION, INTERSECT, EXCEPT:
SELECT city FROM customers UNION SELECT city FROM orders;
📌 CASE Statements
- Conditional logic in SQL:
SELECT name, CASE WHEN age < 30 THEN 'Young' WHEN age BETWEEN 30 AND 50 THEN 'Mid-age' ELSE 'Senior' END AS age_group FROM customers;
📌 Other Useful SQL Commands
- ALTER TABLE → Modify a table
- DROP TABLE → Delete a table
- TRUNCATE → Remove all rows quickly
- CAST() → Convert data types
- DISTINCT() → Remove duplicates
🎓 Recommended Free SQL & Data Science Courses
Ready to strengthen your SQL skills? Start with these free courses (no certificate included):
- 🔗 IBM Data Science
- 🔗 SQL Basics for Data Science
- 🔗 Generative AI for Data Scientists
- 🔗 Google IT Automation with Python

✅ Save this cheat sheet for your SQL interview prep.
♻️ Share with your peers who are also preparing for data roles.
Amr Abdelkarem
Owner
No Comments