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
idnamecountrysignup_date
1SamGermany2026-01-02
2NinaCanada2026-01-05
3DavidGermany2026-01-10
4MayaJapan2026-01-15
5DanielCanada2026-01-20
… 5 more rows
orders
idcustomer_idorder_date
10112026-01-05
10222026-01-08
10332026-01-12
10412026-01-20
10542026-02-01
… 23 more rows
payments
idorder_idamountstatus
6011016200paid
6021023500paid
6031033600pending
60410460000paid
60510512000paid
… 23 more rows
TASK

Help Alex answer the manager:

“For each customer, show total paid revenue and total pending revenue, side by side.”
SQL editor Ctrl+Enter to run
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.

Case closed.

Ready for the next one?

Next: Case 033 — Profit Margin →