Sub topic 8
Working with NULL
Assume you already understand tables, rows, columns, and basic SELECT and WHERE clauses. The examples use the HR sample schema's employees table. You also need basic knowledge of numbers, strings, and dates.
NULL is a marker that means a value is missing, unknown, or not applicable. It is not the same as zero, an empty string, or a blank space.
| Value | Meaning |
|---|---|
| `NULL` | No known value |
| `0` | The numeric value zero |
| `''` | In Oracle, an empty character string is treated as `NULL` |
| `' '` | A string containing one space |
Testing for NULL
SQL does not use = NULL or <> NULL. A comparison with NULL produces UNKNOWN, because Oracle cannot decide whether an unknown value equals another value.
Use IS NULL to find missing values and IS NOT NULL to find values that exist.
SELECT employee_id, first_name, last_name
FROM employees
WHERE commission_pct IS NULL;This query returns employees whose commission_pct has no value.
SELECT employee_id, first_name, last_name, commission_pct
FROM employees
WHERE commission_pct IS NOT NULL;This query returns only employees with a known commission percentage.
NULL and three-valued logic
Normal comparisons produce TRUE or FALSE. Comparisons involving NULL can also produce UNKNOWN. A WHERE clause keeps only rows for which its condition is TRUE. Rows producing FALSE or UNKNOWN are excluded.
For example, this condition does not include employees with a null commission:
WHERE commission_pct > 0.2For a null commission, Oracle cannot establish that the value is greater than 0.2, so the result is UNKNOWN.
You can combine conditions to make the intended rule explicit:
SELECT first_name, last_name, commission_pct
FROM employees
WHERE commission_pct IS NULL
OR commission_pct > 0.2;The OR condition keeps employees with no commission and employees whose commission is greater than 20 percent.
Replacing NULL with a value
NVL returns one value when its first argument is not null and a replacement value when it is null.
SELECT first_name,
last_name,
commission_pct,
NVL(commission_pct, 0) AS commission_for_display
FROM employees;The original commission_pct remains unchanged. Only the result column displays 0 for null commissions.
The replacement should be compatible with the first argument's data type. For example, use a number such as 0 for a numeric column and a character value for a character column.
COALESCE is useful when you have more than one possible replacement. It returns the first non-null expression.
SELECT employee_id,
COALESCE(commission_pct, 0) AS commission
FROM employees;For two arguments, COALESCE and NVL often produce the same result. COALESCE can also check several expressions:
SELECT employee_id,
COALESCE(commission_pct, salary * 0.01, 0) AS estimated_commission
FROM employees;Oracle checks the expressions from left to right and returns the first non-null value.
NULL in expressions
Arithmetic involving NULL normally produces NULL. If a salary or commission is unknown, a calculation using it is also unknown.
SELECT employee_id,
salary,
salary * NVL(commission_pct, 0) AS commission_amount
FROM employees;Here, NVL changes a missing commission percentage to zero before multiplication. Without NVL, salary * commission_pct would be null for employees without a commission.
Exercise
Which condition correctly finds employees whose commission percentage is missing?
Show answer
WHERE commission_pct IS NULL