Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check duplicate record in SQL Server [duplicate]

Does any one know how can I write a SQL Server script to check whether table is contain duplicate phone number?

Example:

I have a table called customer with following data

name   telephone
alvin  0396521254
alan   0396521425
amy    0396521425

How can I write a script in SQL Server that can return those records with duplicate telephone number??

like image 358
Jin Yong Avatar asked Nov 25 '10 04:11

Jin Yong


People also ask

How do I select duplicate records in SQL?

To select duplicate values, you need to create groups of rows with the same values and then select the groups with counts greater than one. You can achieve that by using GROUP BY and a HAVING clause.

How do I find duplicate records in SQL Server without GROUP BY?

1. Using the Distinct Keyword to eliminate duplicate values and count their occurences from the Query results. We can use the Distinct keyword to fetch the unique records from our database. This way we can view the unique results from our database.

How do I find duplicate rows in a data set?

DataFrame. duplicated() method is used to find duplicate rows in a DataFrame. It returns a boolean series which identifies whether a row is duplicate or unique.


1 Answers

To see values with duplicates:

  SELECT c.telephone
    FROM CUSTOMER c
GROUP BY c.telephone
  HAVING COUNT(*) > 1

To see related records in the table for those duplicates:

SELECT c.*
  FROM CUSTOMER c
  JOIN (SELECT c.telephone
          FROM CUSTOMER c
      GROUP BY c.telephone
        HAVING COUNT(*) > 1) x ON x.telephone = c.telephone
like image 197
OMG Ponies Avatar answered Oct 07 '22 10:10

OMG Ponies