How does NULL affect GROUP BY?
SQLLevel: IntermediateNULL Handling
How does NULL affect GROUP BY?
Hints
- Distinguish a missing value from zero, an empty string and false.
- Test comparison, aggregation and filtering on a tiny missing-value example.
Answer
In SQL, NULL values in a GROUP BY column are grouped together into a single group (treated as equal to each other for grouping purposes, even though NULL = NULL is not TRUE).
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
-- Employees with NULL department are grouped into one "NULL" group
Source and licence: SQL Interview Questions — Arda Kocadoru · MIT