Average salary with AVG
Question
Show the average salary of all employees, rounded to a whole number.
employeesid, name, salary, department_id, manager_id, hired_on
departmentsid, name, city
Your query
Ctrl+Enter to run
Result
Expected result
| average_salary |
|---|
| 6367 |
Hint
AVG() gives the average and ROUND(value) rounds it.
Show the solution
solution.sql
Run this code
SELECT ROUND(AVG(salary)) AS average_salary
FROM employees;
Why it works
AVG adds up the salaries and divides by the number of non-NULL salaries. ROUND with one argument rounds to zero decimal places.
Common mistake
In some databases, AVG on an integer column returns an integer and cuts off the decimals. In SQLite AVG always returns a real number, so rounding is up to you.