Skip to content
Advertisement

how to delete rows where a single column value is duplicated

I need to remove all rows having duplicated title column. Something like:

delete from clients where title is duplicated

For example, if five rows have the same title column – four of them shold be deleted.

How to do this?

Advertisement

Answer

If you have a unique column like id or somedate:

delete c 
from clients c inner join clients cc
on cc.title = c.title and cc.id < c.id

This code will keep the row with the minimum id among the duplicates.
See the demo.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement