196. Delete Duplicate Emails

Write a SQL query to delete all duplicate email entriesin a table named Person, keeping only unique emails based on its smallest Id.

+----+------------------+

| Id | Email            |

+----+------------------+

| 1 | [email protected] |

| 2 | [email protected]  |

| 3 | [email protected] |

+----+------------------+

Id is the primary key column for thistable.

For example,after running your query, the above Person tableshould have the following rows:

+----+------------------+

| Id | Email            |

+----+------------------+

| 1 | [email protected] |

| 2 | [email protected]  |

+----+------------------+


DELETE a
FROM Person a, Person b
WHERE a.Id>b.Id AND a.Email=b.Email;




你可能感兴趣的:(MySQL,LeetCode)