Sub topic 1
What NULL Means in Oracle
Prerequisites
You should know basic SELECT statements with WHERE clauses and column aliases. You should also recognize the common Oracle data types (NUMBER, VARCHAR2, DATE) and use the DUAL table for quick experiments.
What NULL Means
In Oracle, NULL marks a value that is missing or unknown. NULL is not zero, and it is not a blank text value. An expression that uses an unknown input usually produces an unknown result.
Comparing with NULL
A comparison with NULL never returns true. Oracle uses three-valued logic, so every condition evaluates to TRUE, FALSE, or UNKNOWN. A comparison with NULL yields UNKNOWN, and a WHERE clause keeps only the rows that are TRUE. Test for NULL with IS NULL or IS NOT NULL.
SELECT 'match' AS result FROM dual WHERE NULL = NULL;
SELECT 'match' AS result FROM dual WHERE NULL IS NULL;The first query returns no rows. The second returns one row.
How NULL Propagates Through Arithmetic and Concatenation
Arithmetic with NULL produces NULL. If any operand is NULL, the whole result is NULL.
SELECT employee_id,
salary,
commission_pct,
salary + salary * commission_pct AS total_pay
FROM employees;For employees with no commission, total_pay is NULL. Concatenation with || behaves differently. Oracle treats a NULL operand as an empty string:
SELECT 'Dept: ' || NULL || 'end' AS joined FROM dual;This query returns Dept: end.
Oracle Treats the Empty String as NULL
In Oracle, an empty string is stored and treated as NULL in VARCHAR2 columns. The following query returns one row:
SELECT 1 AS one FROM dual WHERE '' IS NULL;> Many other databases keep an empty string separate from NULL. Check this behavior before you port queries to Oracle.
Note that the callout above uses a blockquote, which the format does not allow. Use the callout format instead:
Exercise
Exercise
Which of these two queries returns a row, and why does the other one return nothing? Query A: WHERE NULL = NULL. Query B: WHERE NULL IS NULL.
Show answer
Query B returns one row. Query A returns no rows because NULL = NULL evaluates to UNKNOWN, and WHERE keeps only TRUE rows.