NULL value count in group by

MySQL explains it in the documentation of function COUNT():

COUNT(expr)

Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement.

COUNT(*) is somewhat different in that it returns a count of the number of rows retrieved, whether or not they contain NULL values.


PostgreSQL also explains it in the documentation:

Most aggregate functions ignore null inputs, so that rows in which one or more of the expression(s) yield null are discarded. This can be assumed to be true, unless otherwise specified, for all built-in aggregates.

For example, count(*) yields the total number of input rows; count(f1) yields the number of input rows in which f1 is non-null, since count ignores nulls; and count(distinct f1) yields the number of distinct non-null values of f1.


It's pretty simple:

count(<expression>) counts the number of values. Like most aggregate functions, it removes null values before doing the actual aggregation.

count(*) is a special case that counts the number of rows (regardless of any null).

count (no matter if * or <expression>) never returns null (unlike most other aggregate functions). In case no rows are aggregated, the result is 0.

Now, you have done a group by on an nullable column. group by put's null values into the same group. That means, the group for nbr null has two rows. If you now apply count(nbr), the null values are removed before aggregation, giving you 0 as result.

If you would do count(id), there would be no null value to be removed, giving you 2.

This is standard SQL behavior and honored by pretty much every database.

One of the common use-cases is to emulate the filter clause in databases that don't support it natively: http://modern-sql.com/feature/filter#conforming-alternatives

The exceptions (aggregate functions that don't remove null prior to aggregation) are functions like json_arrayagg, json_objectagg, array_agg and the like.


count(*) count the number of rows related to the group by colums. Inpependntly of the fatc the the column in group by contain null or not null values

count(nbr) count the number of rows related to the group by column where nbr is not null