4

I want a query to count the duplicate values in a column, e.g.,

total records=SELECT COUNT(column_name) FROM table_name;
distinct records=SELECT COUNT(DISTINCT column_name) FROM table_name;
duplicate count=total records-distinct records;
1
  • 2
    Choose one database please don't tag all the available database Commented Jan 19, 2015 at 5:25

5 Answers 5

6

A simplification of the SQL you provided:

    SELECT Count(Column) - Count(DISTINCT Column)
    FROM   yourTable
Sign up to request clarification or add additional context in comments.

3 Comments

this works perfectly... and i have one more doubt,is distinct count and unique count were same or there is any difference.
Lot of examples on the web go into unnecessary junction. This one is the shortest an most efficient.
3
SELECT COUNT(column_name) as count 
   FROM table_name 
  GROUP BY column_name 
 HAVING count > 1 

Comments

2
select column_name, count(*) 
from table_name 
group by column_name 
having count(*) > 1

Comments

1
SELECT (SELECT COUNT(col) FROM tbl) - (SELECT COUNT(DISTINCT col) FROM tbl);

EDIT: Good point by NoDisplayName. This works in MySQL at least, I don't guarantee cross-engine compatibility (I last worked on Oracle fifteen years ago, and on SQL Server never)

Comments

0

A subquery can be used:

select sum(duplicates) from
(select column_name, num_duplicates-1 as duplicates from
(select column_name,count(column_name) as num_duplicates from table_name
group by column_name
having count(column_name)>1) as table_alias1) as table_alias2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.