Rajasekhar
Rajasekhar

Reputation: 807

SQL query to get null count from a column

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

Answers (4)

Bector
Bector

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

koushik veldanda
koushik veldanda

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

Sergey Kalinichenko
Sergey Kalinichenko

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

juergen d
juergen d

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

Related Questions