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

Most efficient way to remove duplicate rows

This script uses a hash join, the most performance efficient way of joining huge tables, to find duplicate rows.

This script uses a hash join -- the most efficient way of joining huge tables -- to find duplicate rows. It has been tested on Oracle 6.0 to Oracle 9i v2, and it works in all of those versions.

-- Set hash join enabled 
DELETE FROM <table> 
WHERE rowid IN 
  (SELECT t1.rowid 
   FROM   <table> t1, <same-table> t2 
   -- primary key is (a1, a2) 
   WHERE  t1.a1 = t2.a1 
     AND  t1.a2 = t2.a2 
     AND  t1.rowid < t2.rowid) 

Reader Feedback

Una S. writes: This one confuses me. If there is a prmary key on cols (a1, a2), how can there possibly be more than one row with the same values for a1 and a2 in the table to begin with?

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free Oracle tips and scripts.
  • Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: Oracle tips, tutorials, and scripts from around the Web.

Dig Deeper on Oracle and SQL