COUNT() Function in SQL Server
Last Updated :
13 Sep, 2024
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
- This function finds the number of indexes as returned from the query selected.
- This function comes under Numeric Functions.
- This function accepts only one parameter namely expression.
- 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:
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:
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:
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.