0

Here is my query:

select * from students where status != 4. 

I am not getting rows with status null.

Shouldn't it return all the rows where status is not 4 - including rows with null status values?

I can get what I need by following query.

select * from students where status != 4 or status is null
1

4 Answers 4

4

Use:
IS DISTINCT FROM

SELECT * FROM students WHERE status IS DISTINCT FROM 4;  

http://www.postgresql.org/docs/current/interactive/functions-comparison.html

2

SQL's understanding of NULL is "three-valued". Think of NULL as UNKNOWN. So, in other words:

UNKNOWN  = 4 yields      UNKNOWN
UNKNOWN != 4 yields also UNKNOWN

In other words, if a student has status is null, then it is "unknown" whether that status is different from 4

0
0

This is a bit disturbing but null is not a value and requires its specific operator is.

0

Any comparison using normal comparison operators with null is never true, except for the two special IS NULL and IS NOT NULL comparisons.

1
  • And the other two special comparison operators IS DISTINCT FROM and IS NOT DISTINCT FROM (with their irritating backwards logic). Commented Jan 18, 2014 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.