SQL Avg
The AVG function calculates the average (mean) of the numeric values in a column.
Calculating an average
AVG() divides the sum of a column by the number of non-NULL values in it, giving you the arithmetic mean. It is a fast way to answer questions like 'what does the typical employee earn?' without adding the numbers up by hand.
Average salary
SELECT AVG(salary) AS average_salary
FROM Employees;Note: AVG ignores NULL values in both the total and the count. A row with a NULL salary does not drag the average down, because it is left out of the calculation entirely.
Rounding the result
An average often comes back with a long decimal tail. Wrapping AVG() in ROUND() keeps the output tidy. The second argument to ROUND controls how many decimal places to keep.
Average salary rounded to two decimals
SELECT ROUND(AVG(salary), 2) AS average_salary
FROM Employees;Averages per group
As with the other aggregates, GROUP BY lets you compute a separate average for each category so you can compare, for example, the mean salary across departments.
Average salary by department
SELECT department, ROUND(AVG(salary), 2) AS avg_salary
FROM Employees
GROUP BY department;Note: Because NULLs are skipped, AVG(salary) is not always the same as SUM(salary) divided by COUNT(*). The count used by AVG only includes rows that actually have a value.