Skip to content
Advertisement

Count NULL Values from multiple columns with SQL

I have 3 columns let say A, B, and C. I need to count the NULL values in each column.

For example:

 A  | B  | C
-------------
1   |NULL| 1
1   | 1  | NULL
NULL| 1  | 1
NULL|NULL| 1

Should output:

 A  |  B  |  C
---------------
 2  |  2  |  1

I’ve tried count, sum, sub-queries but nothing has worked for me yet. Any input would be appreciated!

Advertisement

Answer

SELECT COUNT(*)-COUNT(A) As A, COUNT(*)-COUNT(B) As B, COUNT(*)-COUNT(C) As C
FROM YourTable; 
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement