MODULE 2 OF 8: FILTERING AND TRANSFORMING • LESSON 4 OF 8~16 min read

NULL and Three-Valued Logic

NULL represents a missing, unavailable or unknown value—not zero and not an empty string. Understanding SQL's three-valued logic is essential for writing correct filters, aggregates and anti-joins.

•

NULL represents a missing, unavailable or unknown value—not zero and not an empty string. Understanding SQL's three-valued logic is essential for writing correct filters, aggregates and anti-joins.

Key Takeaways at a Glance:

  • •NULL represents missing, unknown, or inapplicable information; it is NOT zero, empty string, or false.
  • •Equality comparisons (col = NULL) always evaluate to UNKNOWN; use 'IS NULL' or 'IS NOT NULL'.
  • •Three-valued logic rules: TRUE AND UNKNOWN = UNKNOWN, TRUE OR UNKNOWN = TRUE, NOT UNKNOWN = UNKNOWN.
  • •COALESCE(val1, val2, ...) returns the first non-NULL expression from its argument list.

In Simple Words (The Mental Model)

In SQL, NULL means 'I don't know'. If you ask 'Is this mystery box equal to 100?', the only logical answer is 'I don't know' (UNKNOWN). If you ask 'Is mystery box A equal to mystery box B?', the answer is still 'I don't know'. That's why two NULLs are never equal to each other in SQL.

Real-World Analogy

Think of an anonymous wrapped gift box. You don't know what is inside (NULL). If someone asks 'Does this box contain a watch?', you cannot answer Yes or No—you answer 'I don't know'. If you have two wrapped boxes, you cannot say they have the same gift. To test if the box is wrapped, you don't look inside, you simply check 'Is the wrapping paper present?' (IS NULL).

Foundational Principles

Core Concept Breakdown

Essential database invariants, relational theory, and execution behavior.

1What NULL means

A NULL value indicates that a value is absent or unknown according to the data model. It is different from numeric zero, an empty string and the text 'NULL'. The reason for a NULL matters: it might mean not yet known, not applicable, or not provided. Queries should handle that meaning intentionally rather than replacing every NULL with zero.

Free Preview Limit Reached (2/2 Lessons Used)

Sign In to Unlock Full Lesson Content

Visitors are limited to 2 free preview lessons. Create a free StudyVex account to unlock all 38 comprehensive theory chapters, runnable query walkthroughs, visual input & result tables, and interactive placement quizzes.