Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Retrieve the duplicate value in a column

Tags:

sql

sql-server

How could I filter out the duplicate value in a column with SQL syntax?

Thanks.

like image 276
Ricky Avatar asked Oct 05 '09 07:10

Ricky


People also ask

How do you select a repeated value 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 can we find duplicate records in a table?

One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.

How do I filter duplicate records in SQL?

The go to solution for removing duplicate rows from your result sets is to include the distinct keyword in your select statement. It tells the query engine to remove duplicates to produce a result set in which every row is unique.


1 Answers

A common question, though filtering out suggests you want to ignore the duplicate ones? If so, listing unique values:

SELECT col  FROM table GROUP BY col  HAVING (COUNT(col) =1 ) 

If you just want to filter so you are left with the duplicates

SELECT col, COUNT(col) AS dup_count FROM table GROUP BY col HAVING (COUNT(col) > 1)  

Used www.mximize.com/how-to-find-duplicate-values-in-a-table- as a base in 2009; website content has now gone (2014).

like image 70
Mead Avatar answered Sep 22 '22 22:09

Mead