Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in SQL by (20.3k points)

I have a table in a PostgreSQL 8.3.8 database, which has no keys/constraints on it and has multiple rows with exactly the same values.

I would like to remove all duplicates and keep only 1 copy of each row.

There is one column in particular (named "key") which may be used to identify duplicates (i.e. there should only exist one entry for each distinct "key").

How can I do this? (ideally with a single SQL command) Speed is not a problem in this case (there are only a few rows).

1 Answer

0 votes
by (40.7k points)

Try the code given below:

DELETE FROM dupes a

WHERE a.ctid <> (SELECT min(b.ctid)

                 FROM   dupes b

                 WHERE  a.key = b.key);

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 27, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 4, 2019 in SQL by Tech4ever (20.3k points)

Browse Categories

...