Skip to content

SQL AVG Function

Syntax

The AVG function in SQL is used to calculate the average value of a numeric column.

-- Calculate the average of a numeric column
SELECT AVG(column1)
FROM table_name;
-- Calculate the average for each group
SELECT column2, AVG(column1)
FROM table_name
GROUP BY column2;

Example

-- Calculate the average salary of all employees
SELECT AVG(salary)
FROM employees;
/* Output:
| AVG(salary) |
|-------------|
| 50000 |
*/
-- Calculate the average salary for each department
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
/* Output:
| department | AVG(salary) |
|-------------|-------------|
| Sales | 50000 |
| Marketing | 40000 |
| HR | 60000 |
*/