7. NULL Values: Missing Data Done Right
7.4 COALESCE and IFNULL to Fill a Display Value
Why. Dashboards often need a display label instead of a blank.
What. COALESCE(a,b) returns the first non-NULL. IFNULL(x,y) is SQLite’s two-argument helper.
How. SELECT IFNULL(note, '(none)') AS note_label FROM orders;
Steps
- Replace NULL notes with (none) for display.
- Try COALESCE(priority, 'Unknown') on tickets.
- Keep raw NULL in storage if the business needs “unknown”.
Examples
Example 1 · dataset: raja_coffee
SELECT order_id, IFNULL(note, '(none)') AS note_label
FROM orders
WHERE order_id IN (1001,1003,1013);Output
| order_id | note_label |
|---|---|
| 1001 | (none) |
| 1003 | gift |
| 1013 | office stock |
Meaning. Blanks become the label (none).
Example 2 · dataset: interview_mini
SELECT ticket_id, COALESCE(priority, 'Unknown') AS priority_label
FROM tickets
ORDER BY ticket_id;Output
| ticket_id | priority_label |
|---|---|
| 1 | High |
| 2 | Low |
| 3 | Medium |
| 4 | High |
| 5 | Low |
| 6 | Unknown |
| 7 | High |
| 8 | Medium |
Meaning. Every ticket shows a priority label.
Ravindra Bagale's Tip
Display fill ≠ forever fill. Do not UPDATE NULLs to 0 without asking.
Ravindra Bagale's Tip – मराठी
Display भरणे ≠ कायम भरणे. विचारल्याशिवाय NULL ला 0 UPDATE करू नका.
Ravindra Bagale's Tip – हिंदी
Display भरना ≠ हमेशा भरना। पूछे बिना NULL को 0 UPDATE न करें।