# In SQL, NULL means unknown, so = NULL never matches

> NULL is not a value: it means unknown, and you can't know if two unknowns are equal, so use IS NULL, and remember that != also drops NULL rows.

- Question: Why does WHERE column = NULL return no rows?
- Topic: SQL
- Author: Jamal Hansen
- Full post: https://jamalhansen.com/blog/null-the-value-that-isnt/
- This page: https://jamalhansen.com/explain/sql/null-means-unknown/


## In one sentence

`NULL` means "I don't know." Any comparison with "I don't know" gives you "I don't know." And `WHERE` only keeps rows where it knows the answer is TRUE.

## If you know Python

The SQL `NULL` is sort of like `None` in Python. After all, they both represent the lack of a value, right?

```python
value = None
if value == None:  # True (though `is None` is preferred)
    print("It's None")

print(None == None)  # True
```

In Python, `None` is a thing you can compare. `NULL` is not a value. It means "unknown" or "missing." Imagine if `None` in Python were contagious: any operation involving it returned `None` instead of raising an error. That's essentially what SQL does with `NULL`.

## The usual mistake

```sql
SELECT * FROM customers WHERE middle_name = NULL;   -- Returns NOTHING
SELECT * FROM customers WHERE middle_name IS NULL;  -- Correct!
```

In SQL, `NULL` means "I don't know," and you can't know if two unknowns are equal. `WHERE` clauses only return rows where the condition is TRUE. If a comparison involves `NULL` and the result is `NULL` (not TRUE), that row gets filtered out. This is the root of almost every `NULL` surprise.

[The same thing happens with `!=`:]

```sql
SELECT * FROM customers WHERE status = 'active';   -- Excludes NULL
SELECT * FROM customers WHERE status != 'active';  -- ALSO excludes NULL!
```

That second one catches people. If status is `NULL`, then `NULL != 'active'` evaluates to `NULL`, not TRUE, so the row doesn't make the cut.

## Example

To include `NULL` values, you have to ask for them explicitly.

```sql
SELECT * FROM customers WHERE status != 'active' OR status IS NULL;
```

To show a default instead of `NULL`, use `COALESCE()`. It takes as many values as you want and returns the first one that is not `NULL`, working left to right.

```sql
SELECT COALESCE(nickname, name, 'Unknown') AS display_name FROM customers;
```

In Python terms, `COALESCE` is like `value if value is not None else default`, but it can chain through multiple fallbacks.

## Go deeper

[NULL: The Value That Isn't](https://jamalhansen.com/blog/null-the-value-that-isnt/), post 15 of [SQL for Python Developers](https://jamalhansen.com/series/sql-for-python-developers/): three-valued logic, `NULL` in `GROUP BY` and joins, the `NOT IN` trap, and `NULLIF`.

