Saturday, January 16, 2010

How to find Duplicate Records from a SQL Table

In this post i'll show you how can we retrieve Duplicate Records from a SQL Table.

The syntax is
Select Column_Name,COUNT(*) from Table_Name GROUP BY Column_Name HAVING COUNT(*)>1 ORDER BY COUNT(*) DESC

WHERE Column_Name is the name of the column in which we are finding the duplicate records.

I am taking Titles table in pubs database as an example.

there are many titles which are published by different different Publishers. So, Suppose we want to find out how many titles have been published by a particular Publishers. To retrieve the desired information we have to write the query below.

select pub_id,COUNT(*) from titles group by pub_id having COUNT(*)>1 order by COUNT(*) desc


this will yield the result as:




Hope this will help you.

No comments:

Post a Comment