Problem
Write a SQL query to delete all duplicate email entries in a table named Person
, keeping only unique emails based on its smallest Id.
1 |
|
For example, after running your query, the above Person
table should have the following rows:
1 |
|
Note:
Your output is the whole Person
table after executing your sql. Use delete statement.
Explanation
- We need to use the
DELETE
statement to solve this problem. We can use two tablesPerson as p1
andPerson as p2
, then deleteWHERE
these two tables have the same emailAND
the first table’s id greater than the second table. WeDELETE
the first table.
Solution
1 |
|