Reputation: 4724
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
Upvotes: 0
Views: 93
Reputation: 425198
Any comparison using normal comparison operators with null is never true, except for the two special IS NULL and IS NOT NULL comparisons.
Upvotes: 0
Reputation: 441
Use:
IS DISTINCT FROM
SELECT * FROM students WHERE status IS DISTINCT FROM 4;
http://www.postgresql.org/docs/current/interactive/functions-comparison.html
Upvotes: 4
Reputation: 5489
This is a bit disturbing but null
is not a value and requires its specific operator is
.
Upvotes: 0
Reputation: 221106
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
Upvotes: 2