SQL interviews tend to test the same core ideas over and over: joins, aggregation, window functions, and how well you can turn an ambiguous business question into a working query. This guide groups the questions that come up most often by topic, with a clear answer and example for each, so you can practice reasoning through them rather than just memorizing syntax.
Quick Answer: What Do SQL Interviews Test?
Most SQL interviews focus on four core areas:
- Joins: INNER, LEFT, RIGHT, FULL, and self joins
- Aggregation: GROUP BY, HAVING, COUNT, SUM, AVG
- Window Functions: ROW_NUMBER, RANK, SUM OVER, running totals
- Subqueries & CTEs: WITH clauses, correlated vs non-correlated subqueries
Interviewers care more about your reasoning process than perfect syntax. Practice explaining your logic out loud.
On this page
Basic Query Questions
1. What is the difference between WHERE and HAVING?
WHERE filters individual rows before any grouping occurs. HAVING filters groups after GROUP BY has aggregated the data. A condition on an aggregate, such as a total greater than a value, must go in HAVING.
SELECT customer_id, SUM(order_total) AS total_spent FROM orders WHERE order_status = 'Completed' GROUP BY customer_id HAVING SUM(order_total) > 1000;
2. What is the difference between DELETE, TRUNCATE, and DROP?
DELETE removes specific rows based on a condition and can be rolled back within a transaction. TRUNCATE removes all rows from a table at once and is typically faster, but usually cannot be filtered with a condition. DROP removes the entire table structure along with its data.
3. What is a primary key, and how is it different from a unique key?
A primary key uniquely identifies each row in a table and cannot contain NULL values. A table can have only one primary key, but it can have multiple unique keys, which also enforce uniqueness but can allow a single NULL value depending on the database.
Join Questions
4. What is the difference between INNER JOIN and LEFT JOIN?
An INNER JOIN returns only rows with a match in both tables. A LEFT JOIN returns every row from the left table, with NULL filled in for any right-table columns where no match exists.
SELECT c.customer_name, o.order_id FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;
5. What is a self join, and when would you use one?
A self join joins a table to itself, typically to compare rows within the same table, such as finding employees who share the same manager.
SELECT e1.employee_name, e2.employee_name AS manager_name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.employee_id;
6. How do you find rows that exist in one table but not another?
Use a LEFT JOIN and filter for NULL on the right table's key, or use NOT EXISTS.
SELECT c.customer_id FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE o.customer_id IS NULL;
Aggregation and Grouping Questions
7. How do you find the second highest value in a column?
SELECT MAX(salary) AS second_highest FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
8. How do you count distinct values in a column?
SELECT COUNT(DISTINCT customer_id) AS unique_customers FROM orders;
9. How do you find duplicate rows in a table?
SELECT email, COUNT(*) AS occurrences FROM users GROUP BY email HAVING COUNT(*) > 1;
Window Function Questions
10. How do window functions differ from GROUP BY?
GROUP BY collapses multiple rows into one summarized row per group. A window function calculates an aggregate or ranking but keeps every original row, which lets you compare a row-level value against a group-level calculation in the same query.
SELECT employee_name, department, salary, AVG(salary) OVER (PARTITION BY department) AS dept_avg_salary FROM employees;
11. What is the difference between RANK, DENSE_RANK, and ROW_NUMBER?
ROW_NUMBER assigns a unique sequential number to every row, even if values tie. RANK gives tied rows the same rank but skips the next rank number. DENSE_RANK gives tied rows the same rank without skipping any rank numbers.
12. How do you calculate a running total in SQL?
SELECT order_date, order_total, SUM(order_total) OVER (ORDER BY order_date) AS running_total FROM orders;
Subquery and CTE Questions
13. What is a common table expression, and why use one instead of a subquery?
A common table expression, defined with a WITH clause, is a named temporary result set. It improves readability by breaking a complex query into sequential, named steps, and can be referenced multiple times within the same query, unlike a repeated subquery.
WITH monthly_totals AS ( SELECT customer_id, SUM(order_total) AS total_spent FROM orders GROUP BY customer_id ) SELECT * FROM monthly_totals WHERE total_spent > 5000;
14. What is the difference between a correlated and a non-correlated subquery?
A non-correlated subquery runs independently and only once. A correlated subquery references a column from the outer query and effectively runs once for every row of the outer query, which can be slower on large tables.
Scenario-Based Questions
15. Write a query to find customers who made a purchase in one month but not the next.
WITH month1 AS ( SELECT DISTINCT customer_id FROM orders WHERE order_date BETWEEN '2026-06-01' AND '2026-06-30' ), month2 AS ( SELECT DISTINCT customer_id FROM orders WHERE order_date BETWEEN '2026-07-01' AND '2026-07-31' ) SELECT m1.customer_id FROM month1 m1 LEFT JOIN month2 m2 ON m1.customer_id = m2.customer_id WHERE m2.customer_id IS NULL;
16. How would you approach a question you don't immediately know the answer to?
Say your thinking out loud. Interviewers generally care more about a structured approach, breaking the problem into smaller steps, checking assumptions, considering edge cases, than about arriving at a perfect answer instantly.
Practice against a real database with realistic messy data, not just isolated syntax drills. Query performance, handling NULLs, and explaining your logic out loud matter as much in most interviews as knowing the correct keyword.
How to Actually Prepare for SQL Interviews
- Explain your logic out loud as you write queries
- Handle NULLs explicitly in your conditions and joins
- Consider edge cases like empty tables, duplicate data, or missing values
- Think about performance when dealing with large datasets
- Ask clarifying questions about ambiguous requirements
Key Takeaways
- Most SQL interviews repeatedly test the same core areas: joins, aggregation, window functions, and subqueries or CTEs.
- Understanding what a JOIN actually does to your row count matters more than memorizing its syntax.
- Window functions preserve row-level detail while GROUP BY collapses it, which is the key distinction interviewers look for.
- Practicing how to explain your query logic out loud is as valuable as writing correct syntax.
- Handle NULLs and edge cases explicitly to show you think like a production-ready analyst.
Frequently Asked Questions
What is the difference between WHERE and HAVING in SQL?
WHERE filters individual rows before any grouping happens. HAVING filters groups after GROUP BY has already aggregated the data. A condition on an aggregate function, such as filtering for a SUM greater than a value, must go in HAVING, not WHERE.
What is the difference between INNER JOIN and LEFT JOIN?
An INNER JOIN returns only rows that have a match in both tables. A LEFT JOIN returns every row from the left table, along with matching rows from the right table, filling in NULL for any right-table columns where no match exists.
What is a self join used for?
A self join joins a table to itself, typically to compare rows within the same table, such as finding employees who share the same manager or comparing a value across two different time periods stored in the same table.
How do window functions differ from GROUP BY?
GROUP BY collapses multiple rows into a single summarized row per group. A window function calculates an aggregate or ranking value but keeps every original row intact, which allows a row-level value to be compared against a group-level calculation in the same query.
What is a common table expression, and why use one?
A common table expression, or CTE, is a named temporary result set defined with a WITH clause. It improves readability by breaking a complex query into named, sequential steps and can be referenced multiple times within the same query.
How should I prepare for a SQL interview beyond memorizing syntax?
Practice explaining your query logic out loud, not just writing correct syntax. Interviewers are often evaluating whether you can reason through an ambiguous business question and translate it into a query, not just recite a memorized answer.
Related Articles
External References
- PostgreSQL Documentation. "Window Functions." postgresql.org

0 Comments