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??
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With