How to use
the GROUP BY clause
or ROW_NUMBER() function
to find duplicate values in SQL Server.
The query for finding the duplicate values in multiple columns using the GROUP BY clause :
SELECT col1,col2,...
COUNT(*)
FROM table_name
GROUP BY col1,col2,...
HAVING COUNT(*) > 1;
Using ROW_NUMBER() function to find
duplicates in a table
Here is the
result:
How it works:
First, the ROW_NUMBER() distributes rows of the t1 table into partitions by
values in the a and b columns. The duplicate rows will
have repeated values in the a and b columns, but different row numbers as shown in the
following picture:
Second, the outer query removes the
first row in each group.
Generally, This statement uses the ROW_NUMBER() function to find the duplicate
values in one column of a table: