Bird
Raised Fist0
SQLquery~10 mins

GROUP BY with NULL values behavior in SQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - GROUP BY with NULL values behavior
Start with table data
Identify column to GROUP BY
Check each row's GROUP BY value
Group rows with same value, including NULLs
Aggregate or list grouped rows
Return grouped result set
The database groups rows by the specified column, treating NULLs as a single group.
Execution Sample
SQL
SELECT category, COUNT(*) FROM products GROUP BY category;
Groups products by category, counting how many products are in each category, including those with NULL category.
Execution Table
StepRowcategory valueGroup assignedGroup count so far
1Row 1'Electronics'Group 'Electronics'1
2Row 2'Clothing'Group 'Clothing'1
3Row 3NULLGroup NULL1
4Row 4'Electronics'Group 'Electronics'2
5Row 5NULLGroup NULL2
6Row 6'Clothing'Group 'Clothing'2
7Row 7'Toys'Group 'Toys'1
8End---
💡 All rows processed; groups formed including NULL group.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
Group 'Electronics' count011122222
Group 'Clothing' count001111222
Group NULL count000112222
Group 'Toys' count000000011
Key Moments - 2 Insights
Why are NULL values grouped together instead of ignored or treated separately?
In the execution_table rows 3 and 5 show NULL values assigned to the same group 'Group NULL', because SQL treats all NULLs as one group in GROUP BY.
Does GROUP BY treat NULL as equal to any other value?
No, as shown in execution_table, NULLs form their own group distinct from any non-NULL value groups.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the group count for NULL values?
A1
B2
C0
D3
💡 Hint
Check the 'Group count so far' column at step 5 for the NULL group.
At which step does the 'Toys' group first appear in the execution_table?
AStep 4
BStep 5
CStep 7
DStep 3
💡 Hint
Look for the first row where 'Group 'Toys'' is assigned.
If the NULL values were not grouped together, how would the variable_tracker change?
AThere would be multiple NULL groups with counts of 1 each
BNULL group count would be zero
CNULL group count would be combined with 'Electronics'
DNULL group count would be the same as shown
💡 Hint
Consider how grouping treats NULLs as one group in variable_tracker.
Concept Snapshot
GROUP BY groups rows by column values.
NULL values are grouped together as one group.
Each group aggregates rows sharing the same value or NULL.
Result shows one row per group including NULL group.
Useful to count or summarize data by categories including NULL.
Full Transcript
This visual execution shows how SQL GROUP BY works with NULL values. Each row is checked for the grouping column value. Rows with the same value form a group. Rows with NULL in the grouping column form a single NULL group. The execution table traces each row's group assignment and counts. The variable tracker shows how group counts update step-by-step. Key moments clarify that NULLs are treated as equal for grouping, forming one group. The quiz tests understanding of group counts and NULL grouping behavior. The snapshot summarizes that GROUP BY includes NULLs as one group in results.

Practice

(1/5)
1. What happens to NULL values when you use GROUP BY on a column containing them?
easy
A. All NULL values are grouped together as one group.
B. NULL values are ignored and not included in any group.
C. Each NULL value forms its own separate group.
D. GROUP BY causes an error if NULL values exist.

Solution

  1. Step 1: Understand how GROUP BY handles NULLs

    In SQL, GROUP BY treats all NULL values in a column as equal, grouping them into one group.
  2. Step 2: Confirm behavior with example

    If a column has multiple rows with NULL, they appear as a single group in the result.
  3. Final Answer:

    All NULL values are grouped together as one group. -> Option A
  4. Quick Check:

    GROUP BY NULL = one group [OK]
Hint: Remember: NULLs group together, not separately [OK]
Common Mistakes:
  • Thinking NULLs are ignored in GROUP BY
  • Assuming each NULL is a separate group
  • Believing GROUP BY errors on NULL values
2. Which of the following SQL queries correctly groups rows by a column that may contain NULL values?
easy
A. SELECT category, COUNT(*) FROM products GROUP BY category;
B. SELECT category, COUNT(*) FROM products GROUP BY category WHERE category IS NOT NULL;
C. SELECT category, COUNT(*) FROM products WHERE category IS NOT NULL GROUP BY category;
D. SELECT category, COUNT(*) FROM products GROUP BY category HAVING category IS NOT NULL;

Solution

  1. Step 1: Check GROUP BY syntax with NULLs

    SELECT category, COUNT(*) FROM products GROUP BY category; uses correct syntax: grouping by category including NULLs. GROUP BY works with NULL values without extra filters.
  2. Step 2: Analyze other options

    Options A and D misuse WHERE and HAVING clauses with GROUP BY. SELECT category, COUNT(*) FROM products WHERE category IS NOT NULL GROUP BY category; filters out NULLs before grouping, which is valid but excludes NULL groups.
  3. Final Answer:

    SELECT category, COUNT(*) FROM products GROUP BY category; -> Option A
  4. Quick Check:

    GROUP BY with NULLs needs no special filter [OK]
Hint: GROUP BY works directly with NULLs, no WHERE needed [OK]
Common Mistakes:
  • Using WHERE after GROUP BY (syntax error)
  • Filtering NULLs before grouping unintentionally
  • Misusing HAVING clause for filtering NULLs
3. 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;
medium
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} ]

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]
Hint: NULLs group together and show as null, not 'NULL' string [OK]
Common Mistakes:
  • Counting NULL rows separately
  • Displaying NULL as string 'NULL'
  • Miscounting NULL group size
4. 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?
medium
A. Add WHERE department IS NOT NULL before GROUP BY.
B. No fix needed; GROUP BY never errors on NULL.
C. Use HAVING department IS NOT NULL after GROUP BY.
D. Replace NULL with a string using COALESCE(department, 'Unknown') in SELECT and GROUP BY.

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]
Hint: GROUP BY handles NULLs without error [OK]
Common Mistakes:
  • Thinking GROUP BY errors on NULLs
  • Unnecessarily filtering out NULLs with WHERE
  • Misusing HAVING for pre-group filtering
5. You have a table orders with columns customer_id and status, where status can be NULL. You want to count orders by status, treating all NULL statuses as 'Pending'. Which query correctly achieves this?
hard
A. SELECT status, COUNT(*) FROM orders GROUP BY status WHERE status IS NULL;
B. SELECT status, COUNT(*) FROM orders GROUP BY status HAVING status IS NOT NULL;
C. SELECT COALESCE(status, 'Pending') AS order_status, COUNT(*) FROM orders GROUP BY order_status;
D. SELECT status, COUNT(*) FROM orders GROUP BY status ORDER BY status;

Solution

  1. Step 1: Replace NULL with 'Pending' using COALESCE

    COALESCE(status, 'Pending') converts NULL statuses to 'Pending' for counting.
  2. Step 2: Group by the alias used in SELECT

    Grouping by order_status ensures all NULLs are counted under 'Pending'.
  3. Final Answer:

    SELECT COALESCE(status, 'Pending') AS order_status, COUNT(*) FROM orders GROUP BY order_status; -> Option C
  4. Quick Check:

    COALESCE + GROUP BY alias counts NULL as 'Pending' [OK]
Hint: Use COALESCE and group by alias to count NULLs as desired [OK]
Common Mistakes:
  • Filtering out NULLs instead of replacing them
  • Using WHERE after GROUP BY (syntax error)
  • Grouping by original column without COALESCE