X is column with a mixture of numerical and null values.
I don’t understand why treating null values as 0 changes the result of the AVG value.
Shouldn’t it be like this,
(2 + null + 2) / 3 = 2 -- with null value
(2 + 0 + 2) / 3 = 2 -- no null value
If you have
nullvalue, it is not being taken into account, hence number of elements (yourn) is not increased, whereas0is treated as a valid value.For example if you have:
1,1,6,7,null,3,2it will be total20divided by6(i.e.3.33). However if you replacenullwith0it becomes20/7(i.e.2.86).SQLFiddle demonstrating this behavior on MySQL server.
REFERENCE: Have a look in the documentation right below the table: