NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts

    196. Delete Duplicate Emails

    Easy
    Database
    Answered: Nov 28, 2022
    View on LeetCode

    Problem Description

    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.

    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.

    Examples

    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.

    Solutions

    Complexity Analysis

    Time Complexity:O(n2)

    The time complexity is O(n2)O(n^{2})O(n2) because of the self-join operation in the subquery, which compares each row with every other row to find duplicates.

    Space Complexity:O(n)

    The space complexity is O(n)O(n)O(n) because of the temporary table created by the subquery to hold the results of the join operation.

    The query deletes rows from the Person table where the id is in a subquery that selects the id of duplicate emails. The subquery joins the Person table with itself to find pairs of rows with the same email, and it selects the id of the row with the larger id (the duplicate). The outer query then deletes those duplicate rows, leaving only the row with the smallest id for each unique email.

    Complexity Analysis

    Time Complexity:O(n2)

    The time complexity is O(n2)O(n^{2})O(n2) because of the self-join operation in the query, which compares each row with every other row to find duplicates.

    Space Complexity:O(n)

    The space complexity is O(n)O(n)O(n) because of the temporary table created by the join operation to hold intermediate results.

    This alternative approach uses a DELETE statement with a JOIN to remove duplicate emails. It joins the Person table with itself on the email column, and deletes rows from the second instance (p2) where the id is greater than the corresponding row in the first instance (p1). This effectively removes all duplicate emails except for the one with the smallest id.

    Conclusion

    ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
    Using Subquery
    O(n2)O(n^{2})O(n2)O(n)O(n)O(n)Explicit and easy to understand the logic step by stepRequires a nested subquery which adds complexity
    Using JOIN
    O(n2)O(n^{2})O(n2)O(n)O(n)O(n)Most concise and idiomatic SQL, directly deletes duplicates in a single statementSame time complexity as the subquery approach due to self-join

    The optimal approach for this problem is Solution 2: Using JOIN. While both solutions have O(n2)O(n^{2})O(n2) time complexity due to the self-join, the JOIN approach is more concise and idiomatic, directly deleting duplicate rows in a single DELETE ... JOIN statement without needing a nested subquery.


    Previous136. Single Number
    Next217. Contains Duplicate
    On this page
    MySQL
    MySQL11 lines
    1DELETE FROM Person AS p
    2WHERE p.id IN (
    3 SELECT id
    4 FROM
    5 ( SELECT p2.id
    6 FROM PERSON AS p1
    7 JOIN PERSON AS p2
    8 ON p1.email = p2.email
    9 WHERE p2.id > p1.id
    10 ) AS tmp
    11);
    MySQL
    MySQL5 lines
    1DELETE p2
    2 FROM Person AS p1
    3 JOIN Person AS p2
    4 ON p1.email = p2.email
    5 WHERE p2.id > p1.id;