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:
Step 1: Review GROUP BY syntax with NULLs
GROUP BY can include NULL values; no special syntax needed to group NULLs.
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.
Final Answer:
SELECT col, COUNT(*) FROM table GROUP BY col; -> Option B
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
Master "GROUP BY and HAVING" in SQL
9 interactive learning modes - each teaches the same concept differently