SQL COUNT Function

The SQL COUNT Function is one of the Aggregate Function which Counts the total number of rows (records) selected by the SELECT Statement

For example, If you require to get the total number of products in your Store. Or you want to locate the Black color products present in your Store. In these situations, you can use this SQL Server COUNT function. The basic syntax of the Count in SQL Server is as shown below:

SELECT COUNT([Column_Name])
FROM [Source]

For this SQL Server Count demonstration, we are going to use the below-shown data

SQL Count Function 1

SQL COUNT(*) Example

The SQL Server COUNT(*) returns the total records from the employee’s table. Let us see an example of Count * to understand this Aggregate Function better

-- SQL Server Count Example
USE [SQLTEST]
GO
SELECT COUNT(*) AS [Total Records]
  FROM [Employees]

The above Select Statement query counts the total records present in the Employee table

SQL Count Function 2

NOTE: The COUNT(*) counts NULL values and Duplicate records, as well.

COUNT (Column Name) Example

The COUNT(Column Name) returns the total number of employees present in the table whose values are NOT NULL (Ignores the NULL Records). Let us see one example of Count function

NOTE: The COUNT(Column Name) counts the Duplicate records.

-- SQL Server Count Example
SELECT COUNT([FirstName]) AS [Total Records]
  FROM [Employees]
SQL Count Function 3

We got the same result as COUNT(*) query. Because there are no NULL Values in the [FirstName] Column. Let us try another Column, i.e., DeptID

-- SQL Server Count Example
SELECT COUNT(DeptID) AS [Total Records]
  FROM [Employees]

Above Select Count Query counts the total records in the [DepartID] whose values are NOT NULL

SQL Count Function 4

SQL Count Distinct Values Example

The SQL Server COUNT DISTINCT Column_Name returns the Unique number of rows present in the table whose values are NOT NULL (Ignores the NULL Records).

TIP: The DISTINCT Keyword is used to remove the Duplicates from the specified column Name.

-- SQL Server Count Example
SELECT COUNT(DeptID) AS [Total Records],
 COUNT(DISTINCT DeptID) AS [Distinct Records]
  FROM [Employees]

Above SQL Query will count the Unique number of records (by removing duplicates) present in the [DepartID] whose values are NOT NULL

SQL Count Function 5

Count Distinct Values Example 2

This SQL count Distinct example shows the difference between the Normal Count and the Count Distinct. For better understand, we selected multiple columns with Count and Count Distinct separately

-- SQL Server Count Example
SELECT COUNT(Education) AS [Total Education],
 COUNT(DISTINCT Education) AS [Distinct Education],
 COUNT(FirstName) AS [Total FirstName],
 COUNT(DISTINCT FirstName) AS [Distinct FirstName],
 COUNT(DeptID) AS [Total Ids],
 COUNT(DISTINCT DeptID) AS [Distinct Ids]
  FROM [Employees]
SQL Count Function 6

SQL Count All Example

The COUNT ALL Column_Name is the same as COUNT Column_Name. The following SQL Server example shows the same.

-- SQL Server Count Example
SELECT COUNT(ALL Education) AS [Total Education],
 COUNT(DISTINCT Education) AS [Distinct Education],
 COUNT(ALL FirstName) AS [Total FirstName],
 COUNT(DISTINCT FirstName) AS [Distinct FirstName],
 COUNT(ALL DeptID) AS [Total Ids],
 COUNT(DISTINCT DeptID) AS [Distinct Ids]
  FROM [Employees]
SQL Count Function 7

Count in Where Clause Example

We can use Count function along with Where Clause. In this example, we are using where clause to count the total employees whose yearly income is greater than or equal to 60000

-- SQL Server Count Example
SELECT COUNT(*) AS [Total Records]
  FROM [Employees]
  WHERE YearlyIncome >= 60000
SQL Count Function 10

SQL Count IS NULL Example

In this example, we used IS NULL to count the total number of customers whose Department ID is Null

-- SQL Server Count Example
SELECT COUNT(*) AS [Total Records]
  FROM [Employees]
  WHERE DeptID IS NULL
