COALESCE fills an empty cell with a backup
Sometimes a column is NULL and a blank cell would confuse whoever reads the result. COALESCE takes a list of values and returns the first one that isn’t NULL. Write the column first and a backup value second: when the column has something, you see it; when it’s NULL, you see the backup. The stored data never changes — only what the result shows.
A tiny example
In plain English: Show me each order’s note, or pickup when there is no note.
SELECT id, COALESCE(note, 'pickup') AS note
FROM orders;Query result
| id | note |
|---|---|
| 1 | extra hot |
| 2 | pickup |
| 3 | oat milk |
Order 2 has no note recorded, so COALESCE shows the backup word pickup instead of a blank.
Common questions
- Can COALESCE take more than two values?
- Yes. COALESCE(a, b, c) returns the first one that isn't NULL, checking left to right, so you can stack several backups.
- Is COALESCE the same as ISNULL or IFNULL?
- Same idea, different names. SQL Server has ISNULL and MySQL has IFNULL, but COALESCE works in nearly every database, so it is the portable choice.
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