Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select rows having column value as null?

Tags:

Consider following database table:

id      |  is_visible ________|_______________ 1       |  true 2       |  false 3       |               -- i.e. null 

I want to select all the rows having is_visible = null. I tried the following queries:

SELECT * FROM tab WHERE is_visible = null;     -- 0 results SELECT * FROM tab WHERE is_visible <> true;    -- only fields with false value SELECT * FROM tab WHERE is_visible <> false;   -- only fields with true 

I tried it in postgresql and sql server
How to select rows having is_visible = null?

like image 806
afzalex Avatar asked Dec 19 '15 12:12

afzalex


People also ask

How do I get columns with NULL values?

You can use df. isnull(). sum() .

How do you select rows that are not NULL?

To display records without NULL in a column, use the operator IS NOT NULL. You only need the name of the column (or an expression) and the operator IS NOT NULL (in our example, the price IS NOT NULL ). Put this condition in the WHERE clause (in our example, WHERE price IS NOT NULL ), which filters rows.


2 Answers

In both Postgres and SQL server,

SELECT * FROM tab WHERE is_visible is null; 

If you want to select the rows for which column values are not null, then use is not null operator:

SELECT * FROM tab WHERE is_visible is not null; 
like image 143
chaudharyp Avatar answered Sep 20 '22 05:09

chaudharyp


you can't compare 2 null values in sql server as null is unknown or not available. https://technet.microsoft.com/en-us/library/aa196339(v=sql.80).aspx

Use IS NULL operator:

SELECT * FROM tab WHERE is_visible IS NULL;  -- only field having null value SELECT * FROM tab WHERE is_visible IS NOT NULL;    -- fields having not null values 
like image 32
Sundar Singh Avatar answered Sep 22 '22 05:09

Sundar Singh