Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use SUM() without ISNULL()

I am trying to hence the performance of an SP. I have a doubt in my mind about SUM and ISNULL. When I sum up a column, should I use ISNULL? Is it SAFE to use SUM() without ISNULL. My example is below

SUM(ISNULL(COL1,0))

Since ISNULL costs a lot, I intend to use SUM without ISNULL like below

SUM(COL1)

I did some small tests and I couldnt see results

like image 588
Arif YILMAZ Avatar asked May 18 '16 09:05

Arif YILMAZ


People also ask

Does SUM function ignore NULL?

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.

Can you SUM NULL values?

MySQL and PostgreSQL cannot sum up NULL values with the + value. The sum value will be NULL . If you want to do additions in the database: use SUM if it's an option to sum up a column of a result set instead of expressions ( SUM ignores NULL values)

Does SUM return NULL SQL?

If there are no rows, sum() will return null . It will also return null if all rows have a null balance.

How do you use Isnull and SUM?

You wrap the SUM with an ISNULL, ISNULL(Sum(Ac_Billbook. CostsNet), 0) because SUM ignores nulls when summing or you can reverse the SUM and ISNULL like this, Sum(ISNULL(Ac_Billbook. CostsNet, 0)) which will convert individual NULL's to 0. Doesn't really matter which one you do.


4 Answers

Yes its safe . You can use Sum without handling NULL Value. You can also check that.

You can use like that also.

ISNULL(SUM(COL1),0).

Returns the sum of all the values, or only the DISTINCT values, in the expression. SUM can be used with numeric columns only. Null values are ignored.

For Reference : https://msdn.microsoft.com/en-IN/library/ms187810.aspx

like image 156
Sandeep Kumar Avatar answered Oct 02 '22 16:10

Sandeep Kumar


Its not mandatory to use NULL and COALESCE while doing SUM because SUM always ignore null values.

Refer the link : https://msdn.microsoft.com/en-IN/library/ms187810.aspx for more info.

like image 39
Ragesh Avatar answered Oct 02 '22 17:10

Ragesh


Updated

If you have [1, 2, NULL, 5] in 4 columns, it will give the output as 8.

However, it is not safe to use SUM() without checking for NULLS in many cases.

You can receive null when it has no matching content for a given clause. And if you are using this SUMMED value in another function, that maybe a point of concern.

More details here: https://msdn.microsoft.com/en-GB/library/ms187810.aspx

Please also look at COALESCE method https://msdn.microsoft.com/en-IN/library/ms190349.aspx

PS: Also check out this post - My Select SUM query returns null. It should return 0

Here are 3 images that shows without checking for NULL it returns NULL and not 0.

SUM with ISNULL CHECK enter image description here

SUM without ISNULL CHECK

enter image description here

SUM with COALESCE enter image description here

like image 27
vohrahul Avatar answered Oct 02 '22 18:10

vohrahul


Its better to use COALESCE method before the SUM aggregation.

like image 22
Uday Prasanna N Avatar answered Oct 02 '22 17:10

Uday Prasanna N