Querydrill

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.

  1. Customers per country Pro
  2. Spend per customer Pro
  3. Repeat buyers Pro
  4. Revenue by month Pro
  5. Average salary per department Pro
  6. First and last order Pro
  7. Orders per status Pro
  8. Products per category Pro
  9. Units sold per product Pro
  10. Customers who ordered Pro
  11. Average paid order Pro
  12. Active in several months Pro

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.

  1. Orders with customer names Pro
  2. Customers who never ordered Pro
  3. Revenue per country Pro
  4. Everyone with a department Pro
  5. Revenue per product Pro
  6. Revenue per category Pro
  7. Products never sold Pro
  8. Headcount per department Pro
  9. Orders per customer Pro
  10. Products per order Pro

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.

  1. Rank within department Pro
  2. Running revenue total Pro
  3. Top earner per department Pro
  4. Dense rank Pro
  5. Change from the previous order Pro
  6. Share of revenue Pro
  7. Each customer's first order Pro
  8. Month over month Pro
  9. Top two per category Pro
  10. Pivot the statuses Pro

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.

  1. Clean up the country report Free
  2. Find duplicate accounts Pro
  3. Total revenue from text amounts Pro
  4. Revenue by month Pro
  5. Signups per plan Pro
  6. Paying customers by country Pro
  7. First payment per customer Pro

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 free

Questions

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.