Delete Duplicate Emails - SQL - Easy - LeetCode
💻 coding

Delete Duplicate Emails - SQL - Easy - LeetCode

1 min read 124 words
1 min read
ShareWhatsAppPost on X
  • 1The SQL query deletes duplicate email entries from the Person table, retaining only the unique email with the smallest Id.
  • 2Id is the primary key in the Person table, ensuring each entry is uniquely identifiable.
  • 3The query uses a delete statement to remove duplicates by comparing email addresses and their corresponding Ids.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The SQL query deletes duplicate email entries from the Person table, retaining only the unique email with the smallest Id."

— Delete Duplicate Emails - SQL - Easy - LeetCode

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

Your output is the whole Person table after executing your sql. Use delete statement.

# Write your MySQL query statement below
Delete p1 from Person p1, Person p2 Where p1.Email = p2.Email And p1.Id>p2.Id;

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 16 October 2020 · 1 min read · 124 words

Part of AskGif Blog · coding

You might also like