Question presented to candidate:
"Some of our employees have no bonus recorded and some have no manager. Explain what NULL actually means in SQL, why WHERE bonus = NULL returns nothing, and which other places in a query NULL quietly changes the result."
What a strong answer should cover:
- NULL means unknown or missing, not zero and not an empty string; it is a marker, not a value
- Three-valued logic: comparisons with NULL give UNKNOWN, and WHERE, ON and HAVING keep only rows that are TRUE, so
= NULLmatches nothing; useIS NULL/IS NOT NULLor the null-safeIS [NOT] DISTINCT FROM - NULL propagates through arithmetic and
||concatenation;NOT (UNKNOWN)is still UNKNOWN, so a filter and its negation can both miss a row - Where NULLs group together instead: GROUP BY, DISTINCT and set operations put all NULLs in one bucket; UNIQUE allows many NULLs (PostgreSQL 15 added
NULLS NOT DISTINCT) - Engine details: NULL sorts first in SQLite and last in PostgreSQL ascending (
NULLS FIRST/LAST), Oracle treats''as NULL, aggregates skip NULLs, andNOT INwith a NULL in the list returns no rows
Code / implementation expected: Optional β show = NULL vs IS NULL and one case where a filter and its negation together lose rows.
Schema used by the examples: