美文网首页
Delete Duplicate Emails

Delete Duplicate Emails

作者: myang199088 | 来源:发表于2015-04-15 09:01 被阅读5次
  • 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 |
    +----+------------------+

  • Code
    delete from Person where Id not in (select * from (select min(Id) from Person group by Email) t);

相关文章

网友评论

      本文标题:Delete Duplicate Emails

      本文链接:https://www.haomeiwen.com/subject/ktcsfttx.html