Skip to content
Advertisement

How do I delete specific entries wherein there are duplicate entries

Sorry I’m just using this account from my old friend, he said he wanted it to have higher reputation. lol

I have a table that looks like this and I want to delete the entries wherein the next duplicate ones based from the first_name, middle_name, and last_name, and alias columns. While keeping the first entry or the ones who have the lowest possible id enter image description here

Advertisement

Answer

You can do it with a self join in the delete statement:

delete t
from tablename t inner join tablename tt
on tt.first_name = t.first_name and tt.last_name = t.last_name and tt.alias = t.alias  
and tt.id < t.id;

or by deleting all the rows with ids other than the min ids of each combination of first_name, last_name and alias:

delete from tablename
where id not in (
  select t.minid from (
    select min(id) minid
    from tablename
    group by first_name, last_name, alias
  ) t
)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement