AVG() Function
Learn how to calculate the average of numeric values using the AVG() aggregate function in SQL Server.
What is AVG()?
The AVG() function is an aggregate function in SQL Server used to calculate the average value of a numeric column.
It is commonly used to calculate average salary, average marks, average price, average rating and other numerical values.
AVG() adds the values and calculates their average.
AVG() Syntax
SELECT AVG(column_name)
FROM table_name;
Example Table
Consider the following Employees table:
| EmployeeId | EmployeeName | Department | Salary |
|---|---|---|---|
| 1 | Rahul | IT | 50000 |
| 2 | Priya | HR | 40000 |
| 3 | Amit | IT | 60000 |
| 4 | Neha | HR | 45000 |
| 5 | Rohit | Sales | 35000 |
Basic AVG() Example
To calculate the average salary of all employees:
SELECT AVG(Salary) AS AverageSalary
FROM Employees;
Calculation
(50000 + 40000 + 60000 + 45000 + 35000) / 5
= 46000
Output
| AverageSalary |
|---|
| 46000 |
AVG() with WHERE
AVG() can be combined with WHERE to calculate the average only for records that satisfy a condition.
For example, calculate the average salary of employees whose salary is greater than 40000:
SELECT AVG(Salary) AS AverageSalary
FROM Employees
WHERE Salary > 40000;
Calculation
(50000 + 60000 + 45000) / 3
= 51666.67
AVG() with GROUP BY
AVG() can be combined with GROUP BY to calculate the average value for each group.
SELECT Department,
AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department;
Output
| Department | AverageSalary |
|---|---|
| HR | 42500 |
| IT | 55000 |
| Sales | 35000 |
AVG() with HAVING
HAVING can be used to filter groups based on their average value.
For example, display departments whose average salary is greater than 40000:
SELECT Department,
AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 40000;
Output
| Department | AverageSalary |
|---|---|
| HR | 42500 |
| IT | 55000 |
AVG() and NULL Values
AVG() ignores NULL values while calculating the average.
For example:
50000
60000
NULL
40000
SQL Server calculates the average using the three available numeric values:
(50000 + 60000 + 40000) / 3
= 50000
AVG(DISTINCT)
AVG(DISTINCT column) calculates the average using only unique values.
SELECT AVG(DISTINCT Salary) AS AverageSalary
FROM Employees;
DISTINCT removes duplicate values before calculating the average.
AVG() with ROUND()
We can use ROUND() when we want to control the number of decimal places in the average result.
SELECT ROUND(AVG(Salary), 2) AS AverageSalary
FROM Employees;
Real-World Uses of AVG()
- Calculate average employee salary.
- Calculate average student marks.
- Calculate average product price.
- Calculate average customer rating.
- Calculate average monthly sales.
Important Points
- AVG() is an aggregate function.
- AVG() works with numeric values.
- AVG() ignores NULL values.
- AVG() can be used with WHERE.
- AVG() can be used with GROUP BY.
- HAVING can filter grouped AVG() results.
- AVG(DISTINCT column) calculates the average of unique values.
Summary
AVG() is used to calculate the average of numeric values in SQL Server. It can be combined with WHERE, GROUP BY and HAVING to calculate averages for specific records or groups.