Using GROUP BY clause to find duplicates in a table
This statement uses the GROUP BY clause to find the duplicate rows in both a and b
columns of the t1 table:
SELECT
a,
b,
COUNT(*) occurrences
FROM t1
GROUP BY
a,
b
HAVING
COUNT(*) > 1;
Code language: SQL (Structured Query Language) (sql)
Here is the result:
SQL Server Find Duplicates using GROUP BY clause
How it works:
First, the GROUP BY clause groups the rows into groups by values in both a and b
columns.
Second, the COUNT() function returns the number of occurrences of each group (a,b).
Third, the HAVING clause keeps only duplicate groups, which are groups that have
more than one occurrence.