DevPath · Learn to code ESPTEN

Sets and functions

Working with NULL

What is NULL?

NULL represents the absence of a value: "unknown" or "not applicable". It is not zero nor an empty string. Any arithmetic operation with NULL yields NULL (price + 10 is NULL if price is NULL).

Three-valued logic

In SQL a condition can be true, false or unknown. Because NULL is "unknown", comparing with it never yields true:

SELECT * FROM products WHERE price = NULL;   -- ❌ returns nothing

Even if price is NULL, NULL = NULL is unknown, not true. That is why there are specific operators:

SELECT * FROM products WHERE price IS NULL;       -- unknown prices
SELECT * FROM products WHERE price IS NOT NULL;   -- known prices

Functions to handle NULL

Examples

Replace NULL with 0 using COALESCE

SELECT name, COALESCE(price, 0) AS final_price
FROM products
ORDER BY id;
Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← Set operationsScalar and aggregate functions →