I'm confused what does UNKNOWN means in SQL in 3 valued logic. Does it mean NULL actually? Is it true that NULL and UNKNOWN can be interchangeable in all boolean contexts?
Is SQL unknown identical to NULL?
8.7k Views Asked by Michael Tsang At
3
There are 3 best solutions below
3

Simple answer:
3 == 2 => FALSE
3 == 3 => TRUE
NULL == 3 => UNKNOWN
3 == NULL => UNKNOWN
NULL == NULL => UNKNOWN
Expression with NULL
on either side or both evaluates to UNKNOWN
.
For example if you have a table:
Employees(id, country)
1, USA
2, USA
3, Canada
4, NULL
Say your boss asks you to select all employees that don't live in USA. You write:
select * from Employees
where country <> 'USA'
and get:
3, Canada
Why 4 is not selected? Because in WHERE
clause only rows are returned where expression evaluates to TRUE
:
1. USA <> 'USA' => FALSE -- don't return this row
2. USA <> 'USA' => FALSE -- don't return this row
3. CANADA <> 'USA' => TRUE -- return this row
4. NULL <> 'USA' => UNKNOWN -- don't return this row
It is not only for equality or inequality. For any predicate
According to Wikipedia:
However, some database systems don't in fact implement SQL's boolean data type (it's optional) and in most of those systems, there is no circumstance where you'd encounter both
UNKNOWN
s andNULL
s within the same context -UNKNOWN
s only appear when evaluating predicates.The are various tools you can use to try to eliminate
NULL
s, such asCOALESCE
orIS [NOT] NULL
. Most of these cannot be used in the context of evaluating predicates and so will never be used with anUNKNOWN
value. E.g. if you have a query like:And you know that there are some
NULL
A
values which are thus causing theWHERE
clause predicate to produceUNKNOWN
, you cannot write:To eliminate the
UNKNOWN
.