Oracle: How to delete Duplicate rows from a table

April 16, 2009

There are many ways to delete duplicate rows from a table, here is one of simple way

   DELETE FROM Mytable WHERE rowid Not In (SELECT min(rowid) FROM Mytable  GROUP BY Column1);

In the sub query we  Select a unique rowid from all the rowid’s ,It can be using any one of  MAX, MIN functions  and the group by clause should include all the UNIQUE columns we desire. If we have a composite primary key on column1 and Column2  then the group by should be group by Column1,column2

The the sub query returns one record for the duplicate ID’s and I delete all of them that are not in the sub query and that deletes all the duplicate rows from the database.

Share and Enjoy:
  • Twitter
  • Google Buzz
  • DZone
  • Digg
  • Reddit
  • Diigo
  • StumbleUpon
  • del.icio.us
  • Yahoo! Buzz
  • Google Bookmarks
  • LinkedIn
  • Mixx
  • email
  • PDF
  • Add to favorites

tags: , ,
posted in Oracle by chandu

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org