SQL COUNT_BIG function

The SQL COUNT_BIG Function is used to Count the number of items/rows selected by the SELECT Statement. It works the same as the SQL Count function, but it returns the bigint in SQL Server. For this COUNT_BIG function, We are going to use the below-shown data

SQL COUNT_BIG Function 1

SQL COUNT_BIG (*) Example

The SQL Server COUNT_BIG (*) returns the total number of records from the MyEmployees table. Let us see one example for better understanding

-- SQL Server COUNT_BIG Function Example
USE [SQL Tutorial]
GO
SELECT COUNT_BIG(*) AS [Number of Employees]
FROM [MyEmployees]

Above SQL Query will count the total records present in the MyEmployees table

SQL COUNT_BIG Function 2

COUNT_BIG (Column Name) Example

The SQL COUNT_BIG Column Name returns the records whose values are NOT NULL (Ignores the NULL Records). Let us see one Aggregate Function example

-- SQL Server COUNT_BIG Function Example
USE [SQL Tutorial]
GO
SELECT COUNT_BIG([EmployeeID]) AS [Number of Employees]
      ,COUNT_BIG([ManagerID]) AS [Number of Managers]	
FROM [MyEmployees]
SQL COUNT_BIG Function 3

SQL Unique Count BIG Example

The SQL COUNT_BIG (DISTINCT Column Name) returns the Unique number of records present in the specified column whose values are NOT NULL. The DISTINCT Keyword is used to remove the Duplicates in SQL Server.

-- SQL Server COUNT_BIG Function Example
USE [SQL Tutorial]
GO
SELECT COUNT_BIG(DISTINCT [EmployeeID]) AS [Number of Unique Employees]
      ,COUNT_BIG(DISTINCT [ManagerID]) AS [Number of Unique Managers]	
FROM [MyEmployees]
SQL COUNT_BIG Function 4

COUNT BIG with GROUP BY Clause

In general, we write a Select query to check for the number of products that belongs to a particular category or color, etc. In these situations, we use the GROUP BY clause to group the products by color or category. We then use the COUNT_BIG Function to Count the number of products present in that group. Let us see the Example

-- SQL Server COUNT_BIG Function Example
USE [SQL Tutorial]
GO
SELECT COUNT_BIG( [EmployeeID]) AS [Number of Employees]
	  ,[Education]
      ,SUM([YearlyIncome]) AS [Total Income]
      ,SUM([Sales]) AS [Total Sale]
 FROM [MyEmployees]
 GROUP BY [Education]
 ORDER BY [Total Income] DESC
SQL COUNT_BIG Function 5