In SQL the language, NULLs are not considered data values. They represent a missing/unknown state. Quoting from Wikipedia's article on SQL NULL:
SQL null is a state (unknown) and not a value. This usage is quite different from most programming languages, where null means not assigned to a particular instance.
This means that any comparison against that UNKNOWN
value can only be UNKNOWN
itself. Even comparing two NULLs can't return true: if both values are unknown, how can we say that they are equal or not?
IS NULL
and IS NOT NULL
are predicates that can be used in conditional expressions. That means that they don't return a value themselves. Therefore, they can't be "cast" to a bit , or treated as a boolean.
Basic SQL comparison operators always return Unknown when comparing anything with Null, so the SQL standard provides for two special Null-specific comparison predicates. The IS NULL and IS NOT NULL predicates (which use a postfix syntax) test whether data is, or is not, Null.
Any other way of treating nulls is a vendor-specific extension.
Finally, BIT
is not a boolean type, it's just a single-bit number. An optional BOOLEAN
type was introduced in SQL 1999 but only PostgreSQL implements it correctly, ie having TRUE
, FALSE
or UNKNOWN
values.
Without a BOOLEAN
type you can't really calculate the result of a conditional expression like A AND B
or x IS NULL
. You can only use functions like NULLIF
or COALESCE
to replace the NULL value with something else.
IS
can't be used outside of a conditional expression. It's not a comparison operator. In null-valued logic, any comparison to NULL is null.IS NULL
andIS NOT NULL
are the only conditional operators that can be used – Kiptonfoo=NULL
is different fromfoo IS NULL
(and why). But, is there some specific scenario where MySQL implementation breaks something? After all, when you useWHERE deletion_date IS NULL
you eventually get a boolean non-nullable decision: display the row o not. – VolutionNULL
with1
, just use a function likeNULLIF
orCOALESCE
. Finally, you can cast a NULL to represent a missing value of a specific type.CAST(NULL as BIT)
is allowed and returns a NULL BIT – Kipton