TATECHATLAS
◎ English
Data & databases / Guide

SQL NULL Semantics: Arithmetic Propagation, COUNT Differences, and DECODE Equality

NULL propagates through arithmetic, COUNT(*) differs from COUNT(column) by ignoring nulls, and Oracle DECODE uniquely treats two NULLs as equal while standard comparisons return UNKNOWN.

On this page

NULL is not a value but the absence of a known value. Arithmetic with NULL returns NULL because the engine cannot compute a result from an unknown operand. COUNT(*) counts all rows while COUNT(column) excludes rows where that column is NULL. Oracle DECODE is a deliberate exception that treats two NULLs as equal, unlike the = operator which returns UNKNOWN. SQL Server ANSI_NULLS setting controls whether NULL comparisons return UNKNOWN or TRUE/FALSE.

What NULL Represents and Why It Propagates Through Arithmetic

NULL represents the absence of a known value. It is not zero, not an empty string, and not a placeholder for any specific datum. Because the value is genuinely unknown, the database engine cannot compute a meaningful result from arithmetic involving it. Oracle documentation states that any arithmetic expression containing a null always evaluates to null, and that all operators except concatenation return null when given a null operand. This propagation rule means that adding, subtracting, multiplying, or dividing by NULL produces NULL regardless of the other operand. The engine refuses to guess, so the result remains unknown.

-- Demonstrate arithmetic propagation with NULL
SELECT 10 + NULL AS addition_result,
       10 * NULL AS multiplication_result,
       'hello' || NULL AS concatenation_result
FROM dual;
-- addition_result: NULL
-- multiplication_result: NULL
-- concatenation_result: 'hello' (concatenation is the sole exception)

When porting queries between Oracle and SQL Server, always check the ANSI_NULLS session setting. Queries written under ANSI_NULLS OFF will silently return different row counts when executed under ANSI_NULLS ON, because equality comparisons with NULL shift from TRUE/FALSE to UNKNOWN. Use explicit IS NULL predicates instead of = NULL to avoid this class of portability bugs entirely.

Three-Valued Logic: TRUE, FALSE, and UNKNOWN in Comparisons

SQL comparison operators do not produce a simple boolean result when NULL is involved. Instead, they return a third truth value called UNKNOWN. This is a deliberate design choice: since NULL means the value is not known, the engine cannot assert whether it equals or does not equal another value. A WHERE clause that evaluates to UNKNOWN filters out the row, behaving like FALSE for row selection. However, NOT UNKNOWN remains UNKNOWN rather than becoming TRUE, which distinguishes three-valued logic from two-valued boolean logic. Oracle documents this explicitly: a condition evaluating to UNKNOWN acts almost like FALSE in a WHERE clause, but further operations on UNKNOWN still yield UNKNOWN.

-- Three-valued logic demonstration
-- Suppose commission is NULL for a given row:
SELECT CASE
         WHEN commission = NULL THEN 'equal'
         WHEN commission != NULL THEN 'not equal'
         ELSE 'unknown'
       END AS comparison_result
FROM employees
WHERE employee_id = 100;
-- Always returns 'unknown' because = and != with NULL produce UNKNOWN

COUNT(*) Versus COUNT(expression): The Mechanism Behind the Difference

COUNT(*) and COUNT(column) differ because they operate on different things. COUNT(*) asks the engine to count row occurrences in the current group or result set, without inspecting any specific column. COUNT(column) evaluates the expression for each row and counts only the rows where the result is not NULL. The exclusion of NULL values follows directly from the aggregate function specification: aggregate functions ignore null inputs. So if a column has 10 rows but 4 contain NULL, COUNT(column) returns 6 while COUNT(*) returns 10. This is not a special case but a direct consequence of the same propagation rule that makes arithmetic with NULL return NULL.

-- Concrete worked example
-- Assume department_id 30 has 10 employees, 4 with NULL commission
SELECT COUNT(*) AS all_rows,
       COUNT(commission) AS has_commission,
       COUNT(*) - COUNT(commission) AS missing_commission
FROM employees
WHERE department_id = 30;
-- all_rows: 10 (counts every row regardless of column content)
-- has_commission: 6 (counts only non-NULL commission values)
-- missing_commission: 4 (derived difference)

DECODE as an Exception: Oracle Treats Two NULLs as Equal

Oracle DECODE is a deliberate exception to the general rule that NULL cannot equal another NULL. When DECODE compares its first argument against its second argument and both are NULL, Oracle treats them as equal and selects the corresponding result branch. This allows DECODE to function as a multi-branch conditional that includes a null-matching case. The standard comparison operator = would return UNKNOWN in the same situation, making it impossible to filter or branch on NULL using equality alone. Oracle documentation confirms this behavior explicitly as a design choice specific to DECODE semantics.

-- DECODE null-matching behavior
SELECT employee_id,
       DECODE(commission, NULL, 'No commission', commission) AS commission_display
