NULL and empty text ('') are not the same
Starting table: orders
| id | note |
|---|---|
| 101 | extra oat milk |
| 102 | NULL |
| 103 | |
| 104 | NULL |
| 108 |
Empty text, written '', is a real value: a note box that was filled in with nothing. NULL means no value was recorded at all. They can look alike on screen, but SQL treats them differently. note = '' finds the blank ones, and only IS NULL finds the missing ones (note = NULL finds nothing). Heads up: Oracle stores '' as NULL.
A tiny example
In plain English: Show me the orders whose note was left blank, which is different from having no note at all.
SELECT id, note
FROM orders
WHERE note = '';Query result
| id | note |
|---|---|
| 103 | |
| 108 |
Orders 103 and 108 have a note that is empty text. Orders 102 and 104 have a NULL note, so they don't appear in your results, because NULL is not equal to ''.
Common questions
- Why does WHERE note = NULL return no rows?
- NULL means no value was recorded, so there is nothing to compare. A comparison with NULL is never true. Use IS NULL to find missing values.
- How do I find both blank and missing values?
- Combine both checks: WHERE note = '' OR note IS NULL. Heads up: Oracle stores '' as NULL, so there IS NULL alone finds both.
- How do I find rows with real text, neither NULL nor empty?
- Check both: WHERE note IS NOT NULL AND note <> ''. In the example, only order 101 (extra oat milk) comes back.
- Does COUNT(column) count empty text?
- Yes. COUNT(note) counts '' because it is a real value, but skips NULL. In the example, COUNT(note) is 3 while COUNT(*) is 5.
- What does LENGTH return for '' and NULL?
- LENGTH('') is 0, because the text exists and has no characters. LENGTH(NULL) is NULL, because there is nothing to measure.
- Why does joining text with NULL give NULL?
- 'Note: ' || NULL is NULL, because adding something unknown makes the whole result unknown. 'Note: ' || '' keeps 'Note: '. Wrap the column in COALESCE(note, '') to keep the text.
Related SQL lessons
Ready for a break?
A fresh, bite-sized SQL puzzle waits every morning. Tap tiles, see real results. No terminal, no setup, no timers.
Try today's free SQL puzzle