MS SQL Server Concepts and Programming Question:

How To Count Duplicated Values in a Column in MS SQL Server?

Tweet Share WhatsApp

Answer:

If you have a column with duplicated values, and you want to know what are those duplicated values are and how many duplicates are there for each of those values, you can use the "GROUP BY ... HAVING" clause as shown in the following example. It returns how many duplicated first names in the ggl_team table:

SELECT first_name, COUNT(*) FROM ggl_team
GROUP BY first_name HAVING COUNT(*) > 1
GO<pre>
first_name count(*)
John 5</pre>
So we have "John" as duplicated first name 5 times in the ggl_team table.

Download MS SQL Server PDF Read All 394 MS SQL Server Questions
Previous QuestionNext Question
How To Apply Filtering Criteria at Group Level with The HAVING Clause in MS SQL Server?Can Multiple Columns Be Used in SQL GROUP BY Clause in MS SQL Server?