Bird
0
0

You want to write a query that returns the first non-NULL value among col1, col2, and col3, but if all are NULL, it should return 'N/A'. Which SQL expression correctly achieves this?

hard📝 Application Q8 of 15
SQL - CASE Expressions
You want to write a query that returns the first non-NULL value among col1, col2, and col3, but if all are NULL, it should return 'N/A'. Which SQL expression correctly achieves this?
ANULLIF(col1, col2, col3, 'N/A')
BCOALESCE(col1, col2, col3, 'N/A')
CCASE WHEN col1 IS NOT NULL THEN col1 ELSE col2 END
DCOALESCE(NULLIF(col1, col2), col3, 'N/A')
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirement

    Return first non-NULL among three columns, else 'N/A'.
  2. Step 2: Match with COALESCE usage

    COALESCE returns first non-NULL; adding 'N/A' last covers all NULL case.
  3. Step 3: Evaluate options

    COALESCE(col1, col2, col3, 'N/A') matches perfectly; others misuse NULLIF or incomplete CASE.
  4. Final Answer:

    COALESCE(col1, col2, col3, 'N/A') -> Option B
  5. Quick Check:

    COALESCE with default last handles all NULLs [OK]
Quick Trick: Add default value last in COALESCE to handle all NULLs [OK]
Common Mistakes:
  • Using NULLIF with more than two arguments
  • Using incomplete CASE statements
  • Not providing a default fallback value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes