Duplicate Emails - Problem
You have a table Person with the following structure:
id(int): Primary key, unique identifier for each personemail(varchar): Email address (guaranteed not NULL, no uppercase letters)
Write a SQL query to find all duplicate emails in the table.
Return the result in any order.
Table Schema
Person
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key, unique identifier |
email
|
varchar | Email address (no NULL values) |
Primary Key: id
Note: Each row represents a person with their email. Multiple people can have the same email.
Input & Output
Example 1 — Basic Duplicate Detection
Input Table:
| id | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
Output:
| [email protected] |
💡 Note:
The email [email protected] appears twice (id 1 and id 3), so it's returned as a duplicate. The email [email protected] appears only once, so it's not included in the result.
Example 2 — Multiple Duplicates
Input Table:
| id | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
| 4 | [email protected] |
| 5 | [email protected] |
Output:
💡 Note:
Both [email protected] and [email protected] appear twice, so both are returned as duplicates. [email protected] appears only once and is not included.
Example 3 — No Duplicates
Input Table:
| id | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
Output:
💡 Note:
All emails are unique, so no duplicates are found. The result is an empty table.
Constraints
-
1 ≤ Person.id ≤ 1000 -
emailis guaranteed to be not NULL -
emailcontains no uppercase letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Person table with id and email columns
2
Group
GROUP BY email to aggregate identical emails
3
Filter
HAVING COUNT(*) > 1 to find duplicates
Key Takeaway
🎯 Key Insight: Use GROUP BY to aggregate duplicates, then HAVING to filter aggregated results
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code