Dealing with NULLS

NULL is a special value that any field or expression can have, except for the fields when it's explicitly forbidden. NULL means the absence of any value. It can also be treated as an unknown value in some cases. In relation to logical values, NULL is neither true nor false. Working with NULL can be confusing, because almost all operators, when taking NULL as an argument, return NULL. If you try to compare some values and one of them is NULL, the result will also be NULL, which is not true.

For example, consider the following condition:

WHERE a > b

This will return NULL if a or b have a NULL value. This is expected, but for the following condition, this is not obvious:

WHERE a = b

Here, even if both a and b have a value ...

Get Learning PostgreSQL 11 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.