Delete Duplicate Emails - Problem

You are given a table Person that contains email addresses with their corresponding IDs.

Your task: Write a DELETE statement to remove all duplicate emails, keeping only the one with the smallest ID.

Important: You must write a DELETE statement, not a SELECT statement. The solution should modify the table in place.

  • Each email may appear multiple times with different IDs
  • Keep the row with the minimum ID for each email
  • Delete all other duplicate rows

Table Schema

Person
Column Name Type Description
id PK int Primary key, unique identifier
email varchar Email address (no uppercase letters)
Primary Key: id
Note: Each row represents a person with their email address

Input & Output

Example 1 — Basic Duplicate Removal
Input Table:
Output:
💡 Note:

The email [email protected] appears twice with IDs 1 and 3. We keep the row with the smaller ID (1) and delete the duplicate with ID 3.

Example 2 — Multiple Duplicates
Input Table:
Output:
💡 Note:

Multiple emails have duplicates: [email protected] (IDs 1,3) and [email protected] (IDs 2,5). We keep the rows with smaller IDs (1,2) and delete the duplicates (3,5).

Constraints

  • 1 ≤ id ≤ 1000
  • email contains no uppercase letters
  • email follows valid email format

Visualization

Tap to expand
Delete Duplicate Emails Problem OverviewBefore DELETEidemail1[email protected]2[email protected]3[email protected]Duplicate emailSQL DELETE OperationDELETE p1 FROM Person p1INNER JOIN Person p2WHERE p1.email = p2.emailAND p1.id > p2.idAfter DELETEidemail1[email protected]2[email protected]✓ Only unique emails remainKey Insight: Keep smallest ID for each email
Understanding the Visualization
1
Identify
Find duplicate emails
2
Compare
Determine which IDs to keep
3
Delete
Remove duplicate rows
Key Takeaway
🎯 Key Insight: Use self-join or subquery to compare IDs and delete higher-numbered duplicates
Asked in
Google 28 Amazon 22 Microsoft 18
125.0K Views
High Frequency
~12 min Avg. Time
892 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