Delete Duplicate Emails

Write a SQL query to delete all duplicate records and keeping only unique ones.

Example:

Table: Person
+----+------------------+
| Id | Email            |
+----+------------------+
1  | ram@example.com  |
2  | Shyam@example.com|
3  | ram@example.com  |
4  | ram@example.com  |
+----+------------------+
Result: After duplicate delete
+----+------------------+
| Id | Email            |
+----+------------------+
1  | ram@example.com  |
2  | Shyam@example.com|
+----+------------------+

Approach :

Mysql


DELETE n1 FROM Person n1, Person n2 
WHERE n1.Id > n2.Id AND n1.Email = n2.Email;


DELETE FROM Person WHERE id NOT IN 
(SELECT MIN(Id) FROM (SELECT * FROM Person) a 
GROUP BY Email)

No comments:

Post a Comment