Reputation: 807
I want to generate a SQL query to get null count in a particular column like
SELECT COUNT (column) AS count
FROM table
WHERE column = null ;
This is returning 0, but I want how many null values are present in that column like
SELECT COUNT (column) AS count
FROM table
WHERE column = 'some value';
which returns the count of the matched records
Upvotes: 2
Views: 476
Reputation: 1334
To get exact output you can use below command as well -
SELECT COUNT (*) AS count FROM table where column IS null OR column='';
because some times only '' doesn't counted as NULL.
Upvotes: 1
Reputation: 1107
A different query but exact answer check it out
select count(*)-count(column) from table
please vote check this as answer if it helps you
Upvotes: 1
Reputation: 726579
NULL
value is special in that you cannot use =
with it; you must use IS NULL
instead:
SELECT COUNT (*) AS count FROM table where column IS null ;
This is because NULL
in SQL does not evaluate as equal to anything, including other NULL
values. Also note the use of *
as the argument of COUNT
.
Upvotes: 4
Reputation: 204766
You can use a conditional sum()
SELECT sum(case when column is null
then 1
else 0
end) AS count
FROM table
Upvotes: 1