Skip to main content
`where not exists` will delete the rows **without** duplicates.
Source Link
Juan Carlos Oropeza
  • 48.4k
  • 14
  • 87
  • 128

If you have no other unique identifier, you can use ctid:

delete from mytable where not exists (select 1   from mytable t2   where t2.name = mytable.name and   t2.address = mytable.address and   t2.zip = mytable.zip and   t2.ctid > mytable.ctid   ); 

It is a good idea to have a unique, auto-incrementing id in every table. Doing a delete like this is one important reason why.

If you have no other unique identifier, you can use ctid:

delete from mytable where not exists (select 1   from mytable t2   where t2.name = mytable.name and   t2.address = mytable.address and   t2.zip = mytable.zip and   t2.ctid > mytable.ctid   ); 

It is a good idea to have a unique, auto-incrementing id in every table. Doing a delete like this is one important reason why.

If you have no other unique identifier, you can use ctid:

delete from mytable where exists (select 1 from mytable t2 where t2.name = mytable.name and t2.address = mytable.address and t2.zip = mytable.zip and t2.ctid > mytable.ctid ); 

It is a good idea to have a unique, auto-incrementing id in every table. Doing a delete like this is one important reason why.

Source Link
Gordon Linoff
  • 1.3m
  • 62
  • 706
  • 857

If you have no other unique identifier, you can use ctid:

delete from mytable where not exists (select 1 from mytable t2 where t2.name = mytable.name and t2.address = mytable.address and t2.zip = mytable.zip and t2.ctid > mytable.ctid ); 

It is a good idea to have a unique, auto-incrementing id in every table. Doing a delete like this is one important reason why.