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 and revenue (the summed total_amount) for buyers whose total revenue is at least 500. Order by revenue descending, then buyer_id ascending.
Result columns · in this order
buyer_idrevenueHow to approach it
GROUP BY buyer_id, then HAVING SUM(total_amount) >= 500, ORDER BY revenue DESC, buyer_id ASC.
Sample input
| order_id | buyer_id | total_amount |
|---|---|---|
| 1005 | 2 | 540 |
| 1011 | 2 | 110 |
| 1010 | 6 | 640 |
| 1006 | 6 | 60 |
| 1001 | 1 | 80 |
5 rows — all rows shown.
Expected output
| buyer_id | revenue |
|---|---|
| 6 | 700 |
| 2 | 650 |
2 rows — all rows shown.
Constraints
GROUP BY buyer_id. The threshold is on an aggregate, so use HAVING SUM(total_amount) >= 500 (not WHERE). Order by revenue DESC, buyer_id ASC.
Expected skills
Filtering groups with HAVING on an aggregate, distinct from row filtering in WHERE.
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.