Paramclasses Academy - Blog

Tutorials Details

←
Post 19 of 40
→

SQL Server Aggregate Functions Explained: COUNT, SUM, AVG, MIN and MAX with Examples

SQL Server Aggregate Functions Explained: COUNT, SUM, AVG, MIN and MAX with Examples

November 17, 2026

SQL Server Aggregate Functions Explained: COUNT, SUM, AVG, MIN and MAX with Practical Examples

Introduction

When working with databases, developers often need to calculate useful information from multiple records. For example, a company may want to count its total employees, calculate the total salary paid, find the average salary, or identify the highest and lowest salary.

Microsoft SQL Server provides Aggregate Functions to perform these calculations directly through SQL queries.

Aggregate functions perform calculations on a set of rows and return a single result for the entire set or for each group of records.

In this tutorial by Param Classes, you will learn five important SQL Server aggregate functions:

  • COUNT() – Counts records.

  • SUM() – Calculates the total of numeric values.

  • AVG() – Calculates the average of numeric values.

  • MIN() – Returns the minimum value.

  • MAX() – Returns the maximum value.

We will understand their syntax, create a sample Employees table, execute practical SQL queries, and explore how aggregate functions work with GROUP BY and HAVING.

1. What Are Aggregate Functions in SQL Server?

Aggregate Functions are built-in SQL Server functions that perform calculations on multiple rows and return a single result.

They are commonly used for:

  • Calculating the total number of records.

  • Finding the sum of numeric values.

  • Calculating average values.

  • Finding the minimum value.

  • Finding the maximum value.

  • Preparing reports and analysing business data.

Common Aggregate Functions

Function Purpose Example
COUNT() Counts records or non-NULL values COUNT(*)
SUM() Calculates the total SUM(Salary)
AVG() Calculates the average AVG(Salary)
MIN() Finds the smallest value MIN(Salary)
MAX() Finds the largest value MAX(Salary)

Important: Aggregate functions are especially useful when working with employee records, student databases, sales reports, inventory systems, and financial applications.

2. Create a Sample Employees Table

Before understanding each aggregate function, let's create a sample table.

We will use an Employees table containing employee IDs, employee names, departments, and salaries.

Step 1: Create the Employees Table

CREATE TABLE Employees
(
    EmployeeID INT PRIMARY KEY,
    EmployeeName VARCHAR(50),
    Department VARCHAR(50),
    Salary DECIMAL(10,2)
);

Step 2: Insert Sample Records

INSERT INTO Employees
(EmployeeID, EmployeeName, Department, Salary)
VALUES
(1, 'Rahul', 'IT', 40000),
(2, 'Priya', 'HR', 35000),
(3, 'Amit', 'IT', 45000),
(4, 'Neha', 'HR', 30000),
(5, 'Rohit', 'Sales', 38000),
(6, 'Anjali', 'IT', 50000),
(7, 'Karan', 'Sales', 42000),
(8, 'Pooja', 'Finance', 36000);

Step 3: Display All Records

SELECT *
FROM Employees;

Expected Output

EmployeeID EmployeeName Department Salary
1 Rahul IT 40000
2 Priya HR 35000
3 Amit IT 45000
4 Neha HR 30000
5 Rohit Sales 38000
6 Anjali IT 50000
7 Karan Sales 42000
8 Pooja Finance 36000

Now let's explore each aggregate function one by one.

3. COUNT() Function in SQL Server

The COUNT() function is used to count records or non-NULL values in a table.

It is useful when you want to find:

  • Total employees in a company.

  • Total students in a college.

  • Total products in an inventory.

  • Total orders in a sales database.

Syntax

SELECT COUNT(expression)
FROM table_name;

Example 1: Count All Employees

SELECT COUNT(*) AS TotalEmployees
FROM Employees;

Expected Output

TotalEmployees
8

Explanation:

  • COUNT(*) counts all rows returned by the query.

  • FROM Employees specifies the table.

  • AS TotalEmployees gives the result column a meaningful name.

Result: There are 8 employee records in the Employees table.

Example 2: Count Employee IDs

SELECT COUNT(EmployeeID) AS TotalEmployeeIDs
FROM Employees;

This query counts non-NULL values in the EmployeeID column.

Example 3: Count Distinct Departments

SELECT COUNT(DISTINCT Department) AS TotalDepartments
FROM Employees;

Expected Output

TotalDepartments
4

Explanation: The Employees table contains four distinct departments: IT, HR, Sales, and Finance.

Difference Between COUNT(*), COUNT(Column), and COUNT(DISTINCT Column)

Expression What It Counts
COUNT(*) All rows
COUNT(EmployeeID) Non-NULL EmployeeID values
COUNT(DISTINCT Department) Distinct non-NULL department values

