Bird
0
0

Consider this query:

medium📝 Debug Q14 of 15
SQL - GROUP BY and HAVING
Consider this query:
SELECT department, COUNT(*) FROM employees GROUP BY department;

It returns an error. Which fix will correctly handle NULL values in department to avoid errors?
AAdd <code>WHERE department IS NOT NULL</code> before GROUP BY.
BNo fix needed; GROUP BY never errors on NULL.
CUse <code>HAVING department IS NOT NULL</code> after GROUP BY.
DReplace <code>NULL</code> with a string using <code>COALESCE(department, 'Unknown')</code> in SELECT and GROUP BY.
Step-by-Step Solution
Solution:
  1. Step 1: Understand why no error occurs

    In standard SQL, GROUP BY handles NULL values correctly by grouping all NULLs together into one group. No error is thrown.
  2. Step 2: Confirm no fix needed

    The query runs successfully and includes a NULL group in the results.
  3. Final Answer:

    No fix needed; GROUP BY never errors on NULL. -> Option B
  4. Quick Check:

    GROUP BY NULL = no error [OK]
Quick Trick: GROUP BY handles NULLs without error [OK]
Common Mistakes:
MISTAKES
  • Thinking GROUP BY errors on NULLs
  • Unnecessarily filtering out NULLs with WHERE
  • Misusing HAVING for pre-group filtering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes