SQL Interview Cheat Sheet: The Ultimate Guide for Data Professionals

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_idnameagecitycountryhas_subscription
1Adam20New YorkUSATRUE
2Nelson40TorontoCanadaFALSE
3Sam35New DelhiIndiaTRUE

Orders Table

order_idcust_idorder_datecostdiscountstatus
10112021-01-01200.010.0Shipped
10222021-02-01150.00.0Shipped
10312021-03-01300.020.0Pending

📌 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):


✅ 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

Leave a Comment