SQL interview questions with answers
By Vikram · Updated 26 Oct 2026
Almost every tech interview, from software engineering to data analytics, includes SQL. The questions are predictable: joins, grouping, NULLs, a few classic queries and some database fundamentals. Here are the ones to know cold.
Joins
| Join | Returns |
|---|---|
| INNER JOIN | Only rows that match in both tables |
| LEFT JOIN | Every row from the left table, with NULLs where the right has no match |
| RIGHT JOIN | Every row from the right table, with NULLs where the left has no match |
| FULL OUTER JOIN | Every row from both (MySQL has no FULL JOIN: UNION a LEFT and a RIGHT join) |
| CROSS JOIN | Every combination: rows(A) × rows(B) |
Questions you will be asked
- WHERE vs HAVING: WHERE filters rows before grouping; HAVING filters groups after GROUP BY and can use aggregates.
- Counting rows vs values: COUNT(*) counts rows; COUNT(column) skips NULLs. AVG, SUM and the others ignore NULLs too.
- NULL comparisons: salary = NULL is never true. Use IS NULL. Beware NOT IN with a subquery that returns a NULL: it returns no rows.
- DELETE vs TRUNCATE vs DROP: DELETE removes chosen rows, TRUNCATE empties the table fast, DROP removes the table itself.
- UNION vs UNION ALL: UNION removes duplicates, UNION ALL keeps them and is faster.
- Primary key vs unique key: both are unique; a primary key also cannot be NULL and there is one per table.
Classic queries
- Second-highest salary: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees).
- Duplicates: SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1.
- Top earner per department: rank with RANK() OVER (PARTITION BY dept ORDER BY salary DESC) and keep rank 1.
- Running total: SUM(amount) OVER (ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW).
RANK vs DENSE_RANK vs ROW_NUMBER
For salaries 90, 70, 60, 60, 50: ROW_NUMBER gives 1, 2, 3, 4, 5; RANK gives 1, 2, 3, 3, 5 (it skips after a tie); DENSE_RANK gives 1, 2, 3, 3, 4 (no gaps).
Database fundamentals
- Indexes: B-tree indexes speed up lookups and sorting; a composite index (a, b) helps filters on a, or on a and b, but not on b alone.
- ACID: atomicity, consistency, isolation, durability.
- Isolation levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ (MySQL InnoDB's default) and SERIALIZABLE.
- Normalisation: 1NF (atomic values), 2NF (no partial dependency), 3NF (no transitive dependency).
Tip: Practise reading queries and predicting their results: the SQL questions with answers page has every SQL question from the Daily Quiz.
Frequently asked questions
What is the difference between WHERE and HAVING?
WHERE filters individual rows before they are grouped. HAVING filters groups after GROUP BY, so it can use aggregates such as COUNT(*) > 1.
How do you find the second-highest salary in SQL?
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees). Or use DENSE_RANK() and keep rank 2.
What is the difference between RANK and DENSE_RANK?
Both give tied rows the same rank. RANK then skips numbers (1, 2, 2, 4), DENSE_RANK does not (1, 2, 2, 3).
Does MySQL support FULL OUTER JOIN?
No. Combine a LEFT JOIN and a RIGHT JOIN with UNION to get the same result.