sqlnullthree-valued-logic

Is SQL unknown identical to NULL?


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?


Solution

  • According to Wikipedia:

    NULL BOOLEAN and UNKNOWN "may be used interchangeably to mean exactly the same thing"

    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 UNKNOWNs and NULLs within the same context - UNKNOWNs only appear when evaluating predicates.

    The are various tools you can use to try to eliminate NULLs, such as COALESCE or IS [NOT] NULL. Most of these cannot be used in the context of evaluating predicates and so will never be used with an UNKNOWN value. E.g. if you have a query like:

    SELECT
        *
    FROM
        TableA
    WHERE A = 'B'
    

    And you know that there are some NULL A values which are thus causing the WHERE clause predicate to produce UNKNOWN, you cannot write:

    SELECT
        *
    FROM
        TableA
    WHERE COALESCE(A = 'B',TRUE)
    

    To eliminate the UNKNOWN.