Skip to content

Swethashajan/Leetcode-solutions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Leetcode-solutions

Delete Duplicate Emails

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

For SQL users, please note that you are supposed to write a DELETE statement and not a SELECT one.

For Pandas users, please note that you are supposed to modify Person in place.

After running your script, the answer shown is the Person table. The driver will first compile and run your piece of code and then show the Person table. The final order of the Person table does not matter.

The result format is in the following example.

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.

Answer:

DELETE P1 FROM PERSON P1 JOIN PERSON P2 WHERE P1.EMAIL=P2.EMAIL AND P1.ID>P2.ID

delete from Person where Id not in (select min_id from (select min(Id) as min_id from Person group by Email) as a)

About

to showcase my learning journey

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published