Ravindra BagaleCourses & study guides Track your progress

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

  1. Replace NULL notes with (none) for display.
  2. Try COALESCE(priority, 'Unknown') on tickets.
  3. 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_idnote_label
1001(none)
1003gift
1013office 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_idpriority_label
1High
2Low
3Medium
4High
5Low
6Unknown
7High
8Medium

Meaning. Every ticket shows a priority label.

Ravindra Bagale's Tip

Display fill ≠ forever fill. Do not UPDATE NULLs to 0 without asking.