Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding SUM(NULL) in MySQL

Tags:

null

mysql

sum

Usually when NULL is involved in any equation then the whole result resolves into NULL (e.g. SELECT 2 + NULL + 5 returns NULL)

Same holds for the following case:

SELECT SUM(NULL) returns NULL. Proposition #1

What happens when SUM is used to aggregate a column and the column can contain NULL values too ?

Based on the proposition #1 why the output doesn't result in NULL.

CREATE TABLE t (age INT NULL);

INSERT INTO t (age)  VALUES (15),(20), (NULL), (30), (35);

SELECT 
SUM(age)
FROM t;

Output: 100

But I was expecting NULL.

Does MySQL silently skips those NULL values in this case?

http://sqlfiddle.com/#!9/3f99bb/2

like image 634
Anonymous One Avatar asked Sep 08 '16 07:09

Anonymous One


People also ask

Does sum ignore NULL values MySQL?

If you use the SUM() function in a SELECT statement that returns no row, the SUM() function returns NULL , not zero. The DISTINCT option instructs the SUM() function to calculate the sum of only distinct values in a set. The SUM() function ignores the NULL values in the calculation.

What is sum of NULL in SQL?

In MySQL, SUM() is supposed to ignore null values. In any case, if you want to be specific, you can instruct the SQL Optimiser to substitute the null values with the value of 0. To do that, you can help yourself with the COALESCE() function and the following model: SELECT COALESCE(SUM(column_name), 0) ...

Does sum ignore NULL values?

SUM can be used with numeric columns only. Null values are ignored.

Does sum count NULL values?

According to the Help Guide (in the Aggregation Functions section): "If no value is found, NULL is returned for all Aggregation Functions, except Sum and Count which both return 0."


2 Answers

Well it's explained in the manual

SUM([DISTINCT] expr)
Returns the sum of expr. If the return set has no rows, SUM() returns NULL. The DISTINCT keyword can be used to sum only the distinct values of expr.

SUM() returns NULL if there were no matching rows.

What's more it's also said that:

This section describes group (aggregate) functions that operate on sets of values. Unless otherwise stated, group functions ignore NULL values.

in other words SUM behaves like this because that's the way it's defined to be.

like image 130
e4c5 Avatar answered Oct 20 '22 00:10

e4c5


You are right, aggregate functions handles null values in a different way than non aggregate functions:

select 2 + NULL + 5

returns NULL because NULL on this context it means an unknown value, so the result will be NULL (unknown) as well.

This will return 7 instead:

select SUM(n)
from (
  select 2 as n
  union all select null
  union all select 5
) s

because on this context a NULL value, even if it is unknown, can be seen as a "not specified value". The reason is that it's very common to use NULL as a not specified value and it's little pratical use to include NULLs in an aggregate function, that's why aggregate functions are defined to ignore NULLs.

like image 23
fthiella Avatar answered Oct 20 '22 00:10

fthiella