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 avg_order_amount (AVG of total_amount, rounded to 2 decimals) grouped by buyer country. Replace a missing country with 'unknown'. Order by avg_order_amount descending, then country ascending.
Result columns · in this order
countryavg_order_amountHow to approach it
JOIN customers, GROUP BY COALESCE(c.country, 'unknown'), ROUND(AVG(o.total_amount), 2), then ORDER BY avg_order_amount DESC, country ASC.
Sample input
| order_id | buyer_id | total_amount |
|---|---|---|
| 1001 | 1 | 80 |
| 1004 | 1 | 220 |
| 1009 | 1 | 30 |
| 1005 | 2 | 540 |
| 1011 | 2 | 110 |
5 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 | avg_order_amount |
|---|---|
| unknown | 325 |
| GB | 198 |
| US | 110 |
| IN | 95 |
4 rows — all rows shown.
Constraints
JOIN orders to customers on customers.customer_id = orders.buyer_id. Group by COALESCE(country, 'unknown'). Use ROUND(AVG(total_amount), 2). Order by avg_order_amount DESC, country ASC.
Expected skills
Joining then aggregating, grouping on a COALESCE expression, and AVG.
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.