Bird
0
0

Given the table sales with data:

medium📝 query result Q13 of 15
SQL - GROUP BY and HAVING
Given the table sales with data:
product | region
-------|--------
A      | East
B      | NULL
A      | NULL
B      | East
NULL   | West
NULL   | NULL

What is the result of:
SELECT region, COUNT(*) FROM sales GROUP BY region ORDER BY region;
A[ {"region": "East", "count": 2}, {"region": "NULL", "count": 3}, {"region": "West", "count": 1} ]
B[ {"region": "East", "count": 2}, {"region": "NULL", "count": 2}, {"region": "West", "count": 1} ]
C[ {"region": "East", "count": 2}, {"region": null, "count": 2}, {"region": "West", "count": 1} ]
D[ {"region": null, "count": 3}, {"region": "East", "count": 2}, {"region": "West", "count": 1} ]
Step-by-Step Solution
Solution:
  1. Step 1: Group rows by region including NULLs

    Rows with region 'East' = 2, 'West' = 1, and NULL values (3 rows) are grouped together as one NULL group.
  2. Step 2: Understand NULL display and count

    SQL returns NULL as null (not string 'NULL'). Count for NULL group is 3 because three rows have region NULL. ORDER BY region ASC places null first.
  3. Final Answer:

    [{"region": null, "count": 3}, {"region": "East", "count": 2}, {"region": "West", "count": 1}] -> Option D
  4. Quick Check:

    GROUP BY NULL groups count 3, NULL shown as null [OK]
Quick Trick: NULLs group together and show as null, not 'NULL' string [OK]
Common Mistakes:
MISTAKES
  • Counting NULL rows separately
  • Displaying NULL as string 'NULL'
  • Miscounting NULL group size

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes