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 columnSELECT AVG(column1)FROM table_name;
-- Calculate the average for each groupSELECT column2, AVG(column1)FROM table_nameGROUP BY column2;Example
-- Calculate the average salary of all employeesSELECT AVG(salary)FROM employees;/* Output:| AVG(salary) ||-------------|| 50000 |*/
-- Calculate the average salary for each departmentSELECT department, AVG(salary)FROM employeesGROUP BY department;/* Output:| department | AVG(salary) ||-------------|-------------|| Sales | 50000 || Marketing | 40000 || HR | 60000 |*/