Practice · Education · Medium
Return each city that has more than one university, with the count as n, most first (ties broken by city name).
Runs in your browser. No signup needed.
universities(name, city). GROUP BY city, then HAVING COUNT(*) > 1. HAVING filters groups; WHERE filters rows.
SELECT city, COUNT(*) AS n
FROM universities
GROUP BY city
HAVING COUNT(*) > 1
ORDER BY n DESC, city;
The reference solution is checked automatically when you run your query on the practice page — results are compared row by row, and order matters for this one.
HAVING is the WHERE of the grouped world — it filters after aggregation, so it can reference COUNT/SUM/AVG. (SQLite docs)