SQL Interview Tutorial with Examples.
SQL interview tutorial: window functions, query optimisation, schema design questions and the common patterns interviewers ask about.
Beginner
Start here — no prior experience needed
What SQL Interviews Actually Test
The four things every SQL round scores, a runnable DuckDB dataset you can paste into a terminal, and why reading your result out loud beats memorising.
Joins and NULL Semantics
Inner, left, anti and self joins with real result sets, why NOT IN returns zero rows against a NULL, and where the status filter has to go in a LEFT JOIN.
GROUP BY and Aggregates
What you may select alongside a group, WHERE versus HAVING, conditional aggregation instead of three queries, and the join fan-out that doubles your SUM.
Intermediate
For developers with core concepts down
Window Functions
ROW_NUMBER, RANK and DENSE_RANK on the same ties, running totals, LAG and LEAD, and the frame clause that silently changes your answer.
Top N Per Group
The most-asked SQL interview pattern solved four ways — window function, correlated subquery, lateral join, and DISTINCT ON — with tie handling.
CTEs, Subqueries, and Recursion
Why a named CTE scores better than a nested subquery returning the same rows, when a correlated subquery runs once per row, and recursive CTEs.
Dates, Cohorts, and Retention
Date truncation and spines, month-over-month growth, the cohort retention grid, and why BETWEEN on a timestamp column loses the last day of the range.
Gaps and Islands
Consecutive-day streaks, session grouping, and merging overlapping intervals — the pattern that looks hardest and reduces to subtracting a row number.
Advanced
Production-grade patterns for experienced engineers
Query Plans and Performance
Reading EXPLAIN ANALYZE, the four reasons a query is slow, what an index can and cannot do, and the estimate-versus-actual gap that explains a bad plan.
A Full Mock SQL Round
Five questions of escalating difficulty with the wrong first attempt shown each time, the clarifying question that prevents it, and what's scored.