SQL TOPICS

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

SELECT is how every question starts: name the columns you want and the table they live in.

1 exercise
WHERE

WHERE keeps only the rows that match your conditions, before anything is counted or summed.

3 exercises
ORDER BY

ORDER BY sorts the result; LIMIT keeps only the first rows. Together they answer "top 5" questions.

1 exercise
DISTINCT

DISTINCT removes duplicate rows from the result, so each combination appears once.

4 exercises
NULL

NULL means "unknown", so it never equals anything, not even another NULL. Test it with IS NULL.

5 exercises
LIKE, IN and BETWEEN

LIKE matches text patterns, IN checks a list of values, BETWEEN checks an inclusive range.

1 exercise
COUNT, SUM and AVG

Aggregate functions turn many rows into one number: how many, how much, on average.

11 exercises
GROUP BY

GROUP BY splits rows into groups and runs the aggregate once per group: revenue per country, orders per month.

10 exercises
HAVING

HAVING filters on aggregate results, such as "countries with more than 3 customers".

6 exercises
Dates

Business questions are usually about time: revenue per month, signups this quarter, days to resolve.

14 exercises
INNER JOIN

JOIN connects tables through a shared key, so you can see customer names next to their orders.

10 exercises
LEFT JOIN

LEFT JOIN keeps every row from the first table, even without a match: customers with no orders, departments with no staff.

10 exercises
Self join

A self join joins a table to itself, for example to put each employee next to their manager.

2 exercises
CASE WHEN

CASE WHEN works like IF in a spreadsheet: it labels rows or counts only some of them.

8 exercises
Subqueries

A subquery is a query inside a query, for example "products priced above the average".

5 exercises
EXISTS

EXISTS answers a yes-or-no question across tables without multiplying the outer rows.

11 exercises
UNION

UNION stacks rows and removes duplicates; UNION ALL keeps every row from both queries.

2 exercises
COALESCE

COALESCE 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 functions

Window functions calculate across related rows without collapsing them: rankings, running totals, previous month.

12 exercises
Text functions

LOWER, TRIM, SUBSTR and REPLACE tidy up text so duplicates and groups line up.

3 exercises