21 November 2026 · 2 min read
SQL interview questions that trip people up — and how to answer them
The SQL questions that separate confident candidates from the rest: joins, window functions, optimisation and design. With clear explanations you can apply in the room.
Almost every developer, analyst and data role tests SQL, and it is one of the most common places candidates quietly lose marks. The queries aren't always hard — but the follow-up questions about performance and edge cases catch people out. Here are the areas that matter and how to handle them.
Joins — know exactly what each one returns
Be able to explain, without hesitation, the difference between INNER, LEFT, RIGHT and FULL OUTER joins, and what happens to unmatched rows. A classic trap: "find customers who have never placed an order." The answer is a LEFT JOIN with a WHERE orders.id IS NULL, or a NOT EXISTS — and you should be able to explain why an INNER join cannot answer it.
Window functions — the senior differentiator
Window functions separate people who learned SQL properly from people who stopped at basic queries. Practise:
ROW_NUMBER(),RANK()andDENSE_RANK()— and the difference between them.- Finding the second highest salary per department, or the latest record per group.
- Running totals and moving averages with
SUM() OVER (...).
Aggregation gotchas
- The difference between WHERE and HAVING, and why you can't filter an aggregate in WHERE.
- How
COUNT(*),COUNT(column)andCOUNT(DISTINCT column)differ. - How
NULLbehaves in comparisons, aggregates and joins — a very common source of wrong answers.
Performance and optimisation
Once you write a correct query, expect "now make it faster." Be ready to talk about:
- What an index does, and why it speeds up reads but costs on writes.
- How to read an execution plan and spot a full table scan.
- Why
SELECT *, functions on indexed columns, and leading wildcards hurt performance.
Design questions
For many roles you'll also get a short schema design: "model a booking system." Interviewers want sensible normalisation, correct keys and relationships, and awareness of when you'd denormalise for read performance.
How to prepare
- Write, by hand, the "never ordered" and "second highest per group" queries until they're automatic.
- Learn one window-function problem cold — it comes up constantly.
- Be ready to explain indexing and execution plans in plain language.
- Do a mock interview where someone asks the follow-up "now optimise it" — that's the part people fluff.
SQL rewards precision. If you can write the query and then explain how the database runs it, you'll stand out from most candidates.