Start learning free

LESSON 23 OF 66 · SQL: INTERVIEW QUESTIONS

Find Duplicate Emails

Practice

A classic interview question: which email addresses appear more than once? In one sentence: group the rows by email, count the rows in each group, and keep the groups that count more than 1.

For this lesson the check adds a signups table to shop.db, with 8 rows:

id | email             | signed_up
1  | ana@example.com   | 2026-01-10
2  | ben@example.com   | 2026-02-03
3  | chloe@example.com | 2026-02-20
4  | ben@example.com   | 2026-02-21
5  | ana@example.com   | 2026-03-01
6  | dan@example.com   | 2026-03-14
7  | ben@example.com   | 2026-03-15
8  | eva@example.com   | 2026-04-01

HAVING is a filter for groups. WHERE runs on single rows, before they are grouped, so it can't test a count. HAVING runs after GROUP BY, so it can. Say you had a reviews table: this lists the products with at least 3 reviews.

SELECT product_id, COUNT(*) AS reviews
FROM reviews
GROUP BY product_id
HAVING COUNT(*) >= 3

Your query must print each repeated email with how many times it signed up, the most repeated first:

email | times
ben@example.com | 3
ana@example.com | 2
(2 rows)

Your task: in query, one clause per line, select email and COUNT(*) named times from signups, make one group per email, keep the groups with more than one row, and sort them by times, biggest first.


Assignment
Follow the Coding Tutorial to build the duplicate-email query one clause at a time.


Hint
The test on the count goes in HAVING, after GROUP BY email: WHERE can't see counts. Each clause is its own string ending with a space, so the strings join into one query.


STEP 1 OF 5
    Ctrl + Enter to check