SQL Interview Questions: 10 Concepts You’ll Be Asked About
Ten common SQL interview questions with clear answers and runnable examples, from WHERE vs HAVING and JOIN types to window functions.
By Learn SQL Online ·
SQL interviews usually mix two things: concept questions (“what’s the difference between…?”) and live coding on a small dataset. This guide covers the ten concept questions that come up most, with short answers you can say out loud and examples you can check.
1. What is the difference between WHERE and HAVING?
WHERE filters rows before grouping. HAVING filters groups after aggregation, so it can use functions like COUNT and SUM.
SELECT category, COUNT(*) AS drinks
FROM drinks
WHERE price >= 4
GROUP BY category
HAVING COUNT(*) >= 2;
| category | drinks |
|---|---|
| coffee | 4 |
| tea | 2 |
Here WHERE drops cheap drinks first, then HAVING keeps categories with at least two drinks left. More in our HAVING tutorial.
2. Explain the different types of JOIN
- INNER JOIN returns rows that match in both tables.
- LEFT JOIN returns every row from the left table, with NULLs where the right table has no match.
- RIGHT JOIN is the mirror image of LEFT JOIN.
- FULL OUTER JOIN keeps unmatched rows from both sides.
- CROSS JOIN pairs every row with every row.
A strong answer adds when you’d use each: LEFT JOIN when you need to keep everything from one side, such as customers with zero orders. See JOIN and LEFT JOIN.
3. What is NULL, and why doesn’t = NULL work?
NULL means “unknown”. Comparing anything to unknown gives unknown, not true, so WHERE city = NULL matches nothing. Use IS NULL and IS NOT NULL. Interviewers often follow up with how NULLs affect COUNT:
SELECT COUNT(*) AS all_rows, COUNT(city) AS known_cities
FROM customers;
| all_rows | known_cities |
|---|---|
| 8 | 7 |
4. What is the difference between UNION and UNION ALL?
Both stack the results of two queries with the same columns. UNION removes duplicate rows, which costs an extra step; UNION ALL keeps every row and is faster. Use UNION ALL unless you specifically need duplicates removed.
SELECT city FROM customers WHERE city = 'Austin'
UNION ALL
SELECT city FROM customers WHERE city = 'Austin';
| city |
|---|
| Austin |
| Austin |
| Austin |
| Austin |
| Austin |
| Austin |
With UNION instead, the same query returns a single row.
5. What are primary keys and foreign keys?
A primary key uniquely identifies each row in a table, like customers.id. It can’t be NULL or repeated. A foreign key is a column that points at another table’s primary key, like orders.customer_id, and the database can enforce that it always points at a real row. See CREATE TABLE.
6. In what order does SQL run a query?
Not top to bottom. The logical order is:
FROMandJOINWHEREGROUP BYHAVINGSELECTDISTINCTORDER BYLIMIT
This explains common errors: you can’t use a SELECT alias in WHERE (in most databases), because WHERE runs first, but you can use it in ORDER BY.
7. What is the difference between GROUP BY and a window function?
GROUP BY collapses rows into one per group. A window function calculates across a group but keeps every row:
SELECT name, department_id, salary,
ROUND(AVG(salary) OVER (PARTITION BY department_id)) AS dept_avg
FROM employees
WHERE department_id = 3;
| name | department_id | salary | dept_avg |
|---|---|---|---|
| Diego Alvarez | 3 | 130000 | 100000 |
| Emma Wilson | 3 | 70000 | 100000 |
Window functions answer “compared to its group” questions without a self-join. See window functions and ROW_NUMBER vs RANK.
8. What is the difference between DELETE, TRUNCATE and DROP?
DELETE removes chosen rows (with WHERE) and can be rolled back inside a transaction. TRUNCATE quickly removes all rows but keeps the table. DROP TABLE removes the table itself. See DELETE.
9. What is an index, and when would you add one?
An index is a lookup structure, like a book’s index, that lets the database find rows without scanning the whole table. Add one to columns you often filter, join or sort on. The trade-off: indexes take space and slow down inserts and updates slightly, because they must be kept up to date.
10. When would you use a CTE instead of a subquery?
They usually perform the same. A CTE (WITH ...) is easier to read when a query has several steps, and lets you refer to the same intermediate result more than once. Interviewers like candidates who break a hard problem into named steps.
How to practise for SQL interviews
- Say your plan before you type. Interviewers care about how you break the problem down.
- Check edge cases out loud: ties, NULLs, empty groups and duplicates.
- Practise the classic patterns: top N per group, second-highest value, running totals, finding duplicates and gaps.
Our interview prep track has classic SQL interview problems on a realistic company dataset, each checked instantly, so you can practise the coding half too.