SQL on messy data: one work week
Real tables are not clean. Countries are typed three ways, amounts are stored as text, dates come in two formats and the same person signed up twice. These exercises use a table like that, and the answers only pass once the cleaning is right.
Four days on the messy tables, then a four-step revenue report where each query builds on the one before, the way a request from a manager actually goes.
Day 1: Clean and deduplicate
- Clean up the country reportFrom Marta in Marketing: "Our country report shows US, us and ' Us' as three different countries. Send me each country once, in capitals with no spaces around it, with the number of signups. Leave out signups that have no country." Return two columns: country and signups.
- Find duplicate accountsFrom Sam in Support: "Some people signed up more than once with the same email, typed differently (capitals, extra spaces). List each email that has more than one account, in lowercase with no spaces, and how many accounts it has. Most accounts first, then alphabetically." Return email and accounts.
- Keep one account per personFrom Sam in Support: "For everyone who signed up more than once with the same email typed differently, I only want their earliest signup. The signup dates come in mixed formats, and if two signups fall on the same day the lowest id wins. Send me one row per person." Return the id of the account to keep and the email in lowercase with no spaces around it.
Day 2: Text amounts and mixed dates
- Total revenue from text amountsFrom Priya in Finance: "The amount column is text, like $1,200.00 and ' 30.50'. What is our total revenue from paid payments? Count a payment as paid whatever the capitals or spaces in its status." Return one number, rounded to 2 decimals.
- Monthly revenue from mixed datesFrom Priya again: "Same paid revenue, but by month please, oldest month first. Careful: paid_at comes in three formats." Return month as YYYY-MM and revenue rounded to 2 decimals.
- Signups per planFrom Leo in Product: "How many signups are on each plan? Write the plan names in lowercase, and count signups without a plan as unknown." Return plan and signups.
Day 3: Who actually pays
- Paying customers by countryFrom Marta: "For each country, cleaned as before, how many different people made at least one paid payment? Count a person once even if they have several accounts. Most payers first, then by country." Return country and payers.
- First payment per customerFrom Sam: "For each customer who paid at least once, what was the date of their first paid payment? One row per email, cleaned to lowercase, oldest first, then by email." Return email and first_paid as YYYY-MM-DD.
- Failed payments by monthFrom Priya in Finance: "How many payments failed each month, and how much money is that? Status capitals and spaces vary, and the amounts are text like $1,200.00. Oldest month first." Return month as YYYY-MM, failed (a count) and amount rounded to 2 decimals.
Day 4: Plans and time to pay
- Revenue per planFrom Leo in Product: "What is our paid revenue per plan? Plan names arrive in any capitals, and some signups have no plan: call those unknown. Amounts are text, and a payment is paid whatever the capitals or spaces in its status." Return plan and revenue rounded to 2 decimals.
- Signed up, never paidFrom Marta in Marketing: "Which people signed up but never made a paid payment? Treat emails as the same person whatever the capitals or spaces, and count a person as paid if any of their accounts has a paid payment. One row per email, in lowercase." Return email.
- Days from signup to first paymentFrom Leo: "For each signup that has a paid payment, how many days passed between the signup and the first paid payment? Both dates come in mixed formats: a plain date, a date with a time, or a date with a T and a time." Return the signup id and the number of days as a whole number.
Day 5: A revenue report in four steps
- Revenue report, step 1: revenue per monthYou are building the monthly revenue report. Start with the base: for each month that has paid orders, return the month as YYYY-MM and the total paid amount, in columns month and revenue.
- Revenue report, step 2: change on the previous monthExtend the monthly report. For each month return month, revenue, the previous month's revenue as prev_revenue, and the difference as change (revenue minus prev_revenue). The first month has no previous month, so both extra columns are NULL there.
- Revenue report, step 3: running totalAdd a year-to-date column. For each month return month, revenue and running_total: the sum of paid revenue from the first month up to and including that month.
- Revenue report, step 4: share of the totalFinish the report. For each month return month, revenue and share_pct: that month's percentage of all paid revenue, rounded to 1 decimal.
Other plans and cases
- 14-day SQL interview sprint (41)
- SQL for data analyst interviews (24)
- SQL for BI analyst interviews (21)
- SQL for data engineer interviews (24)
- Product analytics SQL in four sessions (12)
- SQL traps and time periods in three sessions (10)
- Window functions in six sessions (18)
- PostgreSQL features in four sessions (11)
- SQL business case: the refund review (5)
- SQL business case: the payroll review (5)
- SQL business case: the monthly revenue report (4)
- SQL business case: customer value and segments (4)
- SQL business case: product performance (4)
- All exercises by level
Your first 8 exercises are free, with no account, from any plan or level. Progress is kept in this browser; a free account keeps it on every device.