Failed payments by month
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 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.
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
Cleaning is the same whichever status you report on, so keep the expressions identical to the revenue query. A report that cleans the status in one query and not the other gives numbers that do not add up.
A hint if you get stuck
Filter with LOWER(TRIM(status)) = 'failed', group by substr(paid_at, 1, 7), and convert the amount the same way as for revenue: strip $ and commas, TRIM, CAST AS NUMERIC.
Background you need
Common mistakes on this exercise
- Status not cleaned: Failed and 'failed ' are missed.
- Refunded payments counted as failed.
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