Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Deleting duplicate rows using sql query

 
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 70370
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch , Srikanth Nalam
 
Srikanth Nalam
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell
 
Bartender
Posts: 2658
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srikanth,

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!
Etc..!


Regards, Jan
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic