CODE WORKSPACE

Build merge source delta

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

ordersBuyer totals: 1→330, 2→650, 3→290, 4→190, 6→700.
order_idbuyer_idtotal_amount
10052540
10112110
10106640
1006660
1001180

Expected output

Expected outputOnly buyers whose summed revenue is at least 500, highest first.
buyer_idrevenue
6700
2650

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.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.