sql count null as 0 1

sql count null as 0

SELECT sum(CASE WHEN column_A IS NULL THEN 1 ELSE 0 END) A,
       sum(CASE WHEN column_B IS NULL THEN 1 ELSE 0 END) B
FROM my_table;

Here is what the above code is Doing:
1. The CASE statement is used to check if the column is null.
2. If the column is null, then the CASE statement returns 1, else it returns 0.
3. The SUM function is used to sum up all the values returned by the CASE statement.
4. The result is a single row that shows the number of null values for each column.

Similar Posts