ARTICLE

Count(All) vs Count(Distinct) in SQL Server 2012

Posted by Rohatash Kumar Articles | SQL Server 2012 November 30, 2012
In this article, you will see the SQL aggregate function count in two forms, Count(All) and Count(Distinct).
Reader Level:

In this article, you will see the SQL aggregate function Count in two forms, Count(All) and Count(Distinct) in SQL Server. The Count function is a part of the aggregate functions. In aggregates we consider various types of functions like count, max, avg, min, and sum. In this article you consider the Count function which is used to count the number of rows in a database table. So let's have a look at a practical example of how to get the difference between the Count(All) and Count(Distinct) in SQL Server. The example is developed in SQL Server 2012 using the SQL Server Management Studio.

Count(ColumnName) Function

The COUNT() function is used to count the number of rows when you use a where condition with the Count function in a select statement. It will return the number of rows from the table satistying the where condition. NULL values will not be counted by the Count function.

Example

SELECT  [EmpID]

      ,[EmpName]

      ,[EmpSalary]

  FROM [master].[dbo].[Employee]

  Go

  select Count(empname) as CountResult from [Employee]

Output

Count-function-in SQL-Server.jpg

Count(All ColumnName) Function

The COUNT(All ColumnName) function is similar to Count(ColumnName). It also produces the same result as Count(ColumnName).

Example

SELECT  [EmpID]

      ,[EmpName]

      ,[EmpSalary]

  FROM [master].[dbo].[Employee]

  Go

  select Count(All empname) as CountResult from [Employee]

 

Output

Count-all-function-in SQL-Server.jpg

Count(Distinct ColumnName) Function

When the DISTINCT keyword is used, all duplicate values are eliminated before the function count is applied, or if you want to count the number of unique rows in the table. This form of Count does not count rows with null values for the column.

Example

SELECT  [EmpID]

      ,[EmpName]

      ,[EmpSalary]

  FROM [master].[dbo].[Employee]

  Go

  select Count(Distinct empname) as CountResult from [Employee]

Output

Count-distinct-function-in SQL-Server.jpg

Login to add your contents and source code to this article
post comment
     

If you have millions of rows in a table, COUNT() may throw an error message so you can use COUNT_BIG function instead to avoid this data type error. For return values greater than 2^31-1, COUNT produces an error. Use COUNT_BIG instead. http://msdn.microsoft.com/en-us/library/ms175997.aspx

Posted by Gohil Jayendrasinh Feb 18, 2013
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts