Three-Valued Logic

Concepts covered: Three-Valued Logic

A comparison such as amount > 150 has had 2 possible results so far: TRUE or FALSE. NULL adds a third. Ask whether an unrecorded region equals 'US' and the truthful reply is "can't tell". SQL calls that result UNKNOWN, and a query shows it as NULL.

Logic with 3 possible results is called three-valued logic. It follows one idea all the way through: when an input is missing, the result is whatever can still be known for certain, and often that is nothing.

Seeing the third value

A comparison is an expression, so it can sit in a SELECT list where its result shows up as a column. This query asks every order whether its region is 'US'.

SELECT
  order_id,
  region,
  region = 'US' AS is_us
FROM orders
orders
order_idamountregion
ORD-001100US
ORD-002200NULL
ORD-003150EU
ORD-004300NULL
ORD-005250US
Result
order_idregionis_us
ORD-001US1
ORD-002NULLNULL
ORD-003EU0
ORD-004NULLNULL
ORD-005US1

ORD-001 and ORD-005 come back TRUE and ORD-003 comes back FALSE. ORD-002 and ORD-004 have no region recorded, so the comparison has nothing on its left side to check, and is_us is NULL for both. Guessing FALSE would be a claim about a region nobody recorded, so the engine reports that it cannot tell.

That third line is where NULL changes results. WHERE only lets a row through when its condition is TRUE, so a row whose condition can't be decided never appears in the output. HAVING applies the same rule to groups.

Opposite filters that lose rows

In a table with no NULLs, the filter region = 'US' and its opposite region != 'US' split the rows into 2 piles that add up to the whole. NULLs add a third pile that neither filter returns. Here is each comparison worked out row by row.

order_idregionregion = 'US'region != 'US'
ORD-001USTRUEFALSE
ORD-002NULLUNKNOWNUNKNOWN
ORD-003EUFALSETRUE
ORD-004NULLUNKNOWNUNKNOWN
ORD-005USTRUEFALSE
orders
order_idamountregion
ORD-001100US
ORD-002200NULL
ORD-003150EU
ORD-004300NULL
ORD-005250US

= 'US'

Keeps the 2 rows where the comparison is TRUE: ORD-001 and ORD-005.

SELECT
  order_id,
  region
FROM orders
WHERE region = 'US'
Result
order_idregion
ORD-001US
ORD-005US

!= 'US'

Keeps only ORD-003, the EU order. The 2 orders with a NULL region evaluate to UNKNOWN here as well, so they are dropped again.

SELECT
  order_id,
  region
FROM orders
WHERE region != 'US'
Result
order_idregion
ORD-003EU

The first filter returns 2 orders and its opposite returns 1. That accounts for 3 of the 5 rows. Anyone who reports "2 US orders, so the other 3 are non-US" has assigned a region to 2 orders that never had one.

Even NULL = NULL is unknown

The natural next move is to ask for the missing regions directly with region = NULL. Play the query and watch what each row's comparison produces.

SELECT *
FROM orders
WHERE region = NULL
orders
order_idamountregion
ORD-001100US
ORD-002200NULL
ORD-003150EU
ORD-004300NULL
ORD-005250US

It returns 0 rows. For ORD-001 the question is whether 'US' equals a value nobody knows, and that is UNKNOWN. For ORD-002 the question is whether one unknown value equals another. 2 missing regions could be the same region or different ones, so that's UNKNOWN too. No row reaches TRUE, and the query raises no error to warn you.

Every comparison operator you know behaves this way. Test a NULL with < or > and you get UNKNOWN. != and IN give the same answer.

AND with an unknown side

AND returns TRUE only when both of its sides are TRUE. Three-valued logic adds one rule: a FALSE on either side still settles the answer as FALSE, because nothing the unknown side turns out to be could rescue it. Otherwise an UNKNOWN side leaves the whole condition UNKNOWN.

Left sideRight sideLeft AND rightRow kept?
TRUETRUETRUEYes
TRUEUNKNOWNUNKNOWNNo
FALSEUNKNOWNFALSENo
UNKNOWNUNKNOWNUNKNOWNNo

Take WHERE region = 'US' AND amount > 250 on ORD-004, which has amount 300 and no region. The amount test is TRUE and the region test is UNKNOWN, so the row lands on the second line of the table and is dropped, even though its amount passes.

orders holds 5 rows of (order_id, amount, region): (ORD-001, 100, US), (ORD-002, 200, NULL), (ORD-003, 150, EU), (ORD-004, 300, NULL), (ORD-005, 250, US). Which orders does this query return?

SELECT
  order_id
FROM orders
WHERE region != 'EU'
  AND amount > 100

So comparisons can find values and can rule values out, but they can't find a gap. Asking for the rows where a value is missing takes a different kind of test, one that looks at whether the cell holds anything at all.

About this lesson

This section is part of the NULL Values: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.