Sub topic 7
Replacing and Creating NULLs with COALESCE and NULLIF
This chapter assumes you know basic SELECT, WHERE, ORDER BY, JOIN, and aggregate functions. It also assumes you understand that NULL means a missing, unknown, or inapplicable value. NULL is not the same as zero, an empty string, or FALSE.
Use this sample table in the examples:
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
manager_id INTEGER,
commission NUMERIC(8,2)
);
INSERT INTO employees (id, name, manager_id, commission) VALUES
(1, 'Ada', NULL, NULL),
(2, 'Ben', 1, 100.00),
(3, 'Cy', 1, NULL),
(4, 'Di', 2, 300.00);COALESCE: Choose the First Available Value
COALESCE returns the first argument that is not NULL. It is useful when a query needs a fallback value.
SELECT name,
COALESCE(commission, 0) AS commission_or_zero
FROM employees;For Ada and Cy, commission is NULL, so the result is 0. Ben and Di keep their stored commissions.
COALESCE can check several alternatives. For example, this expression uses a preferred display name, then a legal name, then a final label:
COALESCE(preferred_name, legal_name, 'Unnamed')The database evaluates arguments from left to right and returns the first non-NULL value.
You can also use COALESCE in calculations:
SELECT name,
COALESCE(commission, 0) + 50 AS projected_commission
FROM employees;This treats a missing commission as zero for this calculation. That interpretation is appropriate only when missing commission really means no commission. If it means “not yet evaluated,” replacing it with zero could mislead users.
NULLIF: Turn a Value into NULL
NULLIF(a, b) returns NULL when a and b are equal. Otherwise, it returns a.
SELECT NULLIF(commission, 0) AS nonzero_commission
FROM employees;This converts a stored zero into NULL, which can represent “no meaningful amount” in a report.
A common use prevents division-by-zero errors:
SELECT 100 / NULLIF(0, 0) AS safe_ratio;NULLIF(0, 0) returns NULL, so the expression produces NULL instead of attempting to divide by zero. You can combine it with COALESCE when the report needs a display value:
SELECT COALESCE(100 / NULLIF(0, 0), 0) AS displayed_ratio;The first function protects the calculation. The second supplies a fallback after the calculation has produced NULL.
Choosing Between the Functions
| Need | Function | Typical use |
|---|---|---|
| Pick a usable value from alternatives | `COALESCE` | Missing commission becomes zero for a report |
| Convert a particular value into `NULL` | `NULLIF` | Treat zero as absent |
| Avoid a zero denominator | `NULLIF` | `amount / NULLIF(quantity, 0)` |
Be careful with data meaning. Use COALESCE only when the fallback has a valid interpretation. Use NULLIF only when the value being removed is a known sentinel, such as zero or an empty string.
Exercise
Write a query that returns each employee's name and divides 100 by commission, returning 0 when commission is either NULL or zero.
Show answer
SELECT name, COALESCE(100 / NULLIF(commission, 0), 0) AS ratio FROM employees;