Free course · updated 2026
SQL, from your first SELECT to interview problems
9 phases: 47 lessons and 14 classic interview problems, all on one small practice database. Every example shows the exact rows it returns, and Try it runs it in your browser, so you can change it and see what happens.
Phases
-
Phase 0
Start here: databases, tables and SQL
Know what a database is, what a table, a row and a key are, and the six practice tables you will use in every lesson.
4 lessons · 3 must know -
Phase 1
Reading data: SELECT, WHERE and sorting
Pick the columns you want, keep only the rows you want, sort them, and handle the missing values that trip everyone up.
8 lessons · 7 must know -
Phase 2
Summarising data: aggregates, GROUP BY and HAVING
Turn many rows into answers — totals, averages, counts per group — and know exactly which rows each number is built from.
5 lessons · 5 must know -
Phase 3
Joins: combining tables
Put rows from two or more tables side by side, keep or drop the rows that have no match on purpose, and spot the joins that quietly double your totals.
7 lessons · 6 must know -
Phase 4
Subqueries, CTEs and set operations
Use the result of one query inside another, break hard questions into named steps with WITH, walk trees with recursion, and stack results with UNION.
5 lessons · 3 must know -
Phase 5
Window functions: ranking, running totals and comparing rows
Calculate across related rows — rank them, compare each with the one before, keep running totals — without losing the individual rows the way GROUP BY does.
5 lessons · 4 must know -
Phase 6
Building tables and changing data
Create tables with the right rules, add, change and remove rows safely, group changes into transactions, and design tables that don't contradict themselves.
7 lessons · 5 must know -
Phase 7
Performance: indexes and query plans
Know why a query is slow, read the plan the database chose, and write queries and indexes that let it find rows without reading the whole table.
4 lessons · 1 must know -
Phase 8
SQL interview problems, from easy to hard
Solve the classic SQL interview questions from a blank page, explain your approach while you write, and name the edge cases before the interviewer does.
16 lessons · 9 must know
The practice database
Six small tables, with the awkward cases planted on purpose: NULLs, ties, duplicates and a customer with no orders. See every table and run any query →
SQL cheat sheet: the shapes to know by heart
The order you write a query in
SELECT columns, aggregates
FROM table
JOIN other_table ON key = key
WHERE row filter
GROUP BY columns
HAVING group filter
ORDER BY columns
LIMIT n;The order the database runs it in
1 FROM / JOIN build the rows
2 WHERE keep some rows
3 GROUP BY make groups
4 HAVING keep some groups
5 SELECT work out the columns (and window functions)
6 DISTINCT drop duplicate rows
7 ORDER BY sort
8 LIMIT cutJoins in one line each
INNER JOIN only rows that match on both sides
LEFT JOIN every left row; NULLs where the right has no match
FULL JOIN every row from both sides
CROSS JOIN every left row paired with every right row
anti-join LEFT JOIN … WHERE right.key IS NULL (or NOT EXISTS)NULL rules
x = NULL never true: use x IS NULL
NULL + 1 NULL
COUNT(*) counts rows; COUNT(col) skips NULLs
SUM / AVG / MIN / MAX skip NULLs
COALESCE(x, 0) first value that is not NULL
x NOT IN (…, NULL) returns no rows: use NOT EXISTSTop N per group
WITH ranked AS (
SELECT *, ROW_NUMBER() OVER (
PARTITION BY group_col ORDER BY value DESC) AS rn
FROM t
)
SELECT * FROM ranked WHERE rn <= 3;Window function shapes
ROW_NUMBER() OVER (PARTITION BY g ORDER BY x) 1,2,3,4
RANK() … 1,2,2,4
DENSE_RANK() … 1,2,2,3
LAG(x) OVER (ORDER BY d) previous row
SUM(x) OVER (ORDER BY d) running total
AVG(x) OVER (ORDER BY d ROWS 2 PRECEDING) 3-row moving averageThe four checks
- 1Explain
Say what it does and when to use it, in plain words, with no notes.
- 2Write
Write the query from a blank page, then run it to check.
- 3Predict
Read a query and say which rows it returns before you run it.
- 4Edge cases
Say what happens with NULLs, ties, duplicates or no rows at all, and fix it.
A topic is finished when you pass all four checks, not when you have read it. Be strict: if you had to look at the notes, it is not ticked yet.
Your progress
Your ticks are saved in this browser only, with no account. To keep them safe, or to carry on on another device, download a progress file and load it there. The file is made on your device and never sent to us.