How to Count SQL NULL values in a column?

The COUNT() function is used to obtain the total number of the rows in the result set. When we use this function with the star sign it count all rows from the table regardless of NULL values. Such as, when we count the Person table through the following query, it will return 19972.

How to count SQL NULL values in a table

On the other hand, when we use the COUNT() function with a column name it counts only the non-NULL values in that column.

COUNT() function and SQL NULL values interaction

In order to count NULL values of a column, we can use the following query.

How to count null values in a column

AVG() function and SQL NULL values

The AVG () is used to calculate the average value of a result set, that is, it sums all the values ​​in that result set and divides that sum by the number of rows. One point to note about the AVG() function calculation is that NULL values will not be included in the average calculation.

When we calculate the weekly average fee, the Wednesday fee is not included in the calculation.

NULL values affect AVG function  results

If we want to include the NULL values in the calculation, we can use the COALESCE function.

NULL values and AVG function  interaction