leetcode_gym/_problems/sql_delete_duplicate_emails.md
2026-08-25 19:14:07 -06:00

1.1 KiB

layout, title, last_solved, category, leetcode_url, status
layout title last_solved category leetcode_url status
page Delete Duplicate Emails sql https://leetcode.com/problems/delete-duplicate-emails Attempted

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.

+----+------------------+
| 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

For deletion, start with selection, then modify to deletion

{% highlight sql %}

select a.* from person a join person b on a.email=b.email where a.id > b.id

{% endhighlight %}

{% highlight sql %}

delete a from person a join person b on a.email=b.email where a.id > b.id

{% endhighlight %}

image1