Intermediate
Case 32 of 50
Case 032: Paid vs. Unpaid
customersorderspayments
Finance is doing a reconciliation pass, and the manager brings their question to Alex.
"For each customer, split their revenue: how much is actually paid, versus still pending?"
The data Alex is looking at
customers
| id | name | country | signup_date |
|---|---|---|---|
| 1 | Sam | Germany | 2026-01-02 |
| 2 | Nina | Canada | 2026-01-05 |
| 3 | David | Germany | 2026-01-10 |
| 4 | Maya | Japan | 2026-01-15 |
| 5 | Daniel | Canada | 2026-01-20 |
| … 5 more rows | |||
orders
| id | customer_id | order_date |
|---|---|---|
| 101 | 1 | 2026-01-05 |
| 102 | 2 | 2026-01-08 |
| 103 | 3 | 2026-01-12 |
| 104 | 1 | 2026-01-20 |
| 105 | 4 | 2026-02-01 |
| … 23 more rows | ||
payments
| id | order_id | amount | status |
|---|---|---|---|
| 601 | 101 | 6200 | paid |
| 602 | 102 | 3500 | paid |
| 603 | 103 | 3600 | pending |
| 604 | 104 | 60000 | paid |
| 605 | 105 | 12000 | paid |
| … 23 more rows | |||
TASK
Help Alex answer the manager:
“For each customer, show total paid revenue and total pending revenue, side by side.”
Write a query above and hit Run to see what comes back.
No hints requested yet — click "Get a hint" when you're stuck.
Alex's final query — this is for reference. Copy it into your own thinking, not into the editor above.
SELECT
customers.name,
SUM(CASE WHEN payments.status = 'paid' THEN payments.amount ELSE 0 END) AS paid_revenue,
SUM(CASE WHEN payments.status = 'pending' THEN payments.amount ELSE 0 END) AS pending_revenue
FROM customers
JOIN orders ON customers.id = orders.customer_id
JOIN payments ON payments.order_id = orders.id
GROUP BY customers.name
ORDER BY pending_revenue DESC;
What that query returns
Case Debrief
Conditional aggregation (SUM + CASE)
- SUM(CASE WHEN payments.status = 'paid' THEN payments.amount ELSE 0 END) turns the one status column into two separate totals — paid_revenue and pending_revenue — computed in the same GROUP BY pass, instead of running two separate queries and combining them.