When does COUNT (*) return NULL?

Hi I have a colleague who always writes ISNULL(COUNT(*),0) , but I always thought that COUNT(*) could never return NULL .

But then I searched for interwebs, and my results allowed me to write this little piece of code:

 create table t1 ( val1 varchar(50), ) select count(*) from t1 where val1 like 'abc' group by val1 

Are there any other cases where COUNT (*) returns NULL ?

+6
source share
3 answers

It does not return NULL . GROUP BY in your example causes it to not return any rows at all, which is not NULL in the column.

+10
source

This example does not return NULL. It does not return any rows due to GROUP BY on an empty set.

COUNT (*) cannot return NULL. Therefore, ISNULL is not required.

+2
source

ISNULL it is not needed, it will return the number

0
source

Source: https://habr.com/ru/post/885436/


All Articles