SQL Count Function 8

SQL Count IS NOT NULL Example

In this example, we used IS NOT NULL to count the total customers whose Department ID is not Null

-- SQL Server Count Example
SELECT COUNT(*) AS [Total Records]
  FROM [Employees]
  WHERE DeptID IS NOT NULL
SQL Count Function 9

SQL COUNT GROUP BY Clause Example

In most cases, we usually count the number of customers who belongs to a particular region, etc. In these situations, we use the GROUP BY clause to group the customers by region, profession, education, state, etc. Next, we use the COUNT function to count the number of customers present in that group. Let us see an example of SQL Count group by

-- SQL Server Count Function Example
SELECT Education,
 COUNT(*) AS [Total Records]
  FROM [Employees]
  GROUP BY Education

Above SQL query finds the total number of Employees belongs to each education group.

SQL Count Function 11

Count Group By Clause Example 2

It is another example to showcase the Group By clause, along with the Count Function. In this example, we are using Count, Count Distinct functions on multiple columns along with the group by clause.

-- SQL Server Count Function Example
SELECT Education,
 COUNT(FirstName) AS [Total FirstName],
 COUNT(DISTINCT FirstName) AS [Distinct Name],
 COUNT(EmpID) AS [Total Ids],
 COUNT(DeptID) AS [Total DeptIds]
  FROM [Employees]
  GROUP BY Education
SQL Count Function 12

SQL COUNT HAVING Clause

If you want to check the conditions against the aggregated data, then you have to use the HAVING Clause along with Group By Statement. For example, the following query will group the Customers by their Education and counts the records present in each group.

-- SQL Server Count Function Example
SELECT Education,
 COUNT(FirstName) AS [Total FirstName],
 COUNT(DISTINCT FirstName) AS [Distinct Name],
 COUNT(EmpID) AS [Total Ids],
 COUNT(DeptID) AS [Total DeptIds]
  FROM [Employees]
  GROUP BY Education
  HAVING COUNT(EmpID) > 1

Below lines of code will check whether the total count of Employee Ids is greater than one or not. If this is True, then corresponding records will display.

HAVING COUNT(EmpID) > 1
SQL Count Function 13

Count in Order By Clause

You can use the SQL Server Count function in Order By Clause. Below query count the records group by the Education. Next, Order By Clause will sort those query result based on the Distinct Count of a first name result in descending order.

-- SQL Server Count Function Example
SELECT Education,
 COUNT(FirstName) AS [Total FirstName],
 COUNT(DISTINCT FirstName) AS [Distinct Name],
 COUNT(DeptID) AS [Total DeptIds]
  FROM [Employees]
  GROUP BY Education
  ORDER BY COUNT(DISTINCT FirstName) DESC
SQL Count Function 14

SQL Server Count Joins Example

You can use Count function along with the Joins. In this example, we are using Inner Join to join Employee and Departement table. Next, we are counting the total records grouped by Department name.

-- SQL Server Count Function Example
SELECT E.DeptID,
 dep.DepartmentName,
 COUNT(*) AS [Total Records]
  FROM [Employees] E
  INNER JOIN Department dep ON
  E.DeptID = dep.DeptID
  GROUP BY E.DeptID, dep.DepartmentName
SQL Count Function 15

Use Joins with Count Function Example 2

It is another example to demonstrate the Joins along with count function.

SELECT E.Education,
 dep.DepartmentName,
 COUNT(*) AS [Total Records]
  FROM [Employees] E
  INNER JOIN Department dep ON
  E.DeptID = dep.DeptID
  GROUP BY E.Education, dep.DepartmentName
  ORDER BY Education
SQL Count Function 16

COUNT Multiple tables Example

You can use SQL Count function on Multiple Tables. The below query will count all the records from the Employee table and Department table.

-- SQL Server Count Function Example
USE [SQLTEST]
GO
SELECT 
 (SELECT COUNT(*) 
 FROM [Employees]
 ) AS [Total Employees],
 (SELECT COUNT(*) 
 FROM [Department]
 ) AS [Total Departments]
SQL Count Function 17