TATECHATLAS
◎ English
Data & databases / Guide

SQL NULL Comparison: Why = NULL Fails in WHERE and IS NULL Is the Only Correct Test

SQL uses three-valued logic where NULL comparisons return UNKNOWN rather than TRUE or FALSE, causing WHERE clauses to silently drop rows. Only IS NULL and IS NOT NULL reliably test for missing values across all standard SQL engines.

On this page

In standard SQL, any comparison operator (=, !=, <, >, <>) applied to NULL produces UNKNOWN, not TRUE or FALSE. A WHERE clause excludes rows whose condition evaluates to UNKNOWN, so WHERE status = NULL returns zero rows even when status is actually NULL. The correct pattern is WHERE status IS NULL or WHERE status IS NOT NULL. Wrapping the failed comparison in NOT does not help because NOT UNKNOWN still evaluates to UNKNOWN. SQL Server with SET ANSI_NULLS OFF deviates from this rule, but relying on IS NULL and IS NOT NULL keeps queries portable regardless of session configuration.

What NULL Actually Represents in SQL

NULL is not zero, not an empty string, and not a placeholder for a known missing value. It represents the absence of data or an unknown value. Oracle documentation states that you should use a null when the actual value is not known or when a value would not be meaningful. This semantic choice is the root cause of all subsequent comparison behavior: because NULL means unknown, the database engine cannot determine whether it equals or differs from any other value, including another NULL.

A critical consequence is that any arithmetic expression containing NULL always evaluates to NULL. All operators except concatenation return NULL when given a NULL operand. This means NULL propagates through expressions, and a comparison involving a NULL operand cannot produce a definite TRUE or FALSE result.

> Oracle Database treats a character value with a length of zero as null. However, do not use null to represent a numeric value of zero, because they are not equivalent.

If you inherit a codebase that uses = NULL or != NULL and appears to work, check whether SET ANSI_NULLS OFF is configured at the session, database, or connection level. That setting is deprecated in SQL Server and will eventually be removed, so migrate to IS NULL and IS NOT NULL before the behavior changes.

The Only Correct Test: IS NULL and IS NOT NULL

Oracle documentation is explicit: to test for nulls, use only the comparison conditions IS NULL and IS NOT NULL. These are the sole operators designed to evaluate whether a value is absent. Every other comparison condition, when applied to a NULL operand, produces UNKNOWN rather than a boolean result.

The reason IS NULL works is that it does not attempt to compare values. It tests the metadata property of the operand: whether the value slot is empty. This is a fundamentally different operation from equality, which asks whether two values represent the same datum.

> To test for nulls, use only the comparison conditions IS NULL and IS NOT NULL. If you use any other condition with nulls and the result depends on the value of the null, then the result is UNKNOWN.

Why = NULL and != NULL Always Return UNKNOWN

The Oracle truth table for conditions containing nulls shows that a = NULL evaluates to UNKNOWN whether a is 10 or NULL. Similarly, a != NULL is UNKNOWN in both cases. The same applies to a = 10 and a != 10 when a is NULL: all four combinations yield UNKNOWN.

This is not a bug or an implementation quirk. It follows directly from the definition: because NULL represents a lack of data, a null cannot be equal or unequal to any value or to another null. The engine has no information to make a determination, so it returns the third truth value.

-- Truth table from Oracle documentation (Table 2-22)
-- Condition        Value of A    Evaluation
-- a IS NULL        10            FALSE
-- a IS NOT NULL    10            TRUE
-- a IS NULL        NULL          TRUE
-- a IS NOT NULL    NULL          FALSE
-- a = NULL         10            UNKNOWN
-- a != NULL        10            UNKNOWN
-- a = NULL         NULL          UNKNOWN
-- a != NULL        NULL          UNKNOWN
-- a = 10           NULL          UNKNOWN
-- a != 10          NULL          UNKNOWN

How UNKNOWN in WHERE Silently Drops Rows

The practical consequence is severe and often invisible during development. A condition that evaluates to UNKNOWN acts almost like FALSE in a WHERE clause. A SELECT statement with a condition that evaluates to UNKNOWN returns no rows. There is no warning, no error, and no indication that the filter behaved unexpectedly.

This means a query like SELECT * FROM orders WHERE status = NULL returns an empty result set even if the table contains rows where status is genuinely NULL. The developer sees zero rows and may conclude there are no matching records, when in fact the comparison itself was undefined.

-- Returns zero rows even when status IS NULL in the table
SELECT * FROM orders WHERE status = NULL;

