Easy

NrDifficultyTitleLink

196

Easy

Delete Duplicate Emails

197

Easy

Rising Temperature

577

Easy

Employee Bonus

584

Easy

Find Customer Referee

619

Easy

Biggest Single Number

1075

Easy

Project Employees I

1141

Easy

User Activity for the Past 30 Days I

1251

Easy

Average Selling Price

1280

Easy

Students and Examinations

1484

Easy

Group Sold Products By The Date

1517

Easy

Find Users With Valid E-Mails

1527

Easy

Patients With a Condition

1633

Easy

Percentage of Users Attended a Contest

1667

Easy

Fix Names in a Table

1789

Easy

Primary Department for Each Employee

1978

Easy

Employees Whose Manager Left the Company

Questions

196. Delete Duplicate Emails

Table: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| email       | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.

Write a solution to delete all duplicate emails, keeping only one unique email with the smallest id.

Example 1:
Input: 
Person table:
+----+------------------+
| id | email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
Output: 
+----+------------------+
| id | email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+
Explanation: john@example.com is repeated two times. We keep the row with the smallest 
Id = 1.
DELETE p1.* FROM person p1, person p2
WHERE p1.email = p2.email 
    AND p1.id > p2.id 

Last updated