We use GROUP BY to group rows that have the same values in specified columns. Understanding how NULL values behave helps us group data correctly when some values are missing.
GROUP BY with NULL values behavior in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SQL
SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;
NULL values in the GROUP BY column are treated as a single group.
All rows with NULL in the grouped column are grouped together.
Examples
SQL
SELECT department, COUNT(*) FROM employees GROUP BY department;
SQL
SELECT category, SUM(sales) FROM products GROUP BY category;
Sample Program
This example creates an employees table with some NULL departments. It groups employees by department, counting how many are in each group. NULL departments are grouped together.
SQL
CREATE TABLE employees ( id INT, name VARCHAR(50), department VARCHAR(50) ); INSERT INTO employees (id, name, department) VALUES (1, 'Alice', 'HR'), (2, 'Bob', NULL), (3, 'Charlie', 'IT'), (4, 'David', NULL), (5, 'Eve', 'HR'); SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department ORDER BY department;
Important Notes
NULL values are grouped together as one group in GROUP BY.
Ordering by the grouped column may show NULL as empty or at the start/end depending on the database.
Summary
GROUP BY treats all NULL values in a column as one group.
This helps include rows with missing values in grouped results.
Practice
1. What happens to
NULL values when you use GROUP BY on a column containing them?easy
Solution
Step 1: Understand how GROUP BY handles NULLs
In SQL,GROUP BYtreats allNULLvalues in a column as equal, grouping them into one group.Step 2: Confirm behavior with example
If a column has multiple rows withNULL, they appear as a single group in the result.Final Answer:
All NULL values are grouped together as one group. -> Option AQuick 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
Solution
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.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.Final Answer:
SELECT category, COUNT(*) FROM products GROUP BY category; -> Option AQuick 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
What is the result of:
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
Solution
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.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.Final Answer:
[{"region": null, "count": 3}, {"region": "East", "count": 2}, {"region": "West", "count": 1}] -> Option DQuick 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:
It returns an error. Which fix will correctly handle
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
Solution
Step 1: Understand why no error occurs
In standard SQL,GROUP BYhandlesNULLvalues correctly by grouping all NULLs together into one group. No error is thrown.Step 2: Confirm no fix needed
The query runs successfully and includes a NULL group in the results.Final Answer:
No fix needed; GROUP BY never errors on NULL. -> Option BQuick 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
Solution
Step 1: Replace NULL with 'Pending' using COALESCE
COALESCE(status, 'Pending') converts NULL statuses to 'Pending' for counting.Step 2: Group by the alias used in SELECT
Grouping byorder_statusensures all NULLs are counted under 'Pending'.Final Answer:
SELECT COALESCE(status, 'Pending') AS order_status, COUNT(*) FROM orders GROUP BY order_status; -> Option CQuick 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
