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.

61lessons and problems
159worked queries you can run
244exercises with answers
0topics you've finished

Phases

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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 →

departments employees customers orders products order_items

SQL cheat sheet: the shapes to know by heart

The order you write a query in

text
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

text
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         cut

Joins in one line each

text
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

text
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 EXISTS

Top N per group

text
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

text
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 average

The four checks

  1. 1Explain

    Say what it does and when to use it, in plain words, with no notes.

  2. 2Write

    Write the query from a blank page, then run it to check.

  3. 3Predict

    Read a query and say which rows it returns before you run it.

  4. 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.

Frequently asked questions

Is this SQL course free?
Yes: all 61 lessons and interview problems, every example and answer, and the query runner. There is no sign-up and nothing to install.
Do I need to install a database?
No. Every lesson uses one small practice database of six tables, and Try it runs your query on a real SQLite engine inside your browser. Each run starts from a fresh copy, so you cannot break anything.
Is this SQLite, MySQL or PostgreSQL?
The runner is SQLite, but almost everything here is standard SQL that works the same in MySQL, PostgreSQL and SQL Server. Where they differ, the lesson says so under In other databases.
How do I prepare for a SQL interview?
Learn every Must know topic (43 of them), then do the 14 interview problems twice: once with the hints, once from a blank page. The night before, read the revision notes.
Where are my queries saved?
Every query you change and run is saved in your browser, under the box you ran it in, and all of them are listed on the practice database page. Download my progress backs them up with your ticks.