3

I have this PostgreSQL table, test:

 a | b | c | d 
---+---+---+---
 5 |   | 5 | 7
 5 | 6 |   |  
 1 | 2 | 3 |  

I want to query all tuples whose b value is not NULL:

SELECT * FROM test WHERE b != NULL;
SELECT * FROM test WHERE b <> NULL;

The two commands both have no return records:

 a | b | c | d 
---+---+---+---
(0 rows)

What command should I use to select all tuples that has a b value?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Kingston Chan
  • 923
  • 2
  • 8
  • 25

1 Answers1

4

IS NOT NULL

Use this command:

SELECT * FROM test WHERE b IS NOT NULL;

The result is:

 a | b | c | d 
---+---+---+---
 5 | 6 |   |  
 1 | 2 | 3 |  
(2 rows)

Standard SQL

Both predicates IS NULL & IS NOT NULL are standard SQL (Wikipedia). So they work in Postgres as well as other compliant databases.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Kingston Chan
  • 923
  • 2
  • 8
  • 25