Page 1 of 1
SELECT AVG(SALARY) FROM EMP yields inaccurate results - why?
Posted: Sun Nov 17, 2013 5:51 pm
by Anjali Chopra
Hi,
In a interview I'm asked - My SQL statement SELECT AVG(SALARY) FROM EMP yields inaccurate results Why? I'm not sure about the answer, can someone please help?
Re: SELECT AVG(SALARY) FROM EMP yields inaccurate results -
Posted: Mon Nov 18, 2013 7:20 pm
by Anuj Dhawan
AVG is an scalar function which would sum values of all the rows in the given column and divide the result by the number of rows - in other words, it'll provide you with the average for the particular column, on which you apply it. In this particular question the column is SALARY.
Having that in mind, if the column SALARY is not declared to have NULLs - it can give problems. Because in case, for the employees for whom the salary is not known, AVG will take them in account to give you the resultant average, which is not correct. For example, if an employee has not yet joined in and the NULL is not allowed, you've fill in "some default salary" for him/her; this "some default salary" is culprit in getting the average and you get inaccurate result.
Hope this helps.
Re: SELECT AVG(SALARY) FROM EMP yields inaccurate results -
Posted: Tue Nov 19, 2013 11:01 am
by Anjali Chopra
Thanks Anuj!
That's a great and simple worded explanation, Thanks for your help.
