Bird
0
0

Identify the error in the following SQL query: SELECT SUM(NULLIF(sales, NULL)) FROM transactions;

medium📝 Debug Q6 of 15
SQL - Aggregate Functions
Identify the error in the following SQL query: SELECT SUM(NULLIF(sales, NULL)) FROM transactions;
ANo error, query is correct
BSUM cannot be used with NULLIF
CNULLIF cannot compare to NULL, use COALESCE instead
DSyntax error: missing GROUP BY clause
Step-by-Step Solution
Solution:
  1. Step 1: Understand NULLIF usage

    NULLIF(sales, NULL) is valid SQL syntax. Since any value = NULL is UNKNOWN (not true), NULLIF always returns sales, preserving NULLs.
  2. Step 2: Check for errors

    No syntax error; no GROUP BY needed for table-wide aggregate; SUM works fine with it (equivalent to SUM(sales)).
  3. Final Answer:

    No error, query is correct -> Option A
  4. Quick Check:

    NULLIF(col, NULL) valid, no error = D [OK]
Quick Trick: NULLIF(column, NULL) is valid and equivalent to the column itself [OK]
Common Mistakes:
MISTAKES
  • Thinking NULLIF cannot take NULL as second argument
  • Assuming SUM requires GROUP BY for single aggregate
  • Believing SUM cannot aggregate NULLIF results

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes