Every SQL idea, explained with a real question.
Each topic shows the idea in plain language, runs a worked example on sample company data, lists the mistakes people make, and links to exercises that practise it.
SELECT is how every question starts: name the columns you want and the table they live in.
1 exercise WHEREWHERE keeps only the rows that match your conditions, before anything is counted or summed.
3 exercises ORDER BYORDER BY sorts the result; LIMIT keeps only the first rows. Together they answer "top 5" questions.
1 exercise DISTINCTDISTINCT removes duplicate rows from the result, so each combination appears once.
4 exercises NULLNULL means "unknown", so it never equals anything, not even another NULL. Test it with IS NULL.
5 exercises LIKE, IN and BETWEENLIKE matches text patterns, IN checks a list of values, BETWEEN checks an inclusive range.
1 exercise COUNT, SUM and AVGAggregate functions turn many rows into one number: how many, how much, on average.
11 exercises GROUP BYGROUP BY splits rows into groups and runs the aggregate once per group: revenue per country, orders per month.
10 exercises HAVINGHAVING filters on aggregate results, such as "countries with more than 3 customers".
6 exercises DatesBusiness questions are usually about time: revenue per month, signups this quarter, days to resolve.
14 exercises INNER JOINJOIN connects tables through a shared key, so you can see customer names next to their orders.
10 exercises LEFT JOINLEFT JOIN keeps every row from the first table, even without a match: customers with no orders, departments with no staff.
10 exercises Self joinA self join joins a table to itself, for example to put each employee next to their manager.
2 exercises CASE WHENCASE WHEN works like IF in a spreadsheet: it labels rows or counts only some of them.
8 exercises SubqueriesA subquery is a query inside a query, for example "products priced above the average".
5 exercises EXISTSEXISTS answers a yes-or-no question across tables without multiplying the outer rows.
11 exercises UNIONUNION stacks rows and removes duplicates; UNION ALL keeps every row from both queries.
2 exercises COALESCECOALESCE replaces NULL with a fallback; NULLIF turns a sentinel value into NULL for safe calculations.
5 exercises CTEs (WITH)A common table expression names an intermediate result so a hard question becomes a few readable steps.
12 exercises Window functionsWindow functions calculate across related rows without collapsing them: rankings, running totals, previous month.
12 exercises Text functionsLOWER, TRIM, SUBSTR and REPLACE tidy up text so duplicates and groups line up.
3 exercises