Perfect Pages Canada
ASP.NET Cafe
new tricks every week

SQL: Finding duplicates in table

Saturday, 14 November 2009 06:55 by dmitriy

Looking for rows with some duplicate values in some column(s) is very common. I'm not saying that's something hard.  I was asked about this. Here is the solution.

 

   1:  SELECT [Name], COUNT([Name]) as NumOccurrences 
   2:  FROM [People]
   3:  GROUP BY [Name]
   4:  HAVING ( COUNT( [Name] ) > 1 )
 

And the query to get all PKs ( primary keys ) for rows with duplicate [Name]:

   1:  SELECT [uid] 
   2:  FROM [People] 
   3:  WHERE [Name] IN (
   4:  SELECT [Name]
   5:  FROM [People]
   6:  GROUP BY [Name]
   7:  HAVING ( COUNT([Name]) > 1 ))

That's all.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   SQL
Actions: E-mail | del.icio.us | Kick it! | DZone it! | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


 

  Country flag

biuquote
  • Comment
  • Preview
Loading