Write a SQL query to delete all duplicate e-mail entries in a table named Person
, keeping 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 this table.
For example, after running your query, the above Person
table should has the following rows:
+----+------------------+| Id | Email |+----+------------------+| 1 | [Email protected] | | 2 | [email protected] | +----+------------------+
# Write your MySQL query statement belowdelete from the person where Id not in (the Select ID from ( select ID, if (STRCMP (@ Prev, email) =0, false, True) as Diff, @prev: = email from (SELECT * FROM person P2 order by Email, Id ASC) x,< c5/> (select @prev: = ') y) t where t.diff=true)
Leetcode Database:delete Duplicate Emails