← Back to blog

SQL interview questions and answers

SQL interview questions and answers — cover from Greenroom, the AI mock interviewer

SQL is the one technical skill that shows up in almost every data, backend, and analyst interview — and the one candidates most often wing. The questions are predictable, the concepts are finite, and a few hours of focused practice puts you ahead of most of the field. This guide covers the SQL interview questions that actually get asked, with answers and the concept each one tests.

The fundamentals interviewers always check

The classic query: second-highest salary

This is the single most-asked SQL interview question. One clean answer:

SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

Or with a window function:

SELECT DISTINCT salary FROM (
  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees
) t WHERE rnk = 2;

Knowing both approaches — and when DENSE_RANK beats RANK — signals real depth.

SQL interview topics — joins, group by, subqueries and window functions
SQL rounds reward clear querying logic explained out loud, not memorized syntax.

GROUP BY and aggregation questions

Window functions (the senior signal)

The core truth: SQL interviews reward people who can explain their query logic out loud, not just produce syntax. Talk through your joins and grouping as you write — interviewers care how you reason about the data as much as the final query.

How to practise SQL for interviews

Solve real query problems (start with the joins and the second-highest-salary pattern), then practise explaining your approach out loud — because many SQL rounds are live and verbal, and a correct query you can't explain still loses points. Greenroom runs spoken technical interviews where you talk through your reasoning and get feedback on clarity. Pair it with our OOPs interview questions and backend interview questions guides.

Frequently asked questions

What are the most common SQL interview questions?

The most common are: explain the types of JOINs and what rows each returns; the difference between WHERE and HAVING; primary key vs unique key; what normalization is; DELETE vs TRUNCATE vs DROP; and the classic 'find the second-highest salary' query. GROUP BY aggregation and window-function questions round out a typical round.

How do you find the second-highest salary in SQL?

Two clean approaches: use a subquery — SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); or use a window function — DENSE_RANK() OVER (ORDER BY salary DESC) and filter where the rank equals 2. Knowing both, and when DENSE_RANK beats RANK for ties, signals real depth.

What window functions should I know for SQL interviews?

Know ROW_NUMBER, RANK and DENSE_RANK and how each handles ties; PARTITION BY to compute per-group results like the top earner per department; SUM() OVER for running totals and moving averages; and LEAD/LAG to compare a row with the next or previous one. Window functions are a strong senior-level signal.

How should I practise SQL for an interview?

Solve real query problems starting with joins and the second-highest-salary pattern, then practise explaining your query logic out loud, since many SQL rounds are live and verbal. A correct query you can't explain still loses points, so rehearsing your reasoning with a voice-based mock interview helps you talk through joins and grouping clearly.

SQL rounds reward query logic you can explain out loud. Greenroom runs spoken technical interviews where you talk through your reasoning and get feedback. Free to start.