Open In App

COUNT() Function in SQL Server

Last Updated : 13 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The COUNT() function in SQL Server is a fundamental aggregate function used to determine the number of rows that match a specific condition. Counting rows provides valuable insights into data sets such as the total number of records, distinct values, or records meeting certain criteria.

In this article, We will learn the COUNT() Function in SQL Server by understanding various examples in detail.

What is the SQL Server COUNT() function

  • The COUNT() function in SQL Server is an aggregate function used to return the number of rows that match a specified condition.
  • It counts the number of items in a set, which can be all rows or rows that meet a certain criterion, depending on the query.

Syntax of the COUNT() function:

COUNT(expression)

expression: The column or expression for which the count is calculated. It can be a column name, a constant, or an asterisk (*).

Features OF COUNT() Function in SQL Server

  1. This function finds the number of indexes as returned from the query selected.
  2. This function comes under Numeric Functions.
  3. This function accepts only one parameter namely expression.
  4. This function ignores NULL values and doesn’t count them.

Examples of COUNT() Function in SQL Server

Consider the following example table named Employees:

EmployeeID Name Department Status
1 Alice HR Active
2 Bob IT Inactive
3 Charlie HR Active
4 David IT Active
5 Eve Finance Active

Let’s explore various ways to use the COUNT() function with this table.

Example 1: Count All Rows in the Employees Table

SELECT COUNT(*) AS TotalRows
FROM Employees;

Output:

TotalRows
5

Explanation: This query returns the total number of rows in the Employees table, which is 5.

Example 2: Count Distinct Values in the Department Column

SELECT COUNT(DISTINCT Department) AS UniqueDepartments
FROM Employees;

Output:

UniqueDepartments
3

Explanation: This query counts the number of unique departments in the Employees table, which are HR, IT, and Finance.

Example 3: Count Rows where the Status is ‘Active’

SELECT COUNT(*) AS ActiveEmployees
FROM Employees
WHERE Status = 'Active';

Output:

ActiveEmployees
4

Explanation: This query returns the number of employees with the status ‘Active’, which is 4.

Example 4: Count Employees in Each Department

SELECT Department, COUNT(*) AS NumberOfEmployees
FROM Employees
GROUP BY Department;

Output:

Department NumberOfEmployees
HR 2
IT 2
Finance 1

Explanation: This query counts the number of employees in each department, providing a breakdown by department.

Example 5: Using COUNT() with SUM() and AVG()

Assuming the Employees table has a Salary column:

SELECT Department, COUNT(*) AS TotalEmployees, SUM(Salary) AS TotalSalary, AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department;

Output:

Department TotalEmployees TotalSalary AverageSalary
HR 2 120000 60000
IT 2 150000 75000
Finance 1 80000 80000

Explanation: This query counts the number of employees, calculates the total salary, and finds the average salary for each department.

Conclusion

The COUNT() function is a powerful tool for summarizing data by counting rows based on given conditions or criteria. Whether you’re calculating the total number of records, counting unique values, or aggregating data across groups, COUNT() provides crucial metrics that aid in data analysis.



Next Article
Article Tags :

Similar Reads