Bird
0
0

Why does using ELSE 0 instead of ELSE NULL in a CASE statement inside COUNT affect the result?

hard📝 Conceptual Q10 of 15
SQL - Advanced Query Patterns
Why does using ELSE 0 instead of ELSE NULL in a CASE statement inside COUNT affect the result?
AELSE 0 causes syntax error, ELSE NULL is correct
BELSE 0 counts all rows because 0 is not NULL, ELSE NULL excludes rows
CELSE 0 counts only zero values, ELSE NULL counts all
DNo difference, both count the same rows
Step-by-Step Solution
Solution:
  1. Step 1: Understand COUNT behavior

    COUNT counts only non-null values.
  2. Step 2: Effect of ELSE 0 vs ELSE NULL

    ELSE 0 returns zero (non-null), so COUNT counts those rows; ELSE NULL returns null, excluding rows from count.
  3. Final Answer:

    ELSE 0 counts all rows because 0 is not NULL, ELSE NULL excludes rows -> Option B
  4. Quick Check:

    COUNT excludes NULLs, includes zeros [OK]
Quick Trick: Use ELSE NULL to exclude rows in COUNT, ELSE 0 includes them [OK]
Common Mistakes:
  • Assuming 0 is treated as NULL
  • Thinking ELSE NULL causes errors
  • Believing both ELSE options behave the same

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes