Duplicate Emails - Problem

You have a table Person with the following structure:

  • id (int): Primary key, unique identifier for each person
  • email (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:
Output:
💡 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:
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:
Output:
email
💡 Note:

All emails are unique, so no duplicates are found. The result is an empty table.

Constraints

  • 1 ≤ Person.id ≤ 1000
  • email is guaranteed to be not NULL
  • email contains no uppercase letters

Visualization

Tap to expand
Duplicate Emails Problem OverviewInput: Person Tableidemail1[email protected]2[email protected]3[email protected]GROUP BYHAVINGOutput: Duplicate Emailsemail[email protected]Key Steps:1. GROUP BY email - Groups identical emails together2. COUNT(*) - Counts occurrences in each group3. HAVING COUNT(*) > 1 - Filters groups with duplicatesSQL Query:SELECT email FROM Person GROUP BY email HAVING COUNT(*) > 1;
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
Asked in
Amazon 15 Facebook 12 Google 8
128.0K Views
High Frequency
~8 min Avg. Time
2.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen