96. Default Missing Values with COALESCE
EasyNULL HandlingCOALESCEIFNULL
COALESCE(a, b, ...) returns the first non-NULL argument, which makes it perfect for supplying defaults when a column is missing.
Task: for each user return id, name, and nickname — but when nickname is NULL, fall back to the user's name; if (hypothetically) both were NULL, fall back to the literal 'anonymous'. Order by id.
Output columns, in order: id, name, nickname.
Tables
Loading schema…
Expected outputrows must come back in this order
Computing expected output…
This is the result for the example data above. On Submit your query is graded against this example plus 4 hidden edge cases — 5test cases in all. A sloppy query that only fits the example won't pass.
SQL· SQLite · runs in your browser
⌘/Ctrl+↵ run · ⇧⌘/Ctrl+↵ submit
Run a query to see its output here.