Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Return buyer_id, paid_revenue (sum of total_amount where status is paid), and refunded_amount (sum of total_amount where status is refunded) for each buyer. Order by buyer_id ascending.
Result columns · in this order
buyer_idpaid_revenuerefunded_amountHow to approach it
GROUP BY buyer_id; SUM(CASE WHEN status = 'paid' THEN total_amount ELSE 0 END) AS paid_revenue, and the same for refunded.
Sample input
| order_id | buyer_id | status | total_amount |
|---|---|---|---|
| 1001 | 1 | paid | 80 |
| 1005 | 2 | paid | 540 |
| 1011 | 2 | paid | 110 |
| 1010 | 6 | paid | 640 |
| 1006 | 6 | refunded | 60 |
5 rows — all rows shown.
Expected output
| buyer_id | paid_revenue | refunded_amount |
|---|---|---|
| 1 | 80 | 0 |
| 2 | 650 | 0 |
| 3 | 215 | 0 |
| 4 | 0 | 0 |
| 6 | 640 | 60 |
5 rows — all rows shown.
Constraints
GROUP BY buyer_id. Use SUM(CASE WHEN status = 'paid' THEN total_amount ELSE 0 END) and the same pattern for refunded. Every buyer appears once, with 0 where a status is absent.
Expected skills
Conditional aggregation: a CASE inside SUM to compute several metrics in one pass.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.