Reputation: 608
I have following table. I want to delete duplicate rows and keep the one that has Name1 = Name2. So, in the example below row 2 and 4 should be removed.
ID | Name1 | Name2
1 | n1 | n1
1 | n1 | n2
2 | n1 | n1
2 | n1 | n2
How can I do this with SQL query ?
Upvotes: 0
Views: 110
Reputation: 1269513
I think this does what you want:
delete from t
where t.name1 <> t.name2 and
exists (select 1
from t t2
where t2.id = t.id and
t2.name1 = t2.name2
);
If you just want a select
, that can be done similarly:
select t.*
from t
where t.name1 = t.name2 or
not exists (select 1
from t t2
where t2.id = t.id and
t2.name1 = t2.name2
);
Upvotes: 1