easyGROUP BYHAVING
Duplicate Emails
List the email addresses that appear more than once in the person table.
Start practicePrompt
Duplicate Emails
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_.
Expected concepts
- GROUP BY
- HAVING
- COUNT
- NULL handling
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.