Why NOT UNKNOWN Is Still UNKNOWN, Not TRUE

A common instinct is to negate the failed comparison: NOT (status != NULL) should logically give us the rows where status is NULL. However, Oracle documentation states that NOT FALSE evaluates to TRUE, but NOT UNKNOWN evaluates to UNKNOWN. The negation operator cannot rescue an undefined comparison.

This means the pattern WHERE NOT (status != NULL) also returns zero rows. The UNKNOWN propagates through the NOT operator, and the WHERE clause still excludes the row. The only way to get a definite boolean result is to use IS NULL or IS NOT NULL directly.

-- NOT UNKNOWN is still UNKNOWN, so this also returns zero rows
SELECT * FROM orders WHERE NOT (status != NULL);

SQL Server SET ANSI_NULLS: When = NULL Can Return TRUE or FALSE

SQL Server provides a session-level setting that overrides standard three-valued logic. When SET ANSI_NULLS is ON (the default and the behavior conforming to the ISO standard), an operator with one or two NULL expressions returns UNKNOWN. When SET ANSI_NULLS is OFF, the equals and not-equals operators treat NULL as a known value equivalent to any other NULL, returning TRUE or FALSE instead of UNKNOWN.

This creates a dialect-dependent behavior where the same query text produces different results depending on session configuration. Microsoft's broader documentation on SET ANSI_NULLS indicates that the OFF behavior is deprecated and will be removed in a future release, making code that depends on it fragile. The excerpt above confirms the behavioral difference but does not itself state the deprecation timeline.

> When SET ANSI_NULLS is OFF, these operators treat NULL as a known value, equivalent to any other NULL, and only return TRUE or FALSE (never UNKNOWN).

Decision Rule: Always Use IS NULL and IS NOT NULL Regardless of Setting

Even when ANSI_NULLS is OFF and = NULL appears to work, relying on IS NULL and IS NOT NULL keeps queries portable, predictable, and independent of session-level configuration. The IS NULL pattern is standard SQL, supported identically across Oracle, SQL Server, PostgreSQL, and MySQL without any session variable.

The decision criterion is simple: if a query must run correctly regardless of who connects, what session settings are active, or which database engine hosts the data, use IS NULL and IS NOT NULL. There is no scenario where the comparison-operator approach is safer or more correct.

-- The correct way, portable across all engines and settings
SELECT * FROM orders WHERE status IS NULL;
SELECT * FROM orders WHERE status IS NOT NULL;

Limits and Edge Cases to Watch

Oracle documents two exceptions where two NULLs are considered equal: inside the DECODE function and in compound keys. If all non-null components of two compound keys are equal and both contain NULLs in the same positions, Oracle considers the keys identical. These are narrow, function-specific behaviors and do not change the WHERE-clause rule.

Additionally, Oracle currently treats a zero-length character value as NULL, though the documentation warns this may not continue in future releases. Do not rely on empty strings and NULLs being interchangeable. The scope of this guide covers WHERE-clause filtering only; NULL behavior in JOIN conditions, aggregate functions, and window functions involves additional rules not addressed here.

> Oracle also considers two nulls to be equal if they appear in compound keys. That is, Oracle considers identical two compound keys containing nulls if all the non-null components of the keys are equal.

Things to check

  • IS NULL and IS NOT NULL are the only comparison conditions that return TRUE or FALSE when the operand is NULL; all other operators return UNKNOWN.
  • A WHERE clause excludes rows whose condition evaluates to UNKNOWN, so WHERE col = NULL returns zero rows regardless of the actual data.
  • NOT UNKNOWN evaluates to UNKNOWN, not TRUE, so negating a failed NULL comparison does not recover the missing rows.
  • SET ANSI_NULLS OFF in SQL Server makes = NULL return TRUE or FALSE, but this setting is deprecated and non-portable.
  • Oracle treats two NULLs as equal inside DECODE and in compound keys, but these exceptions do not apply to WHERE-clause comparison operators.

This guide covers NULL comparison semantics in WHERE clauses using standard three-valued logic and the SQL Server ANSI_NULLS deviation. It does not cover NULL behavior in JOIN conditions, aggregate functions, window functions, or the specific handling in MySQL and PostgreSQL, which may have their own extensions or defaults. Oracle-specific notes about DECODE and compound-key equality are mentioned as edge cases but are not the focus of the filtering pattern discussed here.

Sources

  1. Oracle Database: Nulls ↗
  2. Microsoft SQL Server: Comparison Operators ↗
Back to top ↑