Sub topic 1
What NULL Means in Oracle
Oracle uses NULL to represent a missing, unknown, or inapplicable value. It is not the same as zero, an empty string, or a blank space. For example, a missing commission percentage means Oracle does not know whether the employee has a commission, or the commission does not apply.
This chapter assumes you know basic SELECT statements, WHERE clauses, Oracle data types, the DUAL table, and aggregate functions such as COUNT, SUM, and AVG.
Prerequisites
You should be able to read a query such as:
SELECT employee_id, salary
FROM employees
WHERE department_id = 50;You should also recognize common Oracle types such as NUMBER, VARCHAR2, and DATE.
NULL Is Not a Value
A value such as 0 is known. An empty string is a character value in many database systems. NULL means that no value is available for the expression.
This distinction matters because Oracle cannot decide that two unknown values are equal. The expression NULL = NULL does not return TRUE. It returns UNKNOWN.
SQL uses three-valued logic:
TRUEmeans the condition is known to be true.FALSEmeans the condition is known to be false.UNKNOWNmeans the condition cannot be determined.
A WHERE clause keeps only rows for which its condition is TRUE. Both FALSE and UNKNOWN remove the row.
SELECT 1 AS one
FROM dual
WHERE NULL = NULL;
SELECT 1 AS one
FROM dual
WHERE NULL IS NULL;The first query returns no rows. The second returns one row because IS NULL is the special comparison designed for missing values. Use IS NOT NULL to find values that are present.
NULL in Expressions
NULL usually propagates through expressions. Arithmetic with a NULL operand produces NULL because the result is unknown.
SELECT salary,
commission_pct,
salary * commission_pct AS commission_amount,
salary + salary * commission_pct AS total_pay
FROM employees;For an employee whose commission_pct is NULL, both calculated columns are NULL. Oracle cannot calculate a commission or total pay from an unknown percentage.
Concatenation has a related but important behavior. In Oracle, concatenating a non-NULL value with NULL generally returns the non-NULL value:
SELECT 'Employee: ' || NULL AS label
FROM dual;The result is Employee: . This behavior differs from arithmetic, so test expressions when the distinction matters.
Empty Strings Are NULL in Oracle
Oracle currently treats a zero-length character literal, written as '', as NULL. Therefore, inserting or comparing an empty string behaves like using NULL.
SELECT NVL('', 'was null') AS result
FROM dual;The replacement value is returned because the first expression is NULL. A string containing a space is different from an empty string:
SELECT LENGTH(' ') AS space_length
FROM dual;The result is 1, because the value contains one character.
Exercise
Which condition correctly finds employees whose commission percentage is missing: commission_pct = NULL, commission_pct <> NULL, or commission_pct IS NULL?
Show answer
commission_pct IS NULL. The equality and inequality comparisons produce UNKNOWN when one side is NULL, so a WHERE clause rejects them.