In PostgreSQL:
<>
or !=
means Not equal, but cannot treat NULL
as a comparable value.
IS DISTINCT FROM
means Not equal, and can treat NULL
as a comparable value.
So for example, there is person
table as shown below:
postgres=# \pset null NULL
Null display is "NULL".
postgres=# SELECT * FROM person;
id | name | age
----+--------+------
1 | John | 27
2 | David | 32
3 | Robert | NULL
4 | Mark | NULL
(4 rows)
Then, <>
or !=
cannot get the row where age
in NULL
as shown below:
postgres=# SELECT * FROM person WHERE age <> 27;
id | name | age
----+-------+-----
2 | David | 32
(1 row)
Or:
postgres=# SELECT * FROM person WHERE age != 27;
id | name | age
----+-------+-----
2 | David | 32
(1 row)
But, IS DISTINCT FROM
can get the row where age
in NULL
as shown below:
postgres=# SELECT * FROM person WHERE age IS DISTINCT FROM 27;
id | name | age
----+--------+------
2 | David | 32
3 | Robert | NULL
4 | Mark | NULL
(3 rows)
In addition, <>
or !=
and IS NULL
can get the row where age
in NULL
as shown below:
postgres=# SELECT * FROM person WHERE age <> 27 OR age IS NULL;
id | name | age
----+--------+------
2 | David | 32
3 | Robert | NULL
4 | Mark | NULL
(3 rows)
Or:
postgres=# SELECT * FROM person WHERE age != 27 OR age IS NULL;
id | name | age
----+--------+------
2 | David | 32
3 | Robert | NULL
4 | Mark | NULL
(3 rows)