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 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.
Result columns · in this order
countryrevenueHow to approach it
JOIN customers, GROUP BY COALESCE(c.country, 'unknown'), SUM(o.total_amount), then ORDER BY revenue DESC, country ASC.
Sample input
| order_id | buyer_id | total_amount |
|---|---|---|
| 1001 | 1 | 80 |
| 1004 | 1 | 220 |
| 1005 | 2 | 540 |
| 1010 | 6 | 640 |
4 rows — all rows shown.
| customer_id | country |
|---|---|
| 1 | US |
| 2 | null |
| 3 | GB |
| 4 | IN |
| 6 | GB |
5 rows — all rows shown.
Expected output
| country | revenue |
|---|---|
| GB | 990 |
| unknown | 650 |
| US | 330 |
| IN | 190 |
4 rows — all rows shown.
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. Sum order totals at order grain (no fan-out here, orders is the only fact table joined).
Expected skills
Joining then aggregating, grouping on a COALESCE expression, and deterministic ordering of grouped results.
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.