---
title: "Explain the Aggregate functions in the SQL server."  
description: "Aggregate functions in SQL Server perform a calculation on a set of values and return a single value."  
author: "Ravi Vishwakarma"  
published: 2024-07-15  
updated: 2024-07-15  
canonical: https://www.mindstick.com/blog/304494/explain-the-aggregate-functions-in-the-sql-server  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 3 minutes  

---

# Explain the Aggregate functions in the SQL server.

[Aggregate functions](https://www.mindstick.com/forum/158949/what-are-the-conditional-aggregate-functions-in-sql-such-as-sum-case-and-how-are-they-used) in [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) perform a [calculation](https://www.mindstick.com/forum/160723/factorial-calculation) on a set of values and return a single value. They are often used with the `GROUP BY` clause to group rows that share a common attribute. Here are the main aggregate [functions in SQL](https://www.mindstick.com/forum/33707/what-is-the-difference-between-rank-and-dense_rank-functions-in-sql-server) Server:

### 1. COUNT

The `COUNT` function returns the number of rows in a set. There are two main variations:

- `COUNT(*)` counts all rows, including those with `NULL` values.
- `COUNT(column_name)` counts only non-`NULL` values in the specified column.

## Examples:

```plaintext
-- Counts all rows in the table
SELECT COUNT(*) FROM Employees;

-- Counts only rows where Salary is not NULL
SELECT COUNT(Salary) FROM Employees;
```

### 2. SUM

The `SUM` function returns the total sum of a numeric column, ignoring `NULL` values.

## Example:

```plaintext
SELECT SUM(Salary) FROM Employees;
```

### 3. AVG

The `AVG` function returns the average value of a numeric column, ignoring `NULL` values.

## Example:

```plaintext
SELECT AVG(Salary) FROM Employees;
```

### 4. MIN

The `MIN` function returns the smallest value in a set. It can be used with numeric, string, or date columns.

## Example:

```plaintext
SELECT MIN(Salary) FROM Employees;
SELECT MIN(JoinDate) FROM Employees;
```

### 5. MAX

The `MAX` the function returns the [largest value](https://www.mindstick.com/forum/156802/write-a-program-that-take-n-integer-number-as-input-and-display-the-second-largest-value) in a set, Like `MIN` it can be used with numeric, string, or date columns.

## Example:

```plaintext
SELECT MAX(Salary) FROM Employees;
SELECT MAX(JoinDate) FROM Employees;
```

### 6. STDEV and STDEVP

The `STDEV` the function calculates the statistical standard deviation of all values in the specified column (sample standard deviation), ignoring `NULL` values. `STDEVP` calculates the [population](https://answers.mindstick.com/qa/33541/list-top-10-tiger-reserves-in-india-with-maximum-tiger-population) standard deviation.

## Examples:

```plaintext
SELECT STDEV(Salary) FROM Employees;
SELECT STDEVP(Salary) FROM Employees;
```

### 7. VAR and VARP

The `VAR` the function calculates the variance of all values in the specified column (sample variance), ignoring `NULL` values. `VARP` calculates the population variance.

## Examples:

```plaintext
SELECT VAR(Salary) FROM Employees;
SELECT VARP(Salary) FROM Employees;
```

### Using Aggregate Functions with GROUP BY

Aggregate functions are often used with the `GROUP BY` clause to group rows that share a common attribute and perform calculations on each group.

## Example:

```plaintext
SELECT DepartmentID, AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY DepartmentID;
```

### Using Aggregate Functions with HAVING

The `HAVING` clause is used to filter groups based on aggregate calculations. It is similar to the `WHERE` clause but is used with `GROUP BY`.

## Example:

```plaintext
SELECT DepartmentID, COUNT(*) AS NumberOfEmployees
FROM Employees
GROUP BY DepartmentID
HAVING COUNT(*) > 10;
```

### Handling NULL Values

Aggregate functions generally ignore `NULL` values except for `COUNT(*)`, which includes `NULL` values.

## Example:

```plaintext
SELECT
COUNT(*) AS TotalEmployees,
COUNT(Salary) AS EmployeesWithSalary
FROM Employees;
```

[Understanding](https://www.mindstick.com/articles/12918/cat-5e-vs-cat-6a-understanding-the-major-differences) and using these aggregate functions effectively can greatly [enhance your](https://www.mindstick.com/articles/12492/how-to-enhance-your-presence-on-facebook) ability to perform [data analysis](https://yourviews.mindstick.com/audio/1108/why-is-communication-important-in-data-analysis) and reporting in SQL Server.

---

Original Source: https://www.mindstick.com/blog/304494/explain-the-aggregate-functions-in-the-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