FROM employees
WHERE department_id = 30;
-- For rows where commission IS NULL, DECODE matches NULL against NULL
-- and returns the string 'No commission'.
-- The equivalent WHERE commission = NULL would return UNKNOWN and no rows.

SET ANSI_NULLS in SQL Server: A Switch That Changes Comparison Semantics

SQL Server provides a session-level setting called SET ANSI_NULLS that changes comparison semantics. When ANSI_NULLS is ON, the standard three-valued logic applies: any comparison operator with one or two NULL expressions returns UNKNOWN. When ANSI_NULLS is OFF, the equals and not-equals operators treat NULL as a known value equivalent to any other NULL, returning only TRUE or FALSE and never UNKNOWN. This creates a dialect-specific pitfall when porting queries between Oracle and SQL Server, because a query written under ANSI_NULLS OFF will produce different row counts when executed under ANSI_NULLS ON.

-- SQL Server: demonstrate ANSI_NULLS effect
SET ANSI_NULLS OFF;
SELECT employee_id FROM employees WHERE commission = NULL;
-- Returns rows where commission IS NULL (NULL treated as equal to NULL)

SET ANSI_NULLS ON;
SELECT employee_id FROM employees WHERE commission = NULL;
-- Returns zero rows (comparison evaluates to UNKNOWN, filtered out)

Compound Keys and Null Equality in Constraints

Oracle considers two compound keys containing nulls to be identical if all non-null components of the keys are equal. This is another exception to the general rule that NULL cannot equal another NULL, and it is relevant when understanding why certain unique constraints behave unexpectedly with nullable columns. For example, a unique constraint on columns (a, b) will reject two rows where a is 1 and b is NULL, because the non-null component matches and the null components are considered equal. This demonstrates that null-equality semantics vary by context within the same database engine.

-- Oracle compound key null-equality
CREATE TABLE test_keys (a NUMBER, b NUMBER, CONSTRAINT uk_ab UNIQUE (a, b));
INSERT INTO test_keys VALUES (1, NULL);
INSERT INTO test_keys VALUES (1, NULL);
-- Second INSERT fails: Oracle considers the two compound keys identical
-- because the non-null component (a=1) matches and NULLs are treated as equal

Practical Decision Rule: When to Use IS NULL, COALESCE, or DECODE

Use IS NULL and IS NOT NULL for filtering because they are the only comparison conditions that produce TRUE or FALSE when the operand is NULL. Use COALESCE or NVL for arithmetic substitution when you need to replace NULL with a known value before computing. Use DECODE when you specifically need null-matching branch logic in Oracle, since it is the only construct that treats two NULLs as equal in a comparison context. Mixing these approaches without understanding the underlying semantics leads to silently wrong results rather than explicit errors, because UNKNOWN evaluates to no rows in a WHERE clause without raising any exception.

-- Decision framework in practice
-- Filtering: use IS NULL
SELECT * FROM employees WHERE commission IS NULL;
-- Arithmetic substitution: use COALESCE (portable) or NVL (Oracle)
SELECT salary + COALESCE(commission, 0) AS total_comp FROM employees;
-- Null-matching branch: use DECODE (Oracle-specific)
SELECT DECODE(commission, NULL, 0, commission) AS safe_commission FROM employees;

Limitations and Portability Cautions

The Oracle behavior of treating a character value with a length of zero as null is documented as potentially changing in future releases, so code that relies on empty-string-equals-null should be reviewed during upgrades. SQL Server ANSI_NULLS default may vary by client driver and connection method, meaning the same query can behave differently depending on how the connection was established. This guide covers only Oracle and SQL Server null semantics. Readers working with PostgreSQL or MySQL should verify dialect-specific behavior before assuming these rules transfer, because each engine has its own documented exceptions and defaults.

Things to check

  • COUNT(*) returns the total number of rows in the group, while COUNT(column) returns only the count of non-NULL values in that column
  • Arithmetic with NULL always returns NULL in Oracle; concatenation is the sole operator exception
  • DECODE treats two NULLs as equal, unlike the = operator which returns UNKNOWN
  • SET ANSI_NULLS OFF in SQL Server makes = and != with NULL return TRUE or FALSE instead of UNKNOWN
  • Oracle considers two compound keys with matching non-null components and NULL in the same position as identical for unique constraints
  • NOT UNKNOWN evaluates to UNKNOWN, not TRUE, distinguishing three-valued logic from two-valued logic
  • COALESCE and NVL replace NULL before arithmetic, preventing propagation of unknown results
  • PostgreSQL and MySQL null semantics are not covered and may differ from Oracle and SQL Server

This article covers only Oracle and SQL Server null semantics. DECODE null-equality is Oracle-specific and has no direct equivalent in SQL Server CASE expressions. Oracle empty-string-as-null behavior is documented as potentially changing in future releases. SQL Server ANSI_NULLS defaults vary by client driver. PostgreSQL and MySQL have their own rules not addressed here.

Sources

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