null
is the lack of value, or, more theatrically, it is the unkown. From here, it is perfectly logical, that null + a
, null * a
, null / a
, etc. is resulting as null
. This means that null is an absorbing element on these operations. I wonder why does it have to be an absorbing element on relations as well. null > 5
could be considered to be false as well, with an explanation at least as plausible as we can give for the current behavior. Currently we can say that null > 5
is null
, since the unkown might be greater than 5, or not, so the result is the unkown. But if it was false, then we could say that null > 5
is false, since the lack of value is not greater than 5.
Take a look at these queries:
select *
from books
where author like 'Alex%'
This will return all the books
, which have their author
starting with Alex. Let us see the other books:
select *
from books
where author not like 'Alex%'
This will return all the books where author
does not start with Alex, right? Wrong! It will return all the books which have an author
value which does not start with Alex. If we want to select the books
whose author
does not start with Alex, we have to explicitly include null
values, like this:
select *
from books
where (author is null) or (author not like 'Alex%')
This seems to be an unnecessary complication to me which could be sorted out for future versions. But the question is: what is the explanation of this behavior? Why do we use null
as the unkown instead of lack of value?