85

In my database table I am having one boolean column, which have some rows with False, True and Null.

These are the cases I have tried:

Case 1:

select * from table_name where
boolean_column is null;

works well. Give the result with all rows having null value for that column.

Case 2:

select *from table_name where boolean_column = False;

Works well. Gives result with all the rows having False value for that column.

Case 3:

This is requirement which does not works. I want all the rows having values False and Null.

I have tried these.

i) select *from table_name where boolean_column is False or Null;

Which only gives the result for False it does not shows null records.

ii) select *from table_name where boolean_column is Null or False;

Which only gives the result for null it does not shows records with False value.

iii) select *from table_name where boolean_column is Null or boolean_column = False;

This simply displays all the rows, it does not apply any condition at all.

What query can solve this problem?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Anil Kesariya
  • 1,044
  • 1
  • 7
  • 13
  • The `or` in `boolean_column is False or Null` does not apply to `boolean_column` being `null` but whether `boolean_column is False` is TRUE OR `Null` is TRUE. Obviously not what your intending. – user1032531 Oct 10 '22 at 00:30

6 Answers6

166

There are 3 states for boolean in PG: true, false and unknown (null). Explained here: Postgres boolean datatype

Therefore you need only query for NOT TRUE:

SELECT * from table_name WHERE boolean_column IS NOT TRUE;
Brad Dre
  • 3,580
  • 2
  • 19
  • 22
73

I'm not expert enough in the inner workings of Postgres to know why your query with the double condition in the WHERE clause be not working. But one way to get around this would be to use a UNION of the two queries which you know do work:

SELECT * FROM table_name WHERE boolean_column IS NULL
UNION
SELECT * FROM table_name WHERE boolean_column = FALSE

You could also try using COALESCE:

SELECT * FROM table_name WHERE COALESCE(boolean_column, FALSE) = FALSE

This second query will replace all NULL values with FALSE and then compare against FALSE in the WHERE condition.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
13

On PostgreSQL you can use:

SELECT * FROM table_name WHERE (boolean_column IS NULL OR NOT boolean_column)
Dmitry Ukolov
  • 658
  • 7
  • 10
8
  1. select *from table_name where boolean_column is False or Null;

    Is interpreted as "( boolean_column is False ) or (null)".

    It returns only rows where boolean_column is False as the second condition is always false.

  2. select *from table_name where boolean_column is Null or False;

    Same reason. Interpreted as "(boolean_column is Null) or (False)"

  3. select *from table_name where boolean_column is Null or boolean_column = False;

    This one is valid and returns 2 rows: false and null.

I just created the table to confirm. You might have typoed somewhere.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Chris Chris
  • 352
  • 3
  • 10
7

Resurrecting this to post the DISTINCT FROM option, which has been around since Postgres 8. The approach is similar to Brad Dre's answer. In your case, your select would be something like

SELECT *
  FROM table_name
 WHERE boolean_column IS DISTINCT FROM TRUE
shortstuffsushi
  • 2,271
  • 19
  • 33
0

@Brad Dre's answer is the least code and same efficiency as this one, but potentially harder to understand for future developers. Chris's third and Dmitry's answers already covered this, but I wanted to add an answer with a bit more background.

In the OP's words:

"I want all the transactions having value False and Null."

Re-worded in almost-sql:

"I want all the transactions where the value is False or the value is Null."

Re-worded into SQL:

SELECT * 
FROM transactions
WHERE 
    value IS FALSE 
    OR value IS NULL;

Often writing the code to be as close as natural language as possible will give future developers hints on the intention of the initial developer. The issue with @Brad Dre's anwser is a future developer might not think of the NULL case in Brad's code, or might have thought Brad forgot about it.

Chris
  • 5,664
  • 6
  • 44
  • 55