0Pricing
SQL Academy · Lesson

NULL: The Third Truth Value

Master the meaning of NULL, why NULL is not equal to anything (not even itself), and how three-valued logic affects WHERE clauses.

NULL: The Third Truth Value is a free SQL Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is NULL?

NULL represents unknown or missing data. It is not zero, not an empty string, not false. It is the explicit absence of a value.

NULL Is Not Equal to Anything (Not Even Itself)

This is the rule that trips everyone up:

SELECT NULL = NULL;       -- NULL (not true!)
SELECT NULL = 0;          -- NULL
SELECT NULL = '';        -- NULL
SELECT NULL <> 1;         -- NULL

Use IS NULL and IS NOT NULL

To test for NULL, you must use the special predicates IS NULL and IS NOT NULL:

SELECT email FROM users WHERE deleted_at IS NULL;       -- active users
SELECT email FROM users WHERE deleted_at IS NOT NULL;   -- deleted users

Three-Valued Logic

SQL boolean logic has THREE values: TRUE, FALSE, and UNKNOWN (NULL).

ABA AND BA OR B
TRUENULLNULLTRUE
FALSENULLFALSENULL
NULLNULLNULLNULL

WHERE Drops Rows Where the Predicate Is NULL

If WHERE x = 5 evaluates to NULL for a row (because x is NULL), that row is NOT returned. Only rows where the predicate is TRUE are kept.

-- Suppose age is NULL for some users:
SELECT * FROM users WHERE age >= 18;
-- Users with NULL age are NOT returned, even though we
-- don't actually know they're under 18.

The NOT IN NULL Trap

This query returns ZERO rows if any name in the subquery is NULL:

SELECT * FROM users
WHERE name NOT IN (SELECT excluded_name FROM blocklist);
-- If blocklist has a NULL row, the whole NOT IN evaluates to NULL
-- → no rows returned

-- Safer:
SELECT * FROM users u
WHERE NOT EXISTS (
  SELECT 1 FROM blocklist b WHERE b.excluded_name = u.name
);

COALESCE: Replace NULL with a Default

COALESCE returns the first non-NULL argument:

SELECT id, COALESCE(full_name, email, 'Anonymous') AS display_name
FROM users;

-- If full_name is NULL, use email; if that's NULL too, use 'Anonymous'.

NULLIF: The Opposite

NULLIF(a, b) returns NULL when a = b, otherwise a. Handy for treating sentinel values like '' or 0 as NULL.

SELECT NULLIF(price, 0) FROM products;
-- Returns NULL when price is 0, useful for AVG() that should ignore zeros.

NULL in Aggregates

Aggregate functions (except COUNT(*)) skip NULL inputs:

-- Suppose:  prices = [10, 20, NULL, 30]
SELECT COUNT(*)     FROM products;  -- 4 (counts rows)
SELECT COUNT(price) FROM products;  -- 3 (counts non-NULL prices)
SELECT AVG(price)   FROM products;  -- 20  (60 / 3, not 60 / 4)
SELECT SUM(price)   FROM products;  -- 60

NULL Ordering

By default, PostgreSQL sorts NULLs last when ascending and first when descending. You can override this:

SELECT * FROM users ORDER BY last_login_at DESC NULLS LAST;

Storing NULL vs Empty String

Decide for each column: is '' a valid value? If not, use NULL to mean "no value". If yes, NULL means something different (e.g. "not asked yet").

Be consistent — don't mix both in the same column.

Recap

NULL is its own truth value.

  • Test with IS NULL, never = NULL
  • WHERE filters keep only TRUE rows
  • Aggregates skip NULL inputs
  • COALESCE is your friend

Quick Check

What does SELECT NULL = NULL; return?

Frequently asked questions

Is the “NULL: The Third Truth Value” lesson free?

Yes — the full text of “NULL: The Third Truth Value” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.

What will I learn in “NULL: The Third Truth Value”?

Master the meaning of NULL, why NULL is not equal to anything (not even itself), and how three-valued logic affects WHERE clauses. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SQL Academy?

No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “NULL: The Third Truth Value” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SQL Academy lesson?

Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What an RDBMS Is and Why Tables Matter
  2. Primary Keys and Uniqueness
  3. NULL: The Third Truth Value
  4. Data Types Overview: INT VARCHAR DATE BOOLEAN
← Back to SQL Academy