SQL rounds test two things: whether you can write a correct query, and whether you can explain why it works. For each question below, practise saying the approach first, then writing the query. The examples use a simple employees table (id, name, dept_id, manager_id, salary) and a departments table (id, name).
1. Find the second-highest salary
Say how you handle ties and an empty result before you write anything.
-- Simple and works everywhere
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
-- With a window function (handles ties with DENSE_RANK)
SELECT DISTINCT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk = 2;2. Explain INNER, LEFT, RIGHT and FULL joins
- INNER JOIN: only rows that match in both tables.
- LEFT JOIN: every row from the left table; NULLs where the right table has no match.
- RIGHT JOIN: the opposite of LEFT. FULL OUTER JOIN: every row from both sides.
- Warn about duplicates: if the join key is not unique on one side, rows multiply.
-- Departments with their employee count, including empty departments
SELECT d.name, COUNT(e.id) AS employees
FROM departments d
LEFT JOIN employees e ON e.dept_id = d.id
GROUP BY d.name;3. WHERE vs HAVING
WHERE filters rows before grouping; HAVING filters groups after aggregation. You cannot use an aggregate such as COUNT() in WHERE.
SELECT dept_id, AVG(salary) AS avg_salary
FROM employees
WHERE salary > 0 -- row filter
GROUP BY dept_id
HAVING AVG(salary) > 50000; -- group filter4. Find duplicate records
Then say how you would remove them safely: keep the row with the smallest id and delete the rest, after taking a backup.
SELECT name, COUNT(*) AS copies
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;5. Top 3 salaries in each department
A favourite for checking window functions. ROW_NUMBER gives exactly three per group; DENSE_RANK keeps ties.
SELECT dept_id, name, salary
FROM (
SELECT dept_id, name, salary,
DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk <= 3;6. Employees who earn more than their manager
A self join: the same table used twice with different aliases.
SELECT e.name
FROM employees e
JOIN employees m ON e.manager_id = m.id
WHERE e.salary > m.salary;7. UNION vs UNION ALL
- UNION removes duplicate rows, so it does extra work.
- UNION ALL keeps every row and is faster.
- Use UNION ALL unless you really need duplicates removed.
8. How do NULLs behave?
- NULL means unknown: NULL = NULL is not true, so use IS NULL / IS NOT NULL.
- COUNT(column) skips NULLs; COUNT(*) counts every row.
- Use COALESCE(column, 0) to replace NULLs in results.
9. What is an index and when does it hurt?
An index (usually a B-tree) lets the database find rows without reading the whole table. It speeds up filters, joins and sorting, but slows down inserts and updates and uses storage. Show you would check with EXPLAIN before and after adding one.
10. A query is slow. What do you do?
- Run EXPLAIN to see whether it scans the whole table.
- Add or fix indexes on the columns used in WHERE and JOIN.
- Select only the columns you need instead of SELECT *.
- Filter early, and check for accidental row multiplication in joins.
How to practise
Write each query once, then close the article and explain it out loud in under a minute. In the interview, say your approach before typing; interviewers give credit for clear thinking even if a small detail is wrong.