Important: COUNT(column_name) ignores NULL values, whereas COUNT(*) counts rows even when some column values are NULL.

4. SUM() Function in SQL Server

The SUM() function calculates the total of numeric values in a column.

It is commonly used in:

  • Salary calculations.

  • Total sales reports.

  • Product stock calculations.

  • Monthly revenue reports.

  • Financial applications.

Syntax

SELECT SUM(column_name)
FROM table_name;

Example 1: Calculate Total Employee Salary

SELECT SUM(Salary) AS TotalSalary
FROM Employees;

Expected Output

TotalSalary
316000

Explanation:

The SUM() function adds all salary values from the Employees table.

Calculation:

40000 + 35000 + 45000 + 30000
+ 38000 + 50000 + 42000 + 36000
= 316000

Result: The total salary of all employees is 316000.

Example 2: Calculate Total Salary of IT Department

SELECT SUM(Salary) AS ITTotalSalary
FROM Employees
WHERE Department = 'IT';

Expected Output

ITTotalSalary
135000

Explanation: The WHERE clause selects only IT department employees, and SUM() calculates their combined salary.

5. AVG() Function in SQL Server

The AVG() function calculates the average of numeric values in a column.

It is useful for calculating:

  • Average employee salary.

  • Average student marks.

  • Average product price.

  • Average monthly sales.

  • Average order value.

Syntax

SELECT AVG(column_name)
FROM table_name;

Example 1: Calculate Average Employee Salary

SELECT AVG(Salary) AS AverageSalary
FROM Employees;

Expected Output

AverageSalary
39500.00

Explanation:

AVG() adds the non-NULL salary values and divides their total by the number of non-NULL values.

Calculation:

Total Salary = 316000
Number of Employees = 8

Average Salary = 316000 / 8

Average Salary = 39500

Example 2: Calculate Average Salary of IT Department

SELECT AVG(Salary) AS ITAverageSalary
FROM Employees
WHERE Department = 'IT';

Expected Output

ITAverageSalary
45000.00

Explanation: SQL Server calculates the average salary using only employees whose department is IT.

Important: AVG() ignores NULL values. If there are no non-NULL values to average, the result is NULL.

6. MIN() Function in SQL Server

The MIN() function returns the smallest value from a specified column.

It is commonly used to find:

  • Lowest employee salary.

  • Minimum product price.

  • Lowest examination marks.

  • Earliest date.

  • Minimum stock quantity.

Syntax

SELECT MIN(column_name)
FROM table_name;

Example 1: Find the Lowest Employee Salary

SELECT MIN(Salary) AS MinimumSalary
FROM Employees;

Expected Output

MinimumSalary
30000

Explanation: MIN() checks the salary values and returns the smallest value.

Example 2: Find the Lowest Salary in the IT Department

SELECT MIN(Salary) AS ITMinimumSalary
FROM Employees
WHERE Department = 'IT';

Expected Output

ITMinimumSalary
40000

Result: The lowest salary in the IT department is 40000.

7. MAX() Function in SQL Server

The MAX() function returns the largest value from a specified column.

It is useful for finding:

  • Highest employee salary.

  • Maximum product price.

  • Highest examination marks.

  • Latest order date.

  • Maximum stock quantity.

Syntax

SELECT MAX(column_name)
FROM table_name;

Example 1: Find the Highest Employee Salary

SELECT MAX(Salary) AS MaximumSalary
FROM Employees;

Expected Output

MaximumSalary
50000

Explanation: MAX() compares the salary values and returns the highest value.

Example 2: Find the Highest Salary in the Sales Department

SELECT MAX(Salary) AS SalesMaximumSalary
FROM Employees
WHERE Department = 'Sales';

Expected Output

SalesMaximumSalary
42000

Result: The highest salary in the Sales department is 42000.

8. Use All Five Aggregate Functions in One Query

SQL Server allows you to use multiple aggregate functions in a single SELECT statement.

For example, suppose you want to generate a report containing:

  • Total employees.

  • Total salary.

  • Average salary.

  • Minimum salary.

  • Maximum salary.

SQL Query

SELECT
    COUNT(*) AS TotalEmployees,
    SUM(Salary) AS TotalSalary,
    AVG(Salary) AS AverageSalary,
    MIN(Salary) AS MinimumSalary,
    MAX(Salary) AS MaximumSalary
FROM Employees;

Expected Output

TotalEmployees TotalSalary AverageSalary MinimumSalary MaximumSalary
8 316000 39500.00 30000 50000

Explanation:

  1. COUNT(*) counts all employee records.

  2. SUM(Salary) calculates the total salary.

  3. AVG(Salary) calculates the average salary.

  4. MIN(Salary) finds the lowest salary.

  5. MAX(Salary) finds the highest salary.

