Module 02 · lesson 7
IS NULL: filtering empty values
A content manager has to find products with no category in order to fill them in. An ordinary = NULL comparison returns nothing, for some reason.
You'll need:NULL: when there's no value(opens in a new tab)WHERE: keeping only the rows you need(opens in a new tab)
Why = NULL doesn't work
NULL means "the value is unknown". And a comparison with the unknown gives not true/false but "unknown" — so such a row never passes the filter. Because of this, WHERE category = NULL always gives an empty result (a classic beginner's trap).
IS NULL / IS NOT NULL
For NULL there are dedicated operators:
IS NULL— the value is missing;IS NOT NULL— the value is present.
SELECT name
FROM products
WHERE category IS NOT NULL; -- only products with a category
This is the only correct way to work with empty values.
Your turn
The content manager needs products with no category, to fill them in. Output the name and category of such products.
Summary
- Empty values are caught with
IS NULL/IS NOT NULL, never with= NULL.