Learn how to Make Money Online doing freelancing, Affiliate Marketing, Blogging and many more ...
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Database > Oracle

Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Article Tools Search this Article Display Modes

Delete duplicate rows from Oracle


On 26th May, 2005
Delete duplicate rows from Oracle

Show Printable Version Email this Page Subscription Add to Favorites Copy Delete duplicate rows from Oracle link

Author

shabbir ( Go4Expert Founder )

Shabbir is a developer in the field of Applications, web as well as database designing and is devoted to the optimization and usability of the code. He maintains Programming forum and is a C++ addict.


All articles By shabbir

Recent Articles

Similar Articles

There are times when duplicate rows somehow creep into the table. The best scenario to happen this is when the data is to be imported from some other table or data and the Constraints are removed so that data import successfully.

Now in Oracle you can delete the duplicate entries by just executing a simple SQL because Oracle stores an index to each row in a table known as ROWID

Code: SQL
CREATE TABLE duptest
  ( Id varchar(5),
  nonid varchar(5));
I create a table with just 2 fields. I dont have any constraint on ID because I would import some data which may be duplicated but after importing I would like to make the ID as PRIMARY KEY

Insert some date to simulate duplicates
Code: SQL
INSERT INTO duptest VALUES('1','a');
Insert them more than once.
Code: SQL
INSERT INTO duptest VALUES('2','b');
SQL to delete the duplicate rows
Code: SQL
DELETE FROM duptest WHERE rowid NOT IN (SELECT max(rowid) FROM duptest GROUP BY id);
Explanation :- In the subquery I Select a unique rowid from all the rowid's It can be MAX, MIN any one and the group by clause should include all the UNIQUE columns I desire. Say I want a composite primary key then the group by should be group by id1,id2 ...

The the subquery returns one record for the dupliacte ID's and I delete all of them that are not in the subquery and that deletes all the dupliacte rows from the database.
Old 04-01-2006, 05:19 PM   #2
Team Leader
 
coderzone's Avatar
 
Join Date: Jul 2004
Posts: 466
Thanks: 2
Thanked 10 Times in 6 Posts
Rep Power: 7
coderzone is on a distinguished road

Re: Delete duplicate rows from Oracle


Thats good way to delete the rows.
__________________
My First Article - Query Optimization | All My Articles at Go4Expert.
coderzone is offline   Reply With Quote
Old 02-26-2008, 04:49 PM   #3
Contributor
 
Join Date: Feb 2008
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3
lead.smart34 is on a distinguished road

Re: Delete duplicate rows from Oracle


nice way
lead.smart34 is offline   Reply With Quote
Old 07-24-2008, 02:33 PM   #4
Light Poster
 
Join Date: Jul 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
harminder is on a distinguished road

Re: Delete duplicate rows from Oracle


a way to delete duplicate rows in oracle table You can also detect and delete duplicate rows using Oracle analytic functions:

delete from
customer
where rowid in
(select rowid from
(select
rowid,
row_number()
over
(partition by custnbr order by custnbr) dup
from customer)
where dup > 1);
__________________
web site design
Free Templates
harminder is offline   Reply With Quote
Old 11-25-2008, 09:24 PM   #5
Go4Expert Member
 
Join Date: Nov 2008
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 0
kidas is on a distinguished road

Re: Delete duplicate rows from Oracle


Delete Duplicate Rows using Analytic functions
Code:
DELETE FROM TABLENAME
WHERE ROWID IN (SELECT ROWID FROM (SELECT ROW_NUMBER() OVER (PARTITION BY dup_col ORDER BY dup_col_1) rnk
                              	   FROM   TABLENAME)
                WHERE rnk>1);
__________________
Find me at Oracle Forums
kidas is offline   Reply With Quote
Old 04-28-2010, 12:49 AM   #6
Newbie Member
 
Join Date: Apr 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Alok_1978 is on a distinguished road

Re: Delete duplicate rows from Oracle


Incase if you want to check for all the columns then try this query:

delete from emp where rowid not in (select max(rowid) from emp group by id,name,no)

My emp table had 3 columns id,name,no and you need to group by all the columns in order to check if any column contains different value or not.if yes then that record will not be considered as duplicate and will not be deleted. The above query will delete the record only when all the columns have same value
Alok_1978 is offline   Reply With Quote
Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
Audit Trail in Oracle shabbir Oracle 5 01-08-2009 08:33 PM
Uninstalling Oracle 8 Personal Database shabbir Oracle 4 10-19-2008 08:34 PM

 

All times are GMT +5.5. The time now is 05:30 AM.