In the world of databases, you will often encounter situations where data is missing, unknown, or not applicable. SQL uses a special marker called NULL to represent these cases. Understanding NULL is critical because it behaves differently than any other value.
NULL is not a value; it is a state or a placeholder indicating that a data value does not exist in the database.
It is important to remember what NULL is NOT:
Because NULL represents an unknown state, you cannot use standard comparison operators like = or <> with it. Any comparison with NULL (e.g., value = NULL) will result in "unknown," not "true" or "false."
To check for NULL values, you must use specific operators:
Used to find records where a column has no value.
SELECT *
FROM address
WHERE address2 IS NULL;
Used to find records where a column contains any data.
SELECT *
FROM address
WHERE address2 IS NOT NULL;
One of the most important things to remember is that NULL propagates. If you perform a mathematical operation with a NULL value, the result will always be NULL.
10 + NULL = NULL5 * NULL = NULL'Hello ' + NULL = NULL= or <>) do not work with NULL.