Bird
0
0

Which of the following SQL queries correctly groups rows by a column that may contain NULL values?

easy📝 Syntax Q3 of 15
SQL - GROUP BY and HAVING
Which of the following SQL queries correctly groups rows by a column that may contain NULL values?
ASELECT col, COUNT(*) FROM table GROUP BY col WHERE col IS NOT NULL;
BSELECT col, COUNT(*) FROM table GROUP BY col;
CSELECT col, COUNT(*) FROM table WHERE col IS NOT NULL GROUP BY col;
DSELECT col, COUNT(*) FROM table GROUP BY col HAVING col IS NOT NULL;
Step-by-Step Solution
Solution:
  1. Step 1: Review GROUP BY syntax with NULLs

    GROUP BY can include NULL values; no special syntax needed to group NULLs.
  2. Step 2: Check each option for correctness

    SELECT col, COUNT(*) FROM table GROUP BY col; is correct syntax and groups NULLs as one group. Options B and D misuse WHERE/HAVING clauses. SELECT col, COUNT(*) FROM table WHERE col IS NOT NULL GROUP BY col; filters out NULLs before grouping.
  3. Final Answer:

    SELECT col, COUNT(*) FROM table GROUP BY col; -> Option B
  4. Quick Check:

    GROUP BY includes NULLs without extra filters [OK]
Quick Trick: GROUP BY works with NULLs without filters [OK]
Common Mistakes:
MISTAKES
  • Using WHERE in GROUP BY clause incorrectly
  • Filtering NULLs unintentionally
  • Misplacing HAVING clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes