TATECHATLAS
◎ English
Data & databases / Tip

Why NOT UNKNOWN is not TRUE and causes silent row loss in SQL

In SQL's three-valued logic, NOT UNKNOWN remains UNKNOWN. This causes WHERE clauses to silently exclude rows with NULL values when using negated comparisons, leading to incomplete result sets.

On this page

In SQL's three-valued logic, the NOT operator does not convert an UNKNOWN result into TRUE. When a comparison involves a NULL value, the result is UNKNOWN. Applying NOT to UNKNOWN still yields UNKNOWN. Because the WHERE clause only includes rows where the condition evaluates strictly to TRUE, any row that results in UNKNOWN is silently excluded from the result set. This creates a logical gap where the negation of a condition does not capture the full complement of the original set, specifically missing the rows containing NULL values.

NOT UNKNOWN stays UNKNOWN, so negated filters also drop NULL rows

SQL uses three-valued logic, meaning a comparison can result in TRUE, FALSE, or UNKNOWN. When an operand is NULL, any comparison operator (except concatenation in some dialects) returns UNKNOWN. This represents a lack of data rather than a definitive false statement.

The NOT operator follows specific rules: NOT FALSE evaluates to TRUE, and NOT TRUE evaluates to FALSE. However, NOT UNKNOWN remains UNKNOWN. Because the WHERE clause only permits rows where the predicate is TRUE, any row that evaluates to UNKNOWN is discarded without error or warning.

-- Scenario: A table with a nullable status column
-- Row 1: status = 'active' -> TRUE
-- Row 2: status = 'inactive' -> TRUE
-- Row 3: status = NULL -> UNKNOWN

-- Using NOT on a comparison:
SELECT * FROM orders WHERE NOT (status = 'active');

-- Resulting logic for Row 3:
-- 1. status = 'active' -> UNKNOWN
-- 2. NOT (UNKNOWN) -> UNKNOWN
-- 3. WHERE UNKNOWN -> Row excluded

Why this compounds the = NULL problem and what the trade-off is

Developers often use negation to find the 'complement' of a set. If you want everything except 'active' orders, the intuitive thought is to use NOT (status = 'active'). However, this logic fails to capture the NULL rows, resulting in a subset that is smaller than the actual mathematical complement.

The trade-off is between code conciseness and logical completeness. Using negation is shorter but carries the risk of silent data loss. Using explicit IS NOT NULL or IS NULL guards is more verbose but ensures that the developer is making a conscious decision about how to handle missing data.

-- The 'lossy' approach (concise but misses NULLs)
SELECT * FROM orders WHERE status <> 'active';

-- The ''safe' approach (verbose but complete)
SELECT * FROM orders WHERE status <> 'active' OR status IS NULL;

Concrete example: NOT (status = 'active') excludes NULL statuses

Consider an orders table with three rows: one active, one inactive, and one with a NULL status. If we want to find all orders that are not active, we might expect two rows back (inactive and NULL).

In practice, the query below only returns the inactive row. The NULL row is lost because the comparison 'NULL = active' is UNKNOWN, and 'NOT UNKNOWN' is still UNKNOWN, which the WHERE clause treats as a failure to meet the criteria.

-- Setup (Prerequisites: SQL environment with orders table)
-- CREATE TABLE orders (id INT, status VARCHAR(20));
-- INSERT INTO orders VALUES (1, 'active'), (2, 'inactive'), (3, NULL);

-- Illustrative Output for: SELECT id, status FROM orders WHERE NOT (status = 'active');
-- id | status
-- ----|-------
-- 2   | inactive

-- Illustrative Output for: SELECT id, status FROM orders WHERE status <> 'active' OR status IS NULL;
-- id | status
-- ----|-------
-- 2   | inactive
-- 3   | NULL

When this matters and when it does not

The risk of silent data loss is highest when filtering on nullable columns using inequality (<>,!=) or range operators (<, >, <=, >=). In these cases, the UNKNOWN result is highly likely to occur for any missing data.

This issue is non-existent for columns defined with a NOT NULL constraint or as part of a PRIMARY KEY, as these columns are guaranteed to never produce an UNKNOWN result during comparison. Additionally, while SET ANSI_NULLS OFF in SQL Server changes how = and <> behave, it is generally recommended to keep it ON to maintain standard SQL compliance and avoid unexpected behavior in complex queries.

Applicability

  • Does the column being filtered allow NULL values?
  • Does the WHERE clause use negation (NOT or <>) on a nullable column?
  • Have you explicitly handled NULLs using IS NULL or IS NOT NULL?
  • Is the column constrained by a NOT NULL or PRIMARY KEY constraint?

This behavior follows standard SQL three-valued logic. However, exceptions exist: Oracle's DECODE function and compound-key equality rules treat two NULLs as equal. In SQL Server, setting ANSI_NULLS OFF changes the behavior of = and <> to treat NULL as a known value, but this does not eliminate UNKNOWN results for other operators. This logic applies to standard comparison and range operators.

Sources

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