SQL for data analysts: the queries you will write every week
An analyst's SQL is not the SQL of a textbook. It is the same twenty query shapes written over and over against tables that are never quite clean: revenue by month, active customers, top products per region, the change from last month, who signed up but never paid. This page lists those shapes and the exercise that drills each one.
The queries behind most analyst reports
- Revenue, orders or users by month: GROUP BY on a truncated date, ordered chronologically.
- Breakdowns by segment: SUM and COUNT per country, plan, channel or category, with the share of the total.
- Top N: the best products, customers or regions, overall and within each group.
- Change over time: this month against last month with LAG, running totals, cumulative users.
- Activity and retention: customers active in a period, first order date, customers who have not ordered recently.
- Funnel and conversion: signed up, paid, still paying, as counts and rates.
- Data quality: duplicates, inconsistent labels, numbers stored as text, missing values.
Aggregation: counts, totals and averages by group
The GROUP BY question is the center of analyst work. Start with one grouping column and one aggregate, then add HAVING and distinct counts.
Joins: pulling the name next to the number
Reports need the customer name, not the customer id, and a join is how it gets there. The exercises cover the join that keeps every customer even with no orders, the three-table join for revenue by category, and the anti-join for things that never happened.
Window functions: rankings, running totals, month over month
Window functions are what move an analyst from "can query" to "can answer the question". Rank within a group, share of total, the previous month's value in the same row, the first order per customer.
Work mode: the messy export
Seven exercises use tables the way they arrive from real systems: country written four ways, duplicate rows, revenue stored as text with currency symbols, plan names with trailing spaces. Clean first, then aggregate, which is the actual job.
Why the checks use hidden data
A query that returns the right numbers for the ten rows on screen can still be wrong: a join that duplicates rows when a customer has two orders, a filter that happens to match. Every exercise reruns your query on a hidden copy of the tables with different rows, so the answer has to be right in general, which is the standard your manager will hold you to.
Practice the analyst queries
Six exercises are free without an account. Pro unlocks all 60, with hidden-data checks, hints, worked solutions and progress saved to your account.
Start practicing freeQuestions
Which SQL does this teach?
Standard SQL run on SQLite in your browser: SELECT, WHERE, GROUP BY, HAVING, joins, subqueries, CTEs and window functions. The same queries work in PostgreSQL, MySQL, BigQuery and SQL Server with at most small function-name changes.
I use Excel for reporting. Is SQL worth learning?
Yes, as soon as the data lives in a database or exceeds what a sheet handles comfortably. The GROUP BY and join exercises here map directly onto pivot tables and VLOOKUP, which makes the switch quick.
How long does the set take?
Most people finish the 60 exercises in two to three weeks at half an hour a day. The window-function and Work mode levels take the longest.
Does it cover date functions?
Yes: grouping by month, extracting the year, the first and last date per customer, and months active, using date functions that carry over to other databases.