Search code examples
sqlsql-server

When does COUNT(*) return NULL?


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

But then I searched the interwebs and my findings 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 when COUNT(*) returns NULL?


Solution

  • It doesn't return NULL. The GROUP BY in your example makes it return no rows at all, which is not the same as a NULL in a column.