Last updated on 17 Ottobre 2021
There have been endless discussions already if
should be implemented in relational databases, and if it should even be included in the relational theory. My favourite quote on this subject is from the person who helped Ted Codd to divulgate – and probably refine – his relational algebra:
I hate NULL.Chris Date
I tend to hate it too, except that sometimes it is necessary. But my point is totally different from Date’s. I have nothing against the idea of
, I just think that it is wrong by design in SQL.
has two incompatible meanings, and depending from the context, the SQL standard and implementations randomly choose one of them.
Note: SQL:2011 (and probably other SQL standard versions) defines
as the absence of any data value. But this definition is not consistent with the semantics specified in the document itself.
Ted Codd didn’t propose anything corresponding to
in his fist version of the relational algebra. In a later document, he proposed two markers. A marker is something that is placed where you’d expect a value, but is not a regular value. These markers were:
- I-MARKER: Non-applicable, absent value;
- A-MARKER: Applicable but unknown value.
This first proposal has very little to do with SQL design choice of merging these markers into one. Later, Codd himself wrote something about
in his famous 12 rules:
Rule 3: Systematic treatment of null values:
Null values (distinct from the empty character string or a string of blank characters and distinct from zero or any other number) are supported in fully relational DBMS for representing missing information and inapplicable information in a systematic way, independent of data type.
But, as you can see, he only mentioned missing information and inapplicable information, so he didn’t seem to accept that
could include unknown values (unless he implied that a value is missing because it’s unknown, but the 12 rules don’t seem to state this).
Personally, I tend to believe that Codd’s original idea (two markers) would be a bit overcomplicated for most common practical cases. But it is consistent – actually, his purpose was to elaborate a mathematically consistent system, and he did.
SQL doesn’t have the original theory’s consistency, but it is more practical. Yet, when it comes to
, I think it should have chosen one of the following: implementing both the markers, implementing only one of them with consistent semantics, or implementing none.
Again: my point is that SQL merged two ideas into one, and mixed their semantics in an inconsistent way, so the meaning of
depends on the context. I will describe the behaviors of
in different contexts.
NULL = NULL
. In a previous article I explained how to properly compare values that could be
This makes perfectly sense if
is unknown. Is an unknown value equal to an unknown value? The answer is unknown: they could be the same value or not.
is a missing value, this is puzzling. Is a missing value identical to another missing value? The answer in my opinion should be yes. But for sure, the answer shouldn’t be a missing value itself.
However, consider this expression:
null_column >= 100 OR null_column < 100
. This defies the idea that comparisons make sense if
is an unknown value. Comparisons are simply inconsistent in all cases.
NULL and UNKNOWN
-safe comparisons, the
IS NOT NULL
operators can be used. They are synonyms for
IS NOT UNKNOWN
, which suggests that
is an unknown value.
Scalar expressions seem to consider
as an unknown value. Scalar subqueries are a different matter, see below. For example:
1 + NULL = NULL
was a missing value, it would behave exactly as 0, so the above expression would return 1.
Aggregate functions are those that accept any number of 1-column rows and return exactly one value.
returns the number of rows. If a row only contains
s, it still counts as one. So, in this context,
is considered as an unknown existing value.
s. This means that, in this context,
is considered as a missing value.
For a funny fact about
, see this old article by Baron Schwartz.
Other aggregate functions consider
as a missing value – so it is ignored by
GROUP BY, DISTINCT
as a regular value.
This could be consistent with both interpretations of
Some DBMSs return
s before values, others return values first. I’d like to see more consistency, but in both cases it is just a practical choice that does not contradict any interpretation of
Left, right and full outer can return rows that have no match in the other table. When this happens, columns from the other table are populated with
This behaviour treats
s as absent values.
It is interesting to note that, while in most cases this is just fine, sometimes this behaviour is ambiguous. Consider the following query:
SELECT t1.a, t2.b FROM t1 LEFT JOIN t2 ON t1.a = t2.b OR t2.b IS NULL;
In the results, it is impossible to distinguish an absent match in
from a match in
This shows that, even if
represents an absent value, this is not enough to answer joins properly, as it doesn’t say anything about why the value is missing.
Scalar subqueries are nested queries that return one row consisting of one column. In practice, they return a single value, or
in case no value was found. And this is the trap: normally, you don’t know if a subquery returned
because it found it in a table, or because it found nothing.
As mentioned for joins, at least one more marker would be needed to eliminate ambiguity.
Subqueries in the form
WHERE val IN (SELECT ...)
if the value is not found but the subquery returns at least one
. Subqueries in the form
WHERE EXISTS (SELECT ...)
returns 1 even if the subquery only returns
s. This makes sense if
means existing unknown value.
as a normal value. This seems to me coherent with considering
as a missing value. But it is incoherent with the idea of treating it as an unknown value: two unknown values may be identical or not.
Primary keys don’t allow
columns. Primary keys logically identify each row, this is one of the pillars of relational algebra. So this is consistent with treating
as an unknown value. But if the primary key consists of multiple columns, it should be allowed to store
markers – as long as no row only consists of
s and unicity is certain. Of course this is just a theoretical objection, since a practical implementation would probably be not desirable.
Most DBMSs allow to insert any number of
s in a
index. This is consistent with treating
as a missing value: an absence doesn’t duplicate another absence.
Other DBMSs only allow one
per duplicate index. This is not consistent with any
interpretations. Not with absences, as mentioned earlier. And not with unknown values: an unknown value could duplicate another unknown value, but it could also duplicate any known value – the chances are exactly the same. So
shouldn’t be allowed if the table contains more than one row.
Some DBMSs, like Db2, don’t allow any
indexes. This can be a good idea for any interpretation of
, as it’s the only way to be sure that no constraint is violated.
Other meanings of NULL
also has a couple of additional meanings:
- Any value;
- Default value;
The meaning is default when we create a sort of “default row”. For example, we could have a table
, with the columns
. When a user changes a preference, a row is created. But all preferences have a default value, which is used by all users who didn’t change it. The rows with the default values will have a
This technique is not particularly efficient. With many DBMSs, this event prevents us to create a primary key on (
). It’s usually better to use a special value, like 0 or -1 – which is not special for the DBMS, but it’s special for us.
We can also use
with meaning in stored procedures. When
is passed as an argument, the procedure will assign a default value.
Another example is variables. In most DBMSs that support some type of variables, an unset variable is
Sometimes a row represents a fact that is true for any value. You can translate it into English as “any object”, which is usually equivalent to “all objects”.
For example, your website may have contents that appear on multiple pages. You may have a table for those contents, which has a
column. Where this columns is set to
, the content is included in all pages.
Sometimes one of more attributes are not know at the time a row is inserted. So some applications use
as a placeholder. In this case the value that will be inserted may be known by other applications, but not by the database itself: therefore, we can consider this as a special case of using
to indicate an unknown value.
With one caveat: in some situations we don’t know if a value actually exists. A practical implication is that, in such cases, we shouldn’t rely on some of the features discussed above (
…), because they assume that the value exists.
NULL and DML
is used in
statements is quite strange. This does not directly affect the semantics meaning of
, but it is worth mentioning here.
NULL and NOT NULL
It is possible to specify
to determine if a columns should allow
s or not. This is absolutely reasonable.
What I consider less reasonable is that, if we don’t specify this clause,
s will be permitted. Whichever meaning you associate to
, it doesn’t make sense for most columns.
Even worse, there is an exception: for columns that are part of the primary key,
is the default. This is surely confusing for whoever is not familiar with DML.
In my opinion,
should always be specified.
[NOT] NULL and DEFAULT clauses
Columns may have
values or not. The
value applies if no value was specified in the
statement, while it has no effect during an
. I have absolutely nothing against this.
But what happens if no value is specified on
for a column that doesn’t have a
value? Here’s the funny part: it depends if the column can contain
s or not.
If it is allowed,
is inserted. If it is not, the client will receive an error. In my opinion, this difference is confusing. It is also error prone: if a column is not specified, it is possible that the author of the query simply forgot it. The only exceptions should be the ones for which a
About specific implementations
Every SQL implementation is different, and none of them reflects a big portion of the standard. A couple of them deserve some words.
All of the below oddities is documented and should be considered as a design choice, not a bug.
PostgreSQL has several markers, or special values.
is special even between them.
SELECT FLOAT8 'Infinity' = FLOAT8 'Infinity';
This expression returns
, but this is wrong. Some infinities are bigger than others. PostgreSQL allows us to talk about infinity, but it treats is like a regular number.
If you consider
as an absence, the result is funny. The number of points in a plane is the number of points in a straight line, but two absences are not equal.
Oracle is fantastic in this respect. Sometimes it treats
as inconsistently as standard SQL. Sometimes it treats it as… an empty string! Despite this, the documentation says:
Do not use null to represent a value of zero, because they are not equivalent.
I’m not going to dig more into this absurdity, so for more info see Oracle documentation.
SQLite is kind enough to let us decide if
should be treated as one value in
or not. By default it is. To change this behaviour, we can change the
macro and recompile the code.