How To Find Duplicate Records In Oracle Using Rowid



To delete duplicate rows, we can either keep the row with the minimum or maximum rowid. Below is the query where you preserve the row with minimum rowid. You can use whichever way you like codeDELETE FROM yourtable WHERE rowid not in (SELECT M. In case of duplicates (for the 5th and 6th place for example), oracle decides who to chose, and will never return more than five rows. Use rank, denserank, rownumber/rownum strictly depends on how you are going to handle duplicates. If you do not specify it explicitely, there is no wrong and no right answer.

  • Interview Question: How to Find Duplicate Records in Table?

Answer: To Find Duplicate Records in table you can use following query:

select a.* from Employee a where rowid !=
(select max(rowid) from Employee b where a.Employee_num =b.Employee_num;

1.Command Used to fetch records:

Select * from Employee;

Employee_numEmployee_nameDepartment
1RahulOBIEE
1RahulOBIEE
2RohitOBIEE


So we will start analysing above table.First we need to calculate the records or
fetch the records which are dupicate records.

We are again using concept of row_id here.So i am displaying row_ids of the
employees.

Find

select e.*,e.row_id from Employee e;

Employee_numEmployee_nameDepartmentRow_ID
1RahulOBIEE5001
1Rahul5002
2RohitOBIEE5003

Here you will see or analyse that for the duplicate records the row_ids are different.So our logic is fetch the records where the row_id is maximum.But we need to take care of joining condition because we want data for specific group.So in our table we will use Employee_num as condition.

How To Find Duplicate Records In Oracle Using Rowid

So to Fetch the Duplicate records from table following is the Query:

Delete duplicate records from oracle table

select a.* from Employee a where rowid !=
(select max(rowid) from Employee b where a.Employee_num =b.Employee_num;

It will fetch following results:

Employee_numEmployee_nameDepartmentRow_ID
1RahulOBIEE5002

Using Simple delete statement you can remove the duplicate records from the table.

Use Following Query:

Delete from Employee a where rowid != (select max(rowid) from Employee b where a.Employee_num =b.Employee_num;

Query 2 : Suggested By Reader Sai Jagdish:

DELETE FROM EMPLOYEE WHERE ROWID IN(SELECT ROWID FROM (SELECT E.*, ROW_NUMBER() OVER (PARTITION BY EMPNO ORDER BY EMPNO) RANK, ROWID FROM EMPLOYEE E) WHERE RANK!=1);

Query 3 : Suggested By Reader Milan Dhore

select Employee_num,count(*) from Employee
group by Employee_num
having count(Employee_num) > 1;

On Request of Reader :

Query to Find duplicate records in Mysql :

Lets say User wants to find out duplicate Employee Number records from Employee table,

SELECT
COUNT(Employee_Num)
Employees
HAVINGCOUNT(Employee_Num) > 1;

Delete Duplicate Records From Oracle Table

Hope the query explanation is helpful to you .Please post comment if any questions or queries if you have.These kind of questions are always asked in interviews.

Oracle Sql Duplicate Rows Query

NO TIME TO READ CLICK HERE TO GET THIS ARTICLE





Comments are closed.