Mysql – SQL which removes entries with duplicate emails

deleteMySQL

DELETE
FROM Person
WHERE Id IN (
 SELECT Id
 FROM Person
 GROUP BY Email
 HAVING COUNT(*) > 1 AND Id <> MIN(Id)
)

The above query produces the error:

You can't specify target table 'Person' for update in FROM clause

Why?

I am just trying to solve this problem to improve my SQL skills.

I checked the above SQL query here and it seems my syntax is fine.

Best Answer

Use multiple-table DELETE syntax:

DELETE t1.*
FROM Person t1
JOIN Person t2 USING (Email)
WHERE t1.id > t2.id