> For the complete documentation index, see [llms.txt](https://anand-aryan.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anand-aryan.gitbook.io/leetcode/196.-delete-duplicate-emails.md).

# 196. Delete Duplicate Emails

Delete Duplicate Emails

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

+----+------------------+ | Id | Email | +----+------------------+ | 1 | <john@example.com> | | 2 | <bob@example.com> | | 3 | <john@example.com> | +----+------------------+ Id is the primary key column for this table.

For example, after running your query, the above Person table should have the following rows:

+----+------------------+ | Id | Email | +----+------------------+ | 1 | <john@example.com> | | 2 | <bob@example.com> | +----+------------------+

## Solution <a href="#solution" id="solution"></a>

```java
delete p1
from Person p1, Person p2
where p1.Email = p2.Email and p1.Id > p2.Id
```
