Last answered:

03 Apr 2022

Posted on:

26 Mar 2022

0

SUM for columns with null values

The column contains both values and missing values. Will SUM function work for such columns by ignoring those null values?Thanks.

1 answers ( 0 marked as helpful)
Instructor
Posted on:

03 Apr 2022

1

Hi Christine Li!

Thanks for reaching out.

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)
...

Hope this helps.
Best,
Martin

Submit an answer