NULL means unknown
In the context of a database, if a column is set to NULL
, it effectively means that the value is unknown. If we compare any other value with NULL
, the result of that comparison is also unknown. In other words, a value can never be equal to NULL
, as NULL
is the absence of a value. This means the expression ColumnValue = NULL
will never evaluate to true or false; even if ColumnValue
is in fact NULL
, it will always evaluate to unknown. To detect if a column value is NULL
, we must use the special expressions IS NULL
or IS NOT NULL
rather than =
or <>
.
Note
This handling of NULL
is not unique to the SQL Database Engine, it is based on the ANSI standard handling of NULL
values.
Having NULL
values in our database is not an anti-pattern in and of itself, but when we assign a meaning to the value NULL
in our application, we may face some challenges when it comes to writing performant T-SQL due to the need for special handling of NULL
comparisons.
Let’...