SQL SERVER – Count Duplicate Records – Rows

In my previous article SQL SERVER – Delete Duplicate Records – Rows, we have seen how we can delete all the duplicate records in one simple query. In this article we will see how to find count of all the duplicate records in the table. Following query demonstrates usage of GROUP BY, HAVING, ORDER BY in one query and returns the results with duplicate column and its count in descending order.

SELECT YourColumn, COUNT(*) TotalCount
FROM YourTable
GROUP BY YourColumn
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC

Watch the view to see the above concept in action:

[youtube=http://www.youtube.com/watch?v=ioDJ0xVOHDY]

Reference : Pinal Dave (https://blog.sqlauthority.com) , SQL SERVER – Delete Duplicate Records – Rows

Duplicate Records, SQL Scripts
Previous Post
SQL SERVER – 2005 – List All Stored Procedure Modified in Last N Days
Next Post
SQL SERVER – 2008 – Scheduled Launch at an Event in Los Angeles on Feb. 27, 2008

Related Posts

187 Comments. Leave new

  • Another solution is to put the table with duplicates in join with itself.
    For a simple example see:

    Reply
  • thx pinal…. it was very easy …

    Reply
  • Pinal,

    The query posted at the top of this page results with duplicate column and its count in descending order.
    How can i find the count the returned rows???

    Please help me.

    Reply
    • Try this

      SELECT COUNT(*) as total_duplicates FROM
      (
      SELECT YourColumn, COUNT(*) TotalCount
      FROM YourTable
      GROUP BY YourColumn
      HAVING COUNT(*) > 1
      ) as t

      Reply
  • Hi Pinal,
    I have a question.
    I need to loop through a table and if there are some duplicate values in one of the columns I need to add a suffix of a, b, c, and so on. But, there are different sets of duplicates. So, I need to do the suffix on each separate set of duplicates.
    Please notice bellow, there are three sets of values that are duplicates. This is a sample of my data.

    Could you give me any ideas on how to accomplish this task? Thank you.
    ZP19188AMR
    ZP19188AVE

    ZP19188AVH
    ZP19188AVH

    ZP19188COC
    ZP19188COC
    ZP19188COC
    ZP19188COC
    ZP19188COC
    ZP19188COC

    ZP19188PHM
    ZP19188PHM

    ZP19188RIM
    ZP19188SAB
    ZP19188WEH

    Reply
  • thanks for the post it really helped me.

    Reply
  • Thanks. This is very helpful.

    Reply
  • I need to limit the number of duplicates. Can this be achieved? Thanks.

    Reply
  • Thanks.

    Reply
  • HI Pinal,

    If I want to select all the fields in a table,
    How shold I write the query for that

    thank u

    Reply
    • select t1.* from table as t1 inner join
      (
      select col from table group by col having count(col)>1
      ) as t2
      on t1.col=t2.col

      Reply
  • thanks.

    Reply
  • select Taluk_Name_Eng ,count(Taluk_Name_Eng)as kk from tbl_PL04_Taluk

    what problem in this query

    Reply
    • You need to include that column in the group by clause

      select Taluk_Name_Eng ,count(Taluk_Name_Eng)as kk from tbl_PL04_Taluk group by Taluk_Name_Eng

      Reply
  • Using the above Query is nearly just what I need but it only gives me an ID (YourColumn). I have another table which would have a named value for this ID. How can I return that instead? A Join or a nested statement?

    Many thanks

    Reply
  • Did it, quite new to SQL I’m afriad, hope it helps another novice :)

    SELECT top 20 computers.name as CID, COUNT(*) as ErrorCount
    FROM errors
    inner join computers
    on computers.id = errors.computerid
    GROUP BY computers.name
    HAVING COUNT(*) > 1
    ORDER BY COUNT(*) DESC

    Reply
  • Hi,

    I have written to get Duplicate Rows from the table. You can use the ROW_Number() in SQL 2005.

    To read more about it:

    Tejas

    Reply
  • u r good

    Reply
  • MarioSanath(SriLankan)
    February 4, 2009 5:50 pm

    SELECT YourColumn, COUNT(*) TotalCount
    FROM YourTable
    GROUP BY YourColumn
    HAVING COUNT(*) > 1
    ORDER BY COUNT(*) DESC

    this query is solved my lot of problems
    Thank you very much

    Reply
  • If you’d like to get a total count of the duplicates as I needed to do, try returning the above query as a derived table and then do a sum on the count.

    SELECT SUM(Duplicates.TotalCount) FROM (SELECT YourColumn, COUNT(*) TotalCount
    FROM YourTable
    GROUP BY YourColumn
    HAVING COUNT(*) > 1) AS Duplicates

    My database has over 1200 duplicates. :(

    Reply
  • Hey Pinal,

    Excellent query ! thanks !

    Reply
  • its really good update its help ful for every one thanks

    Reply
  • i have a data entry system for patient records. in my system there are several forms through which data is entered in different tables. and there is a form which takes the present condition of the patient (i call it PresentAssessment). now i what i want is when the user enters a new record i want to compare the values of the attributes (database table fields) of the newly entered patient and from all the matching records i want to calculate and return the chance(in terms of %age) of particular disease(s) (stored in PresentAssessment) to newly added patient. e.g. if i have two tables i.e. PatientDrugHistory and PresentAssessment. and say each of them already have 10 records with 10 columns each. now when entering the 11th record in PatientDrugHistory i want to calculte and store how much chances (in terms of %age) exist that this 11th patient is suffereing from disease1 (a column in PresentAssessment). please if anybody can tell me what query to write for this purpose

    Reply

Leave a Reply