It would be appreciated explaining the internal functionality of SUM function in Oracle, when encountering null values:
The result of
select sum(null)
If you are looking for a rationale for this behaviour, then it is to be found in the ANSI SQL standards which dictate that aggregate operators ignore NULL values.
If you wanted to override that behaviour then you're free to:
Sum(Coalesce(<expression>,0))
... although it would make more sense with Sum() to ...
Coalesce(Sum(<expression>),0)
You might more meaningfully:
Avg(Coalesce(<expression>,0))
... or ...
Min(Coalesce(<expression,0))
Other ANSI aggregation quirks:
So ...
Coalesce(Count(<expression>),0)
... is a waste of a good coalesce.
You're looking at this the wrong way around. SUM() operates on a column, and ignores nulls.
To quote from the documentation:
This function takes as an argument any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. The function returns the same data type as the numeric data type of the argument.
A NULL has no data-type, and so your first example must return null; as a NULL is not numeric.
Your second example sums the numeric values in the column. The sum of 0 + null + 1 + 2 is 3; the NULL simply means that a number does not exist here.
Your third example is not an operation on a column; remove the SUM() and the answer will be the same as nothingness + 1 is still nothingness. You can't cast a NULL to an empty number as you can with a string as there's no such thing as an empty number. It either exists or it doesn't.
This is incorrect: The sum of 0 + null + 1 + 2 is 3; select 0 + null + 1 + 2 total from dual;
Result is null! Similar statements give result null if any operand is null.
As Bohemian has pointed out, both SUM and AVG exclude entries with NULL in them. Those entries do not go into the aggregate. If AVG treated NULL entries as zero, it would bias the result towards zero.
It may appear to the casual observer as though SUM is treating NULL entries as zero. It's really excluding them. If all the entries are excluded, the result is no value at all, which is NULL. Your example illustrates this.
Arithmetic aggregate functions ignore nulls.
SUM()
ignores themAVG()
calculates the average as if the null rows didn't exist (nulls don't count in the total or the divisor)SQL does not treat NULL
values as zeros when calculating SUM
, it ignores them:
Returns the sum of all the values, or only the
DISTINCT
values, in the expression. Null values are ignored.
This makes a difference only in one case - when the sequence being totalled up does not contain numeric items, only NULL
s: if at least one number is present, the result is going to be numeric.