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:
| id | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
Output:
| id | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
💡 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:
| id | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
| 4 | [email protected] |
| 5 | [email protected] |
Output:
| id | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
| 4 | [email protected] |
💡 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 -
emailcontains no uppercase letters -
emailfollows valid email format
Visualization
Tap to expand
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code