Level 3: Advanced SQL Exercises
Level 3 is the SQL data analysts use every day. You write queries in
readable steps with WITH, pick each customer's latest order, rank a
leaderboard with ties, compute running totals and day-over-day changes, walk
an org chart and turn revenue into growth percentages.
Window functions (OVER (...)) are the big idea here: they calculate across
rows without collapsing them. As always, each lesson names the trap that
produces convincing but wrong numbers, like integer division turning every
percentage into 0.
The 9 exercises in this level
- 1 SQL WITH Clauses (CTEs) Name a step, query it like a table Find the customers who spend more than the average customer by naming a per-customer step with WITH, and compare against the right average. Start here — no prerequisites
- 2 SQL ROW_NUMBER() and the Latest Row The newest order of each customer Pick each customer's most recent order with ROW_NUMBER() OVER (PARTITION BY ...), and filter it in a CTE since WHERE can't see window functions. Recommended first: SQL WITH Clauses (CTEs)
- 3 SQL RANK() and DENSE_RANK() Leaderboards with ties Give tied players the same place on a leaderboard, and know the difference between ROW_NUMBER(), RANK() and DENSE_RANK(). Recommended first: SQL ROW_NUMBER() and the Latest Row
- 4 SQL Running Totals SUM() OVER (ORDER BY ...) Show each store's revenue adding up day by day with SUM() OVER, partitioned by store and ordered by day. Recommended first: SQL ROW_NUMBER() and the Latest Row
- 5 SQL LAG() and Day-over-Day Change Compare with the previous row Show how revenue changed from one day to the next with LAG(), and handle the first day, which has no previous value. Recommended first: SQL Running Totals
- 6 SQL Date Arithmetic Days between two dates Find the late deliveries by computing the days between two dates with julianday(), across months and leap years. Start here — no prerequisites
- 7 SQL Recursive CTEs Walk a tree with WITH RECURSIVE List everyone who reports to a manager, directly or not, with WITH RECURSIVE, and track how deep each person is in the org chart. Recommended first: SQL WITH Clauses (CTEs)
- 8 SQL Percent of Total Each row's share of the whole Show each category's share of revenue with SUM() OVER (), and avoid the integer division that turns every percentage into 0. Recommended first: SQL Running Totals
- 9 Project: SQL Monthly Growth Report Everything from this level Build a month-by-month growth report: total paid revenue per month in a CTE, compare each month with the one before using LAG, and compute the percent. Recommended first: SQL LAG() and Day-over-Day Change, SQL Percent of Total, SQL Date Arithmetic
Frequently asked questions
- What is a window function?
- A function with `OVER (...)` that looks at a group of rows around the current one, like a running total or the previous day's value, while keeping every row in the result. `GROUP BY` collapses rows; a window function doesn't.
- Why can't I filter on ROW_NUMBER() in WHERE?
- `WHERE` runs before window functions are computed. Put the query in a CTE (`WITH ranked AS (...)`) and filter the CTE's result.
- Do these work outside SQLite?
- CTEs and window functions are standard and work in PostgreSQL, MySQL 8, SQL Server and SQLite. Date functions differ: the date maths lesson uses SQLite's `julianday()` and says what PostgreSQL uses instead.
- Why are my percentages all 0?
- Dividing two integers gives an integer in SQL, so `30 / 40` is `0`. Multiply by `100.0` first: `100.0 * 30 / 40` is `75.0`.