T-SQL Tutorial

Find duplicates rows T-SQL


Select statement

To find duplicates rows in a table you need to use a Select statement that contains group by with having keyword. Another option is to use the ranking function Row_Number().

Find duplicates rows - Group By

USE model;
GO
SELECT Name, ID, COUNT(*) CN
FROM Students_Math
GROUP BY name, id
HAVING COUNT(*) > 1
ORDER By Name ;
GO

Find duplicates rows - Row_Number()

USE model;
GO
SELECT * FROM (
SELECT Name, ID,
Row_Number() OVER(PARTITION BY Name, ID ORDER By Name) as CN
FROM Students_Math
) AS Q WHERE Q.CN > 1
GO