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
DELETEstatement to solve this problem. We can use two tablesPerson as p1andPerson as p2, then deleteWHEREthese two tables have the same emailANDthe first table’s id greater than the second table. WeDELETEthe first table.
Solution
1 | |