Practice · Education · Medium

Cities with more than one university (HAVING)

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.

What you are given

universities(name, city). GROUP BY city, then HAVING COUNT(*) > 1. HAVING filters groups; WHERE filters rows.

Starting point

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.

Did you know

HAVING is the WHERE of the grouped world — it filters after aggregation, so it can reference COUNT/SUM/AVG. (SQLite docs)

More Education challenges

Open it in the playground →