File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Deleting duplicate rows using sql query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Deleting duplicate rows using sql query" Watch "Deleting duplicate rows using sql query" New topic

Deleting duplicate rows using sql query

Skanda Raman
Ranch Hand

Joined: Mar 21, 2008
Posts: 205

I have a requirement to delete rows using sql query in oracle database. Actually for instance i have employee table having empname and emplocation.

When i write the sql statement as "select * from employee" i get the below results

empname emplocation

user1 india
user2 india
user1 india
user1 india
user3 india
user1 india
user2 india

i have a requirement to delete multiple occurrence of user1 and user2 and just retain one copy of the record.

so my final result set should have

user1 india
user2 india
user3 india

please let me know how can i do this. i tried using distinct, count etc but not meeting the requirement as above.

Your suggestions would be precious
Srikanth Nalam

Joined: Feb 23, 2010
Posts: 20
Please mention the exact query you have used.
If you use select distinct empname, emplocation from employee you will get unique records with unique combination of these columns.

Thanks & Regards,
Srikanth Nalam.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
Welcome to the Ranch , Srikanth Nalam
Srikanth Nalam

Joined: Feb 23, 2010
Posts: 20
Thanks Campbell
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565


this query might give you a starting point to remove the duplicates. Oracle has a concept called rowid. It gives all your records a unique identifier.
This rowid should typically not be used in any query (it is a relational database after all, we should never address a record this way), but it can help you in this occasion.
You have to find all records that have the same values in all columns (duplicates), and only keep one of them.
In my example, I have kept the one with the lowest rowid.

Ensure that you understand the concept before trying it.
Test first on a test database!
Make a backup!

Regards, Jan

OCUP UML fundamental and ITIL foundation
I agree. Here's the link:
subject: Deleting duplicate rows using sql query
It's not a secret anymore!