Menu
CoddyTech

Fallbacks with COALESCE

Part of the First Queries section of Coddy's PostgreSQL journey. Lesson 14 of 20.

COALESCE returns the first argument that is not NULL. Use it when a missing value needs a display fallback. It does not replace other values that happen to look empty.

Input table products:

idnamecategorypricestockactivenote
1Maptravel125TrueNULL
2Notebookbooks80TrueRecycled
3Atlasbooks243FalseNULL
4Mughome128TrueBlue
5lamphome302FalseClearance
6Mini maptravel610TrueNULL
SELECT id, COALESCE(note, 'No note') AS display_note FROM products ORDER BY id;

Map displays No note; Notebook keeps Recycled. An empty string would remain an empty string because it is a non-null value. The source note remains unchanged.

COALESCE(value, fallback) replaces NULL in the result, while keeping zero and empty text.

challenge icon

Challenge

Easy

Return id and shipping_note, using the existing note when present and Pack carefully when missing. Order by id.

The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.

Try it yourself

-- Write your SQL here.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in First Queries

Practice on your own: PostgreSQL playground