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 COUNT function. The basic syntax of the SQL Server Count is as shown below:

SELECT COUNT([Column_Name])
FROM [Source]

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

Employee table for Aggregate Function Example 1

SQL COUNT (*) Example

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

SELECT COUNT(*) AS [Total Records]
  FROM [Employees]

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

SQL Count * Example 2

NOTE: The COUNT(*) add up NULL values and Duplicate records, as well. And the (Column Name) considers and add the Duplicate records.

COUNT (Column Name) Example

It 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 this function

SELECT COUNT([FirstName]) AS [Total Records]
  FROM [Employees]
SQL Count Column Name 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

SELECT COUNT(DeptID) AS [Total Records]
  FROM [Employees]

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

Returns Not Null Values 4

SQL Count Distinct Values Example

The 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.

SELECT COUNT(DeptID) AS [Total Records],
 COUNT(DISTINCT DeptID) AS [Distinct Records]
  FROM [Employees]

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

Return Total Number of Distinct records in a Customer Table 5

Distinct Values Example 2

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

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]
Total Distinct Employees 6

SQL Count All Example

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

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

SQL Count Where Clause Example

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

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 find the total number of customers whose Department ID is Null

SELECT COUNT(*) AS [Total Records]
  FROM [Employees]
  WHERE DeptID IS NULL
IS NULL example 8

SQL Count IS NOT NULL Example

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

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 find 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 this function to find the number of customers present in that group. Let us see an example of SQL Count group by

SELECT Education,
 COUNT(*) AS [Total Records]
  FROM [Employees]
  GROUP BY Education

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

SQL Count Function 11

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

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 add up the records present in each group.

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

The last line of code will check whether the total number of Employee Ids is greater than one or not. If this is True, then corresponding records will display.

SQL Count Function 13

SQL Count Order By Clause

You can use this in Order By Clause. Below query add up the records group by the Education. Next, Order By Clause will sort those query result based on the Distinct records of a first name result in descending order.

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 Count Joins Example

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

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

It is another example to demonstrate the Joins along with this 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

Multiple tables Example

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

-- Multiple Tables Example

SELECT 
 (SELECT COUNT(*) 
 FROM [Employees]
 ) AS [Total Employees],
 (SELECT COUNT(*) 
 FROM [Department]
 ) AS [Total Departments]
SQL Count Function 17