BeginnerAggregation

Find duplicate emails

Task

Return every email that appears more than once in users, with how many times it appears. Columns: email, n.

Row order does not matter. Column names do not have to match. SQLite dialect.

Tables

users

id INTEGER, email TEXT

idemail
1asha@mail.com
2ben@mail.com
3asha@mail.com
4chen@mail.com
5ben@mail.com
6asha@mail.com
7dev@mail.com
Expected output
emailn
asha@mail.com3
ben@mail.com2
Show solution
SQL
SELECT email, COUNT(*) AS n
FROM users
GROUP BY email
HAVING COUNT(*) > 1;

WHERE filters rows before grouping; HAVING filters groups after aggregation, so the count condition belongs in HAVING.