×

sql · level 2 · unit 5 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

swaps a NULL for a .

Reports with NULL in them look unfinished. COALESCE(author, 'Unknown') looks at the author. If there is one, you get it. If it is NULL, you get the fallback instead.

It hands back the first value in its brackets that is not NULL, so you can list several, like COALESCE(nickname, name, 'friend').

It fixes the SUM problem too. After a LEFT JOIN, COALESCE(SUM(o.quantity), 0) turns an empty group's NULL into 0.

SELECT title, COALESCE(author, 'Unknown') AS author,
  COALESCE(borrower, 'on the shelf') AS borrower
FROM books
ORDER BY title;

Pick a fallback of the same kind: text for a text column, a number for a number column.