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?
SELECT AVG(SALARY) FROM EMP yields inaccurate results - why?
-
- Website Team
- Posts: 100
- Joined: Sun May 12, 2013 12:33 am
- Anuj Dhawan
- Founder
- Posts: 2802
- Joined: Sun Apr 21, 2013 7:40 pm
- Location: Mumbai, India
- Contact:
Re: SELECT AVG(SALARY) FROM EMP yields inaccurate results -
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.
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.
Thanks,
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
-
- Website Team
- Posts: 100
- Joined: Sun May 12, 2013 12:33 am
Re: SELECT AVG(SALARY) FROM EMP yields inaccurate results -
Thanks Anuj!
That's a great and simple worded explanation, Thanks for your help.
That's a great and simple worded explanation, Thanks for your help.
Regards,
Anjali
Anjali
Create an account or sign in to join the discussion
You need to be a member in order to post a reply
Create an account
Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute