Delete Duplicate Emails
Task: Write a solution to DELETE all duplicate emails, keeping only one unique email with the smallest id. NOTE: return SELECT * FROM Person to verify.
Table: Person
+-------------+--------------+
| Column Name | Type |
+-------------+--------------+
| id | INT |
| email | varchar(255) |
+-------------+--------------+
Input: Person table:
+----+------------------+
| id | email |
+----+------------------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+------------------+
Output:
+----+------------------+
| id | email |
+----+------------------+
| 2 | [email protected] |
| 1 | [email protected] |
+----+------------------+ View Reference Solution & Explanation ▼
This solution uses the `GROUP BY` clause to aggregate results to find the required output.
SELECT MIN(id) AS id, email FROM Person GROUP BY email;