Result: A single SQL query generates a complete employee salary summary.

9. Aggregate Functions with GROUP BY

Aggregate functions become even more powerful when combined with the GROUP BY clause.

GROUP BY divides records into groups based on the values of one or more columns.

For example, suppose you want to calculate the total salary of each department.

Example 1: Department-Wise Employee Count

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

Expected Output

Department TotalEmployees
Finance 1
HR 2
IT 3
Sales 2

Example 2: Department-Wise Total Salary

SELECT
    Department,
    SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Department;

Expected Output

Department TotalSalary
Finance 36000
HR 65000
IT 135000
Sales 80000

Example 3: Department-Wise Average Salary

SELECT
    Department,
    AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department;

Expected Output

Department AverageSalary
Finance 36000.00
HR 32500.00
IT 45000.00
Sales 40000.00

Important: When you select a normal column together with an aggregate function, the normal column must generally be included in the GROUP BY clause.

10. Aggregate Functions with HAVING

The HAVING clause filters grouped results based on conditions.

It is commonly used with aggregate functions such as COUNT(), SUM(), and AVG().

Example 1: Find Departments Having More Than Two Employees

SELECT
    Department,
    COUNT(*) AS TotalEmployees
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 2;

Expected Output

Department TotalEmployees
IT 3

Explanation:

  • GROUP BY creates department-wise groups.

  • COUNT(*) counts employees in each group.

  • HAVING keeps only departments with more than two employees.

Example 2: Find Departments with Total Salary Greater Than 70000

SELECT
    Department,
    SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Department
HAVING SUM(Salary) > 70000;

Expected Output

Department TotalSalary
IT 135000
Sales 80000

Remember: WHERE filters individual rows, whereas HAVING filters grouped results.

11. Aggregate Functions and NULL Values

Understanding NULL values is important when working with aggregate functions.

In SQL Server, most aggregate functions ignore NULL values.

Consider the following sample data:

EmployeeID EmployeeName Salary
1 Rahul 40000
2 Priya 35000
3 Amit NULL
4 Neha 30000

Example 1: COUNT(*) with NULL

SELECT COUNT(*) AS TotalRows
FROM Employees;

COUNT(*) counts every row, including rows containing NULL values.

Example 2: COUNT(Salary) with NULL

SELECT COUNT(Salary) AS SalaryCount
FROM Employees;

COUNT(Salary) counts only rows where Salary is not NULL.

Example 3: SUM() with NULL

SELECT SUM(Salary) AS TotalSalary
FROM Employees;

SUM() ignores NULL salary values and adds the available numeric values.

Example 4: AVG() with NULL

SELECT AVG(Salary) AS AverageSalary
FROM Employees;

AVG() calculates the average using only non-NULL salary values.

Important NULL Behaviour

Function Behaviour with NULL
COUNT(*) Counts the row
COUNT(column) Ignores NULL values
SUM(column) Ignores NULL values
AVG(column) Ignores NULL values
MIN(column) Ignores NULL values
MAX(column) Ignores NULL values

Note: If an aggregate function such as SUM(), AVG(), MIN(), or MAX() has no non-NULL input values, its result is NULL. COUNT() returns zero when there are no rows or no qualifying non-NULL values, depending on the expression.

12. Aggregate Functions with DISTINCT

The DISTINCT keyword can be used inside aggregate functions to consider only unique values.

For example, suppose a department column contains repeated department names.

Example 1: Count Unique Departments

SELECT COUNT(DISTINCT Department) AS UniqueDepartments
FROM Employees;

Expected Output

UniqueDepartments
4

Example 2: Calculate Sum of Distinct Salaries

SELECT SUM(DISTINCT Salary) AS DistinctSalaryTotal
FROM Employees;

Explanation: SUM(DISTINCT Salary) adds each distinct non-NULL salary value only once.

Important: DISTINCT changes the input values considered by the aggregate function. Use it only when unique values are required for the calculation.

13. Real-World Example: Employee Salary Summary Report

Suppose a company wants to generate a salary report for every department.

The report should display:

  • Department name.

  • Total employees.

  • Total salary.

  • Average salary.

  • Minimum salary.

  • Maximum salary.

Complete SQL Query

SELECT
    Department,
    COUNT(*) AS TotalEmployees,
    SUM(Salary) AS TotalSalary,
    AVG(Salary) AS AverageSalary,
    MIN(Salary) AS MinimumSalary,
    MAX(Salary) AS MaximumSalary
FROM Employees
GROUP BY Department
ORDER BY TotalSalary DESC;

Expected Output

Department TotalEmployees TotalSalary AverageSalary MinimumSalary MaximumSalary
IT 3 135000 45000.00 40000 50000
Sales 2 80000 40000.00 38000 42000
HR 2 65000 32500.00 30000 35000
Finance 1 36000 36000.00 36000 36000

How this query works:

  1. GROUP BY creates department-wise groups.

  2. COUNT(*) counts employees in each department.

  3. SUM(Salary) calculates total salary.

  4. AVG(Salary) calculates average salary.

  5. MIN(Salary) finds the lowest salary.

  6. MAX(Salary) finds the highest salary.

  7. ORDER BY TotalSalary DESC sorts departments from highest to lowest total salary.

Real-world application: This type of report is useful in payroll systems, HR management applications, business analytics, and employee reporting dashboards.

14. Common Mistakes to Avoid

  1. Using COUNT(column) when you need all rows: COUNT(column) ignores NULL values. Use COUNT(*) when you want to count all rows.

  2. Using SUM() on non-numeric data: SUM() is intended for numeric expressions.

  3. Confusing SUM() and AVG(): SUM() calculates a total, whereas AVG() calculates an average.

  4. Forgetting GROUP BY: When selecting a non-aggregated column alongside an aggregate result, include the required grouping column.

  5. Using WHERE for aggregate conditions: Use HAVING to filter groups based on aggregate results.

  6. Ignoring NULL behaviour: COUNT(column), SUM(), AVG(), MIN(), and MAX() do not treat NULL as an ordinary value.

  7. Assuming GROUP BY sorts results: Use ORDER BY when a specific output order is required.

  8. Using DISTINCT unnecessarily: DISTINCT changes which values are included in a calculation, so apply it only when unique values are intended.

15. Practice Questions

Create the Employees table and practise the following SQL queries:

  1. Count the total number of employees using COUNT().

  2. Calculate the total salary of all employees using SUM().

  3. Find the average employee salary using AVG().

  4. Find the highest salary using MAX().

  5. Find the lowest salary using MIN().

  6. Count the number of unique departments.

  7. Calculate the total salary of the IT department.

  8. Find the average salary of the HR department.

  9. Display department-wise employee counts using GROUP BY.

  10. Calculate department-wise total salary and average salary.

  11. Find departments having more than one employee using HAVING.

  12. Generate a complete department-wise salary summary using all five aggregate functions.

16. Frequently Asked Questions (FAQs)

Q1. What are aggregate functions in SQL Server?

Aggregate functions perform calculations on a set of rows and return a single result for the entire set or for each group of records.

Q2. What is COUNT() in SQL Server?

COUNT() counts rows or non-NULL values in a specified expression. COUNT(*) counts all rows.

Q3. What is the difference between COUNT(*) and COUNT(column)?

COUNT(*) counts all rows, while COUNT(column) counts only non-NULL values in the specified column.

Q4. What is SUM() used for?

SUM() calculates the total of numeric values in a column.

Q5. What is AVG() used for?

AVG() calculates the average of non-NULL numeric values in a column.

Q6. What is the difference between MIN() and MAX()?

MIN() returns the smallest value, whereas MAX() returns the largest value.

Q7. Do aggregate functions ignore NULL values?

Most aggregate functions, including SUM(), AVG(), MIN(), and MAX(), ignore NULL values. COUNT(*) counts rows, while COUNT(column) ignores NULL values in that column.

Q8. Can we use multiple aggregate functions in one SQL query?

Yes. You can use COUNT(), SUM(), AVG(), MIN(), and MAX() together in one SELECT statement.

Q9. Can aggregate functions be used with GROUP BY?

Yes. Aggregate functions are frequently used with GROUP BY to calculate values separately for each group.

Q10. Can we use aggregate functions with HAVING?

Yes. HAVING can filter grouped results using aggregate conditions such as COUNT(*) > 2 or SUM(Salary) > 70000.

17. Conclusion

In this tutorial, we learned about the five important SQL Server Aggregate Functions: COUNT(), SUM(), AVG(), MIN(), and MAX().

We created a sample Employees table and used COUNT() to count records, SUM() to calculate total salary, AVG() to find average salary, MIN() to identify the lowest salary, and MAX() to find the highest salary.

We also explored aggregate functions with GROUP BY, HAVING, DISTINCT, and NULL values. Finally, we created a practical department-wise employee salary report using all five aggregate functions in a single query.

These functions are essential for database reporting, business analysis, employee management systems, student databases, inventory applications, and real-world SQL Server projects.

At Param Classes, we focus on helping students understand SQL Server through structured tutorials, practical SQL queries, and beginner-friendly explanations. Practise these aggregate functions in SQL Server Management Studio (SSMS) to strengthen your database skills and prepare for advanced SQL concepts.

Keep learning SQL Server step by step with Param Classes!

←
Post 19 of 40
→