# NULL & Three-Valued Logic

> Understand NULL as unknown, why = NULL never matches, and why WHERE drops rows whose test is unknown.

Subject: SQL | Lesson 3 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/null-and-three-valued-logic
Previous: [SQL Data Types](https://codewithanimation.in/sql/sql-data-types.md) | Next: [Primary Keys & Uniqueness](https://codewithanimation.in/sql/primary-keys-and-uniqueness.md)

## NULL means unknown

`NULL` means a value is missing or unknown. It is not zero, and it is not an empty string. In our `customers` table, Chen and Eli have `NULL` for `age` because we do not know it.

Because a value can be unknown, a test can be unknown too. SQL logic has three results: `TRUE`, `FALSE` and unknown, which SQL writes as `NULL`.

A normal comparison gives `NULL` when either side is `NULL`. So `7 = NULL` is `NULL`, and so is `7 <> NULL`.

## The database must not guess

If we do not know Chen's age, the honest answer to "is Chen older than 18?" is "unknown". If the database guessed, your results would quietly be wrong.

`WHERE` keeps a row only when the test is `TRUE`. Rows where the test is `FALSE` or unknown are both dropped.

## Ask about NULL the right way

Here are three queries on `customers`. The comments show which customers come back:

```sql
SELECT * FROM customers WHERE age > 18;      -- Asha, Dara
SELECT * FROM customers WHERE age = NULL;     -- no rows, ever
SELECT * FROM customers WHERE age IS NULL;    -- Chen, Eli
```

For Chen, `age > 18` is unknown, so Chen is dropped. `age = NULL` is unknown for every row, so nothing is kept. To test for missing values, use `IS NULL` or `IS NOT NULL`.

`AND` and `OR` follow one idea: unknown stays unknown unless the other side already decides the answer. `FALSE AND NULL` is `FALSE`. `TRUE OR NULL` is `TRUE`. `TRUE AND NULL` is `NULL`.

## Common mistake: Writing = NULL

Beginners often write `WHERE age = NULL` and get no rows. The reason is that `NULL` is not equal to anything, not even to `NULL`, because we do not know whether two unknown values match.

A second trap: `NOT (age > 18)` does not bring back the unknown rows either. `NOT` of unknown is still unknown, so Chen and Eli stay dropped.

## Tip: Compare NULL like a normal value when you need to

`IS NOT DISTINCT FROM` works like `=`, except it treats two `NULL` values as equal and never returns unknown. `IS DISTINCT FROM` is the matching "not equal".

## Try it: Watch each row get TRUE, FALSE or UNKNOWN

Pick a `WHERE` condition and read the result for every customer. Then open the Truth table to see how `AND`, `OR` and `NOT` treat `NULL`.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/null-and-three-valued-logic

## Check yourself

### Question 1

What does `age = NULL` return for each customer?

- A. Unknown (NULL), so no row is kept
- B. TRUE for the customers with no age
- C. An error

Correct answer: A

A normal comparison with NULL is unknown, and WHERE drops rows whose test is not TRUE.

- Why not B: Two unknown values are not known to be equal, so the test is unknown.
- Why not C: It is valid SQL. It just never matches.

### Question 2

Which query finds the customers whose age we do not know?

- A. WHERE age IS NULL
- B. WHERE age = 0
- C. WHERE age = NULL

Correct answer: A

`IS NULL` is the test made for missing values. It returns TRUE or FALSE, never unknown.

- Why not B: Zero is a real age, not a missing one.
- Why not C: This is unknown for every row, so it returns nothing.

### Question 3

Ben is 17 and Chen's age is NULL. After `WHERE NOT (age > 18)`, which customers are kept?

- A. Ben only
- B. Ben and Chen
- C. Nobody

Correct answer: A

Only a TRUE result keeps a row. Ben gets TRUE. Chen gets unknown and is dropped.

- Why not B: NOT of unknown is still unknown, and unknown rows are dropped.
- Why not C: For Ben, `age > 18` is FALSE, so NOT of it is TRUE and Ben is kept.

## Remember this

- NULL means unknown, not zero and not an empty string.
- A normal comparison with NULL gives NULL, so `= NULL` never matches. Use `IS NULL`.
- WHERE keeps a row only when the test is TRUE, so FALSE and unknown rows are both dropped.
- Unknown stays unknown in AND, OR and NOT unless the other side already decides the answer.
