Curriculum
This tutorial, we will show you how to use SQL AVG function to get the average value of a set.
The SQL AVG function is an aggregate function that calculates the average value of a set. The following illustrates the syntax of the SQL AVG function:
AVG([ALL|DISTINCT] expression)
If we use the ALL keyword, the AVG function takes all values in the calculation. By default, the AVG function uses ALL whether we specify it or not.
If we specify the DISTINCT keyword explicitly, the AVG function will take the unique values only in the calculation.
For example, we have a set of (1,2,3,3,4) and apply the AVG(ALL) to this set, the AVG function will perform the following calculation:
(1+2+3+3+4)/5 = 2.6
However, the AVG(DISTINCT) will process as follows:
(1+2+3+4)/4 = 2.5
We will use the employees
table in the sample database to demonstrate how the SQL AVG function works.
To calculate the average salary of all employees, you apply the AVG function to the salary column as follows:
SELECT AVG(salary) FROM employees;
Let’s apply the DISTINCT operator to see if the result changes:
SELECT AVG(DISTINCT salary) FROM employees;
Let’s apply the DISTINCT operator to see if the result changes:
SELECT AVG(DISTINCT salary) FROM employees;
It changed because some employees have the same salary.
To round the result to 2 decimal places, you use the ROUND function as follows:
SELECT ROUND(AVG(DISTINCT salary), 2) FROM employees;
To calculate the average value of a subset of values, we add a WHERE clause to the SELECT statement. For instance, to calculate the average salary of employees in the department id 5, we use the following query:
SELECT AVG(DISTINCT salary) FROM employees WHERE department_id = 5;
The following statement returns the average salary of employees who hold the job id 6:
SELECT AVG(salary) FROM employees WHERE job_id = 6;
To calculate the average values of groups, we use the AVG function with the GROUP BY clause. For example, the following statement returns the departments and the average salary of employees of each department.
SELECT department_id, AVG(salary) FROM employees GROUP BY department_id;
We can use the inner join clause to join the employees
table with the departments
table to get the department name data:
SELECT e.department_id, department_name, AVG(salary) FROM employees e INNER JOIN departments d ON d.department_id = e.department_id GROUP BY e.department_id;
To sort the result set that includes the AVG results, you use the AVG function in the ORDER BY clause as follows:
SELECT e.department_id, department_name, AVG(salary) FROM employees e INNER JOIN departments d ON d.department_id = e.department_id GROUP BY e.department_id ORDER BY AVG(salary) DESC;
We can apply AVG function multiple times in a single SQL statement to calculate the average value of a set of average values.
For example, we can use the AVG function to calculate the average salary of employees in each department, and apply the AVG function one more time to calculate the average salary of departments.
The following query illustrates the idea:
SELECT AVG(employee_sal_avg) FROM ( SELECT AVG(salary) employee_sal_avg FROM employees GROUP BY department_id ) t;
How the query works.