SQL NULL Equality: Why DECODE and Compound Keys Match Two NULLs
Standard comparisons return UNKNOWN for NULL operands, yet Oracle's DECODE and its compound-key rules treat two NULLs as matching. This article explains the split and where it stops applying.
On this page
Key idea
In Oracle, a predicate such as NULL = NULL evaluates to UNKNOWN, not TRUE, because NULL means the value is unknown; a WHERE clause keeps only TRUE, so those rows disappear. DECODE is a value-returning function that must choose one branch, and Oracle documents that it considers two nulls equal when matching. Compound-key comparison is a separate identity rule: keys are identical when all non-null components are equal. These are context-specific matching rules, not a general NULL equality, and they do not port unchanged to other SQL engines.
Context and Recommendation
The same pair of values can produce opposite-looking answers. Oracle's documentation states that because null represents a lack of data, a null cannot be equal or unequal to any value or to another null, and that only IS NULL and IS NOT NULL should be used to test for nulls. The same page then states that Oracle considers two nulls equal when evaluating DECODE, and that it considers compound keys containing nulls identical when all non-null components of the keys are equal.
The useful mental model is that these are two layers rather than a contradiction: predicates run on three-valued logic, while particular constructs define their own matching or identity rules. The practical recommendation is to treat null equality in DECODE and in compound keys as an Oracle-specific exception, and to write explicit null handling whenever the same SQL must run in another engine or be reviewed by a mixed-dialect team.
Reasoning and Trade-offs
Three-valued logic exists because NULL stands for missing or unknown data. A comparison whose result depends on a null yields UNKNOWN, and a WHERE clause returns rows only when the condition is TRUE. The asymmetry matters: NOT FALSE evaluates to TRUE, but NOT UNKNOWN evaluates to UNKNOWN, so negating a null comparison does not recover the missing rows. Oracle therefore directs users to IS NULL and IS NOT NULL, which are tests for presence rather than value equality.
DECODE works differently because it is a function that must return exactly one branch. It needs a decisive match rule instead of an UNKNOWN outcome, and Oracle defines a null-to-null match as successful. Compound-key comparison is again about tuple identity for lookup and uniqueness, expressed through the non-null components, not a boolean claim that null equals null.
The trade-off is convenience versus portability. In SQL Server, with SET ANSI_NULLS ON, an operator with one or two NULL expressions returns UNKNOWN. With SET ANSI_NULLS OFF, the equals and not-equals operators treat NULL as a known value equivalent to any other NULL and return only TRUE or FALSE. That OFF behavior is a documented exception to the normal rules, so code depending on it must control the session setting explicitly rather than assume it.
Concrete Illustration
The example runs in Oracle Database 21c through SQL*Plus or SQLcl with SERVEROUTPUT enabled and a schema that can create tables. It seeds three rows, then contrasts the predicate result with DECODE, and finally probes a composite primary key. The output below is illustrative, not observed.
For the comparison query, row 1 (both values NULL) gives predicate_result 'Not equal or unknown' but decode_result 'Match'; row 2 ('A','A') gives 'Equal' and 'Match'; row 3 ('A','B') gives 'Not equal or unknown' and 'No Match'. The COUNT query returns 1, because only row 2 satisfies col1 = col2. The procedural blocks print 'duplicate composite key rejected' for the repeated ('A','B') key and 'null key rejected: primary key columns are NOT NULL' for the all-null key, since Oracle applies NOT NULL to every primary key column. Error wording varies by version, so the block prints stable messages instead of relying on a specific error text.
SET SERVEROUTPUT ON
CREATE TABLE t_demo (
id NUMBER,
col1 VARCHAR2(10),
col2 VARCHAR2(10)
);
INSERT INTO t_demo VALUES (1, NULL, NULL);
INSERT INTO t_demo VALUES (2, 'A', 'A');
INSERT INTO t_demo VALUES (3, 'A', 'B');
COMMIT;
SELECT id,
CASE WHEN col1 = col2 THEN 'Equal'
ELSE 'Not equal or unknown'
END AS predicate_result,
DECODE(col1, col2, 'Match', 'No Match') AS decode_result
FROM t_demo
ORDER BY id;
SELECT COUNT(*) AS matching_rows
FROM t_demo
WHERE col1 = col2;
CREATE TABLE cmp_demo (
k1 VARCHAR2(10),
k2 VARCHAR2(10),
CONSTRAINT cmp_demo_pk PRIMARY KEY (k1, k2)
);
INSERT INTO cmp_demo VALUES ('A', 'B');
BEGIN
INSERT INTO cmp_demo VALUES ('A', 'B');
DBMS_OUTPUT.PUT_LINE('unexpected: duplicate accepted');
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('duplicate composite key rejected');
END;
/
BEGIN
INSERT INTO cmp_demo VALUES (NULL, NULL);
DBMS_OUTPUT.PUT_LINE('unexpected: null key accepted');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('null key rejected: primary key columns are NOT NULL');
END;
/Applicability Limits
The behavior described is Oracle-specific and is documented for Oracle Database 21c; standard SQL does not guarantee null equality in DECODE or in compound keys, and other engines may differ. SQL Server reaches similar equality only under SET ANSI_NULLS OFF, which is a legacy compatibility path, so migration code should not assume it. PostgreSQL, MySQL and others generally keep null comparisons UNKNOWN in predicates, and their unique constraints typically allow multiple rows containing nulls.
Two Oracle details further bound the claim. A PRIMARY KEY constraint makes every key column NOT NULL, so the all-null composite key cannot be inserted at all; the documented compound-key rule applies where nulls are permitted in a key context, not as a way to seed duplicate-looking primary keys. Oracle also treats a character value of length zero as null today, while recommending that empty strings not be treated as nulls in future work, which affects any sentinel-based equality you write.
Applicability
- Does the construct under test produce a boolean predicate (WHERE, HAVING, join ON) or a value from a function such as DECODE?
- Will the same SQL run outside Oracle, or is Oracle-only behavior acceptable and covered by tests?
- In SQL Server, is SET ANSI_NULLS ON or OFF for the session and for any stored T-SQL code?
- Are the key columns nullable, and does the constraint actually permit NULLs, given that Oracle PRIMARY KEY implies NOT NULL?
- Does any report or migration depend on DECODE's null-to-null match instead of an explicit IS NULL branch or sentinel?
- Is an empty string involved, since Oracle currently treats a zero-length character value as null?
- Were duplicate-key and null-key outcomes tested on an isolated copy with seeded rows rather than assumed from syntax?
- Does the target engine's unique constraint allow multiple rows containing nulls, which would change the intended outcome?
Where this applies
This article covers Oracle Database 21c documented null semantics and the SQL Server ANSI_NULLS comparison behavior; it does not establish rules for every DBMS or version. The compound-key statement is a documented Oracle rule whose observable effect depends on constraint configuration, and Oracle primary keys cannot hold nulls at all. The example output is illustrative rather than observed, database error wording varies by release, and no performance or execution-plan claims are made.