jpmc26
jpmc26

Reputation: 29874

COUNT number of NULLs in multiple columns

I want to count the number of occurrences of NULL in several columns in a single table. I need the counts per column, not a total count on the entire table. I would very strongly prefer to do so in a single query, to avoid the overhead of the back and forth, simplify my app side code, and avoid multiple table scans.

How can I count the number of rows where each particular column is NULL?

Ideas so far

Is there a cleaner way?

Upvotes: 1

Views: 2759

Answers (1)

JMac
JMac

Reputation: 1756

Try this:

SELECT COUNT(*)-COUNT(column1) As column1, COUNT(*)-COUNT(column2) As column2 FROM my_table; 

Maybe this will work for you. Since like you mentioned, COUNT only counts non-null values, this should give you the sum of null values you need.

Upvotes: 6

Related Questions