How do you calculate the average value of a numeric column?
Instruction: Write a SQL query to calculate the average salary from a 'employees' table.
Updated
Official answer
Use AVG on the requested numeric salary column:
SELECT AVG(salary) AS average_salary
FROM employees;
For salaries 10, 20, NULL the result is 15, because NULL is excluded from both the sum and denominator. An empty table or a column containing only NULLs returns NULL, not zero. Replacing unknown salaries with zero changes the question and can bias the result.
Confirm the table has one relevant row per employee and that salaries share compatible units and periods. A join that repeats some employees can change their weights. This query computes the arithmetic mean of non-NULL rows; a weighted mean or department-level report requires a different specification. Return types and rounding behavior depend on the database, so keep adequate precision and round only where the reporting contract requires it.