D8LooPFocus modeCODE 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.
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 |
Expected output
| buyer_id | revenue |
|---|---|
| 6 | 700 |
| 2 | 650 |
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.
Run the SQL query to inspect preview rows.