Intermediate#05 · GROUP BY
Customers per country
Marketing wants to know where customers are. Return each country and its number of customers, largest first. Break ties alphabetically by country.
Write your query in the lab. It runs in your browser and tells you exactly what's wrong if it doesn't match.
Solve it in the labHints
Hint 1
GROUP BY country makes one row per country.
Hint 2
Sort by the count descending, then by country: ORDER BY customers DESC, country.
Expected output
Your result should match this, in this order (7 rows).
| country | customers |
|---|---|
| United States | 12 |
| United Kingdom | 9 |
| Portugal | 8 |
| Canada | 7 |
| Kenya | 6 |
| Australia | 3 |
| India | 3 |
Model answer
Show the solution
SELECT country, COUNT(*) AS customers FROM customers GROUP BY country ORDER BY customers DESC, country;
Other queries that return the same rows are correct too. The lab checks your result, not your exact wording.