Search This Blog

Thursday, January 20, 2011

Finding duplicates

A question I get often by my C# developers is what is the TSQL to find repeated entries in table x? The one solution I return to over and over is :

SELECT 
       FirstName
       ,COUNT(*)
from Person.Contact
GROUP BY FirstName HAVING COUNT(*) > 1

Using the AdventureWorks database  as the source to execute the query we can quickly determine how many times a person has the same first name. This was a simple example but can be applied to many examples.