Problem solve Get help with specific problems with your technologies, process and projects.

Deleting all but one of the duplicate records

I want to delete duplicate records in table x, and I executed a query saying:

delete from x where col1 in (
 select col1 from x group by col1 having count(*)>1) 
It is deleting all the records in table that have duplicate col1 values, but my problem is that I want one record to stay.

Deleting records would not be a problem if you had a primary key on your table. The absence of a PK constraint leads to this exact problem. If you had a PK on the table, then you could delete by the PK columns to uniquely identify a row.

But a solution to your problem is not too difficult. This solution has elegantly been posted on Tom Kyte's AskTom Web site. Rather than duplicate the work, I'll give you the URL below for your solution:

This was last published in August 2004

Dig Deeper on Oracle database design and architecture

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.