Duplicate Emails
List the email addresses that appear more than once in the person table.
Start practiceProblem statement
Prompt
Given the person table below, write a query that returns the email address of every person whose email appears more than once.
- Each duplicate email should be returned once, regardless of how many times it appears.
- Only the
emailcolumn should be in the result. person.emailmay beNULL; aNULLvalue is not an email and must not be treated as a duplicate.
Example
| id | |
|---|---|
| 1 | a@example.com |
| 2 | b@example.com |
| 3 | a@example.com |
| 4 | c@example.com |
Expected result:
| a@example.com |
Constraints
person.idis the primary key.person.emailis nullable.
Tips
GROUP BY emailthen filter the groups withHAVING COUNT(*) > 1.- Remember that
HAVINGfilters groups _after_ aggregation, whileWHEREfilters rows _before_.
Schema
CREATE TABLE person (
id INTEGER PRIMARY KEY,
email TEXT -- NULL allowed
);
Sample data
INSERT INTO person (id, email) VALUES
(1, 'a@example.com'),
(2, 'b@example.com'),
(3, 'a@example.com'),
(4, 'c@example.com');
Additional hidden fixtures are applied during validation to test edge cases.
Solution
One correct approach — try solving it yourself in the practice editor first, then compare. There is usually more than one valid solution.
-- Reference: one row per email appearing more than once.
SELECT email
FROM person
WHERE email IS NOT NULL
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY email;
Key concepts
- GROUP BY
- HAVING
- COUNT
- NULL handling
Related questions
- easyClasses With At Least 5 StudentsFind classes with five or more students enrolled.
- mediumCustomers Who Bought All ProductsFind customers who have purchased every product in the catalogue.
- mediumDepartments by Average SalaryFind departments whose average salary is above the company average.
- easyActive Users Per DayReport the number of distinct active users for each date of activity.
- mediumMonthly Sales RankingRank salespeople by their monthly sales, including ties, and handle months with no sales.