Write a SQL query to delete all duplicate e-mail entries in a table named person, keeping unique emails based on its s Mallest 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] | +----+------------------+
Two methods don't quite understand, t,p?
delete from person where Id not in ( select Id from (select min (ID) ID from person group by Email) p);
# Write Your MySQL query statement below SELECT from GROUP by having COUNT (*>1
Method 1:join and then delete the unwanted.
DELETE from JOIN on=WHERE> p1. Id;
Method 2: Do not join, with where:
DELETE from Person p1, person p2 WHERE = and > p1. Id;
Leetcode 196. Delete duplicate Emails. (Database)