Instagram
youtube
Facebook
Twitter

Error Calculation and Rounding SQL Query

Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table, but did not realize her keyboard's 0 key was broken until after completing the calculation. She wants your help finding the difference between her miscalculation (using salaries with any zeroes removed), and the actual average salary.
Write a query calculating the amount of error (i.e.: actual - miscalculated average monthly salaries), and round it up to the next integer.

Input Format
The EMPLOYEES table is described as follows:

Note: Salary is measured in dollars per month and its value is < 10^5.

Solution:

SELECT CEIL(AVG(salary) - AVG(REPLACE(salary, '0', ''))) FROM employees;

Explanation:

  • The SELECT statement is used to query data from a database.
  • CEIL(AVG(salary) - AVG(REPLACE(salary, '0', '')))):

This calculates the difference between the average values of the column.