Stop 03 · Filtering DataTopics · 8/8

Three-Valued Logic (TRUE, FALSE, UNKNOWN)

Part of the SQL Roadmap

In short

You filter city = 'Delhi', then city <> 'Delhi', and add up the counts. They don't match the total. The missing rows are hiding in SQL's third truth value: UNKNOWN.

SQL conditions can be TRUE, FALSE or UNKNOWN. Comparisons with NULL give UNKNOWN, and WHERE keeps only TRUE rows, which explains why NULL rows quietly disappear from both = and <> filters.

The jaadu, as a housing society guard, holds the gate shut in front of a visitor who shrugs with a puzzled face.

Think of it like this

The strict society guard

A guard asks each visitor "Are you from flat 302?" A yes gets in. A no gets turned away. A visitor who shrugs "I'm not sure" is also turned away, because the guard only opens the gate for a clear yes. WHERE is that guard, and UNKNOWN is the shrug.

Watch it happen

What WHERE does with each answer

Row with city = NULL
city = 'Delhi' ?
TRUE: keep
FALSE: drop
UNKNOWN: drop

1/4A row with no city reaches the WHERE test.

The big ideas

What you need to know

Three answers, not two

Any comparison with NULL gives UNKNOWN, including NULL = NULL.

WHERE keeps only TRUE

UNKNOWN is treated like FALSE by WHERE, so NULL rows fall out of both = and <> filters.

AND and OR rules

FALSE AND UNKNOWN is FALSE. TRUE OR UNKNOWN is TRUE. Otherwise UNKNOWN usually stays UNKNOWN.

Plan for NULLs

Add OR city IS NULL when you want them, or use IS DISTINCT FROM in PostgreSQL for NULL-safe comparison.

See it in code

Walk through the code

sql
1-- Three filters that should cover everyone2SELECT COUNT(*) FROM customers WHERE city = 'Delhi';3SELECT COUNT(*) FROM customers WHERE city <> 'Delhi';4SELECT COUNT(*) FROM customers WHERE city IS NULL;5 6-- Everyone not in Delhi, including unknown cities7SELECT customer_id, city8FROM customers9WHERE city <> 'Delhi' OR city IS NULL;10 11-- PostgreSQL NULL-safe version of the same thing12SELECT customer_id, city13FROM customers14WHERE city IS DISTINCT FROM 'Delhi';

1/3Delhi plus not-Delhi misses the NULL cities. The third count finds them.

The first two counts plus the third equal the total. IS DISTINCT FROM is standard SQL and works in PostgreSQL and SQL Server 2022+. MySQL uses NOT (city <=> 'Delhi').

Save yourself the pain

Common mistakes

Oops

Assuming WHERE x = 'A' and WHERE x <> 'A' together return every row.

Do this instead

Rows where x is NULL appear in neither. Count them separately with IS NULL.

Oops

Expecting NULL = NULL to be TRUE.

Do this instead

It's UNKNOWN. Use IS NULL, or IS NOT DISTINCT FROM to compare two columns that might both be NULL.

Your mission

Find the missing rows

~20 min

Quick check

What does TRUE OR UNKNOWN evaluate to?

Go deeper

  1. 1Count rows where status = 'active', where status <> 'active' and where status IS NULL, and check they add up to the total only with all three.
  2. 2Work through how NOT, AND and OR behave with UNKNOWN using a small truth table.
  3. 3Use COALESCE or an explicit IS NULL check when NULL rows should be included.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge