196_Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table namedPerson
, keeping only unique emails based on its smallest Id.
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
Id is the primary key column for this table.
For example, after running your query, the abovePerson
table should have the following rows:
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
+----+------------------+
Solution: self join
DELETE p1
FROM Person AS p1,
Person AS p2
WHERE
p1.Email = p2.Email
AND p1.Id > p2.Id
Last updated
Was this helpful?