Databases · Junior · Question 71 of 75
How do you get the 5 cities with the most users?
- 1
SELECT TOP 5 city FROM users; - 2
SELECT city FROM users ORDER BY city LIMIT 5; - ✓Correct answer:
SELECT city, COUNT(*) AS n FROM users GROUP BY city ORDER BY n DESC LIMIT 5;
Explanation
Group, count, sort by the count and limit. ORDER BY can use the alias n because it runs after SELECT.