Kamran Agayev's Oracle Blog

Oracle Certified Master

How delete duplicate rows

Posted by Kamran Agayev A. on December 6th, 2008

Today I want to give you an example of how delete duplicate rows from a table
Let’s create new table with name students:
CREATE TABLE students (ID NUMBER, NAME VARCHAR2(10), surname VARCHAR2(10))
Now let’s add some duplicate rows to it:
INSERT INTO students VALUES (1, ‘Kamran’, ‘Agayev’);
INSERT INTO students VALUES (2, ‘Elcin’, ‘Mammedov’);
INSERT INTO students VALUES (1, ‘Kamran’, ‘Agayev’);
INSERT INTO students VALUES (2, ‘Elcin’, ‘Mammedov’);
INSERT INTO students VALUES (1, ‘Kamran’, ‘Agayev’);
INSERT INTO students VALUES (2, ‘Elcin’, ‘Mammedov’);
And now let’s look to the data of our table:
SELECT * FROM students
1  Kamran  Agayev
2  Elcin   Mammedov
1  Kamran  Agayev
2  Elcin   Mammedov
1  Kamran  Agayev
2  Elcin   Mammedov
Now let’s delete duplicate rows using this script:
 
DELETE FROM students   
WHERE ROWID NOT IN (SELECT MAX (ROWID) FROM students GROUP BY ID) 
And now let’s look to students table
SELECT * FROM students
1  Kamran  Agayev
2  Elcin   Mammedov

8 Responses to “How delete duplicate rows”

  1. H.Tonguç Yılmaz Says:

    when data is huge performance can be a primary concern, so check asktom on Removing Duplicates – http://www.oracle.com/technology/oramag/oracle/04-jul/o44asktom.html

  2. kamranagayev Says:

    Thank you very much Tonguc for directing us to Tom’s valuable answer. It’s much more speeder by using analytic function.

  3. oruvan Says:

    Did you actually test this anywhere ?

  4. Kamran Agayev A. Says:

    Yes of course! Don’t you think it should work? :)

  5. Ravi Kumar Says:

    thank you its works

  6. Kamran Agayev A. Says:

    You’re welcome Ravi

  7. ma jamil Says:

    hi Kamran,
    mashaALLAH…you blog is really informatics and your work is also really superb.
    Dear, I am getting issue regarding duplicates records, please find below my thread against find and delete duplicate records, in my case there are number of duplicate records and i m facing issue to find and delete all dup. records,
    http://scn.sap.com/thread/2129829

    Regards,
    ma jamil

  8. Kamran Agayev A. Says:

    Have you checked the script I’ve provided in this article? Have you run it? What you got?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>