Interviews2 answers
How we can count duplicate entry in particular table against Primary Key ? What are constraints?

Sapna Malik
Posted Aug 27, 2009, 8:35 PM EDT2 Answers
SELECT YourColumn, COUNT(*) TotalCount
FROM YourTable
GROUP BY YourColumn
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
Devinder KumarPosted Jul 10, 2014, 9:54 PM EDT
The syntax in the previous answer (where count(*) > 1) is very questionable. suppose you think that you have duplicate employee numbers. There's no need to count them to find out which values were duplicate but the followin SQL will show only the empnos that are duplicate and how many exist in the table:
Select empno, count(*)
from employee
group by empno
having count(*) > 1
Generally speaking aggregate functions (count, sum, avg etc.) go in the HAVING clause. I know some systems allow them in the WHERE clause but you must be very careful in interpreting the result.WHERE COUNT(*) > 1 will absolutely NOT work in DB2 or
ORACLE. Sybase and SQLServer is a different animal.
Select empno, count(*)
from employee
group by empno
having count(*) > 1
Generally speaking aggregate functions (count, sum, avg etc.) go in the HAVING clause. I know some systems allow them in the WHERE clause but you must be very careful in interpreting the result.WHERE COUNT(*) > 1 will absolutely NOT work in DB2 or
ORACLE. Sybase and SQLServer is a different animal.
Partap TanwarPosted Sep 7, 2009, 12:15 AM EDT
