Categories: SQL Complex Queries

Query to find Duplicate Records in Table? | Query to Delete Duplicate Records from table

  • 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;

CLICK HERE TO GET 20 COMPLEX SQL QUESTIONS

1.Command Used to fetch records:

Select * from Employee;

Employee_num Employee_name Department
1 Rahul OBIEE
1 Rahul OBIEE
2 Rohit OBIEE


  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.

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

Employee_num Employee_name Department Row_ID
1 Rahul OBIEE 5001
1 Rahul 5002
2 Rohit OBIEE 5003

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.

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

          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_num Employee_name Department Row_ID
1 Rahul OBIEE 5002

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

CLICK HERE TO GET 20 IMPORTANT INTERVIEW QUESTIONS FOR TECH MAHINDRA

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
    Employee_Num,
    COUNT(Employee_Num)
FROM
    Employees
GROUP BY Employee_Num
HAVING COUNT(Employee_Num) > 1;

CLICK HERE TO GET INFORMATION ABOUT DIFFERENT ORACLE ERRORS

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.

Home

NO TIME TO READ CLICK HERE TO GET THIS ARTICLE

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

4 hours ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

3 days ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

3 days ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

3 days ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

3 days ago

What are Roles and Responsibilities of L1 Support Engineer with real industry examples?

In this series we are starting with Roles and responsibilities of L1 support engineer .…

3 days ago