How To Eliminate Duplicates In Sql Query

Posted : admin On 17.08.2019

Find duplicate records,delete duplicate records in sql,delete duplicate records in oracle,delete duplicate records in postgres,remove duplicates using sql. Query to find Duplicate Records in Table? Query to Delete Duplicate Records from table. Amit S October 29, 2017 7 Comments.

  1. How To Remove Duplicates In Sql Query Using Rank
  2. How To Eliminate Duplicates In Sql Query Pdf

It's easy to find duplicates with one field:

  1. Finding duplicate values in a SQL table. Ask Question 1650. It's easy to find duplicates with one field: SELECT name, COUNT(email) FROM users GROUP BY email HAVING COUNT(email) 1 So if we have a table. Then we can use below query to delete duplicate query from table.
  2. We some reason mistakely we had missed to add any constraints in SQL server table and the records has been inserted duplicate in all columns with front-end application. Then we can use below query to delete duplicate query from table.

So if we have a table

This query will give us John, Sam, Tom, Tom because they all have the same email.

However, what I want is to get duplicates with the same email and name.

That is, I want to get 'Tom', 'Tom'.

The reason I need this: I made a mistake, and allowed to insert duplicate name and email values. Now I need to remove/change the duplicates, so I need to find them first.

DineshDB
4,0514 gold badges23 silver badges39 bronze badges
AlexAlex
12.3k20 gold badges71 silver badges123 bronze badges

28 Answers

Simply group on both of the columns.

Note: the older ANSI standard is to have all non-aggregated columns in the GROUP BY but this has changed with the idea of 'functional dependency':

In relational database theory, a functional dependency is a constraint between two sets of attributes in a relation from a database. In other words, functional dependency is a constraint that describes the relationship between attributes in a relation.

Support is not consistent:

  • Recent PostgreSQL supports it.
  • SQL Server (as at SQL Server 2017) still requires all non-aggregated columns in the GROUP BY.
  • MySQL is unpredictable and you need sql_mode=only_full_group_by:
    • GROUP BY lname ORDER BY showing wrong results;
    • Which is the least expensive aggregate function in the absence of ANY() (see comments in accepted answer).
  • Oracle isn't mainstream enough (warning: humour, I don't know about Oracle).
Andriy M
65k13 gold badges80 silver badges134 bronze badges
gbngbn
354k60 gold badges499 silver badges587 bronze badges

try this:

OUTPUT:

if you want the IDs of the dups use this:

OUTPUT:

to delete the duplicates try:

OUTPUT:

simhumileco
9,3814 gold badges62 silver badges62 bronze badges
KM.KM.
84.9k27 gold badges150 silver badges191 bronze badges
Chris Van OpstalChris Van Opstal
28.9k8 gold badges63 silver badges88 bronze badges

If you want to delete the duplicates, here's a much simpler way to do it than having to find even/odd rows into a triple sub-select:

And so to delete:

Much more easier to read and understand IMHO

Note: The only issue is that you have to execute the request until there is no rows deleted, since you delete only 1 of each duplicate each time

AncAinuAncAinu
4,4572 gold badges31 silver badges54 bronze badges
Find duplicates in sql queryMarthyM
1,6162 gold badges16 silver badges21 bronze badges
gaurav singhgaurav singh
Moyed Ansari
7,7402 gold badges29 silver badges52 bronze badges
PRADEEPTA VIRLLEYPRADEEPTA VIRLLEY

A little late to the party but I found a really cool workaround to finding all duplicate IDs:

Indivision DevIndivision Dev
Tanmay NeheteTanmay Nehete
1,5172 gold badges17 silver badges31 bronze badges

In case you work with Oracle, this way would be preferable:

xDBAxDBA

This selects/deletes all duplicate records except one record from each group of duplicates. So, the delete leaves all unique records + one record from each group of the duplicates.

Select duplicates:

Delete duplicates:

Be aware of larger amounts of records, it can cause performance problems.

Martin SilovskýMartin Silovský
Debendra DashDebendra Dash

If you wish to see if there is any duplicate rows in your table, I used below Query:

shekhar singhshekhar singh

How we can count the duplicated values??either it is repeated 2 times or greater than 2.just count them, not group wise.

as simple as

SysDragon
7,89515 gold badges42 silver badges76 bronze badges
Muhammad TahirMuhammad Tahir

This is the easy thing I've come up with. It uses a common table expression (CTE) and a partition window (I think these features are in SQL 2008 and later).

This example finds all students with duplicate name and dob. The fields you want to check for duplication go in the OVER clause. You can include any other fields you want in the projection.

Darrel LeeDarrel Lee

By Using CTE also we can find duplicate value like this

Debendra DashDebendra Dash
kapex
22.4k3 gold badges83 silver badges98 bronze badges
naveednaveed
How To Eliminate Duplicates In Sql Query

SELECT id, COUNT(id) FROM table1 GROUP BY id HAVING COUNT(id)>1;

I think this will work properly to search repeated values in a particular column.

Schemetrical
4,8302 gold badges19 silver badges39 bronze badges
user4877838user4877838
NarendraNarendra

This should also work, maybe give it try.

Especially good in your case If you search for duplicates who have some kind of prefix or general change like e.g. new domain in mail. then you can use replace() at these columns

veritaSveritaS

If you want to find duplicate data (by one or several criterias) and select the actual rows.

Lauri LubiLauri Lubi
Panky031Panky031

SELECT column_name,COUNT(*) FROM TABLE_NAME GROUP BY column1, HAVING COUNT(*) > 1;

rahul kumarrahul kumar
SheriffSheriff

To Check From duplicate Record in a table.

or

To Delete the duplicate record in a table.

or

Arun SolomonArun Solomon

We can use having here which work on aggregate functions as shown below

Here as two fields id_account and data are used with Count(*). So, it will give all the records which has more than one times same values in both columns.

How To Remove Duplicates In Sql Query Using Rank

We some reason mistakely we had missed to add any constraints in SQL server table and the records has been inserted duplicate in all columns with front-end application. Then we can use below query to delete duplicate query from table.

Here we have taken all the distinct records of the orignal table and deleted the records of original table. Again we inserted all the distinct values from new table to the original table and then deleted new table.

Suraj KumarSuraj Kumar
2,8295 gold badges11 silver badges26 bronze badges

You can use the SELECT DISTINCT keyword to get rid of duplicates. You can also filter by name and get everyone with that name on a table.

ParkofadownParkofadown
adeshadesh
5023 gold badges10 silver badges20 bronze badges
JIYAUL MUSTAPHAJIYAUL MUSTAPHA

protected by Lalit Kumar BJul 22 '15 at 8:55

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

How To Eliminate Duplicates In Sql Query Pdf

Not the answer you're looking for? Browse other questions tagged sqlduplicates or ask your own question.