Kingston Chan
Kingston Chan

Reputation: 923

How to select in PostgreSQL table where an attribute is not NULL?

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?

Upvotes: 3

Views: 2710

Answers (1)

Kingston Chan
Kingston Chan

Reputation: 923

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.

Upvotes: 5

Related Questions