Revenue per plan
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
From 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.
signups_raw (first 12 of 14 rows)
| id | country | plan | signed_up | |
|---|---|---|---|---|
| 1 | anna@mail.com | US | Pro | 2025-01-04 |
| 2 | Anna@Mail.com | us | pro | 2025-01-09 08:30:00 |
| 3 | bruno@mail.com | BR | FREE | 2025-01-15T10:02:00 |
| 4 | chloe@mail.com | Us | Pro | 2025-02-02 |
| 5 | dev@mail.com | IN | NULL | 2025-02-11 17:45:00 |
| 6 | elif@mail.com | tr | free | 2025-02-20 |
| 7 | farah@mail.com | US | Team | 2025-03-01T09:00:00 |
| 8 | CHLOE@mail.com | US | pro | 2025-03-05 |
| 9 | gus@mail.com | nl | Free | 2025-03-14 |
| 10 | hana@mail.com | NULL | Pro | 2025-03-22 12:00:00 |
| 11 | ivo@mail.com | NL | team | 2025-04-02 |
| 12 | jun@mail.com | NULL | 2025-04-10T16:20:00 |
payments_raw (first 12 of 16 rows)
| id | signup_id | amount | status | paid_at |
|---|---|---|---|---|
| 1 | 1 | $12.00 | paid | 2025-01-04 |
| 2 | 1 | $12.00 | Paid | 2025-02-04 08:00:00 |
| 3 | 4 | $1,200.00 | paid | 2025-02-02T11:00:00 |
| 4 | 3 | 45 | failed | 2025-01-16 |
| 5 | 3 | 45 | PAID | 2025-01-17 |
| 6 | 5 | $99 | refunded | 2025-02-12 |
| 7 | 7 | $1,200.00 | paid | 2025-03-01 09:05:00 |
| 8 | 7 | 12.5 | paid | 2025-03-15 |
| 9 | 8 | $12.00 | Failed | 2025-03-05 |
| 10 | 9 | 30.50 | paid | 2025-03-14T18:00:00 |
| 11 | 10 | $12.00 | paid | 2025-03-22 |
| 12 | 11 | $600 | Paid | 2025-04-02 10:10:00 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
Three cleaning steps in one query: the status filter, the plan label and the amount conversion. Do each one once, in the same expression in SELECT and GROUP BY, or the groups split on the raw spelling.
A hint if you get stuck
Join payments to signups on signup_id, keep paid payments, group by COALESCE(LOWER(TRIM(plan)), 'unknown') and sum the converted amount.
Background you need
Common mistakes on this exercise
- Status not cleaned.
- Spaces around the plan not trimmed.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exercise