Bird
0
0

Why can NULLIF(expr1, expr2) be considered a shortcut for a specific CASE expression in SQL?

hard📝 Conceptual Q10 of 15
SQL - CASE Expressions
Why can NULLIF(expr1, expr2) be considered a shortcut for a specific CASE expression in SQL?
ABecause it replaces NULL values with expr2, like a CASE WHEN expr1 IS NULL THEN expr2 ELSE expr1 END
BBecause it always returns the greater of expr1 and expr2, like a CASE statement
CBecause it returns NULL if expr1 equals expr2, else returns expr1, similar to a CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END
DBecause it counts non-NULL values between expr1 and expr2, like a CASE statement
Step-by-Step Solution
Solution:
  1. Step 1: Understand NULLIF behavior

    NULLIF(expr1, expr2) returns NULL if expr1 = expr2; otherwise, it returns expr1.
  2. Step 2: Compare with CASE

    This matches the CASE expression:
    CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END
  3. Final Answer:

    Because it returns NULL if expr1 equals expr2, else returns expr1, similar to a CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END -> Option C
  4. Quick Check:

    NULLIF(5,5) = NULL equals CASE WHEN 5=5 THEN NULL ELSE 5 END [OK]
Quick Trick: NULLIF mimics CASE for equality check [OK]
Common Mistakes:
  • Thinking NULLIF returns max value
  • Confusing NULLIF with COALESCE
  • Assuming NULLIF replaces NULLs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes