CODE WORKSPACE

Revenue by buyer country

Return country and revenue (the SUM of orders.total_amount) grouped by buyer country. Replace a missing country with 'unknown'. Order by revenue descending, then country ascending.

JOIN customers, GROUP BY COALESCE(c.country, 'unknown'), SUM(o.total_amount), then ORDER BY revenue DESC, country ASC.

Sample input

ordersAll 12 orders (buyer_id and total_amount shown).
order_idbuyer_idtotal_amount
1001180
10041220
10052540
10106640
customersCustomer 2 has a NULL country, which becomes the unknown bucket.
customer_idcountry
1US
2null
3GB
4IN
6GB

Expected output

Expected outputRevenue summed per buyer country (missing country grouped as unknown), highest first.
countryrevenue
GB990
unknown650
US330
IN190

Constraints

JOIN orders to customers on customers.customer_id = orders.buyer_id. Group by COALESCE(country, 'unknown') so the missing country forms its own bucket.

Expected skills

Joining then aggregating, grouping on a COALESCE expression, and deterministic ordering of grouped results.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.