How do you check for NULL in SQL?
SQLLevel: FoundationNULL Basics
How do you check for NULL in SQL?
Hints
- Distinguish a missing value from zero, an empty string and false.
- Test comparison, aggregation and filtering on a tiny missing-value example.
Answer
You must use IS NULL or IS NOT NULL. Using = NULL will not work.
-- Wrong: always returns no rows
SELECT * FROM users WHERE deleted_at = NULL;
-- Correct
SELECT * FROM users WHERE deleted_at IS NULL;
SELECT * FROM users WHERE deleted_at IS NOT NULL;
Source and licence: SQL Interview Questions — Arda Kocadoru · MIT