← Back to SQL, page 2

sql-0038 · Easy fresher

How are NULLs treated by GROUP BY? What does a query grouping employees by department return for employees without a department?

GROUP BY puts all NULLs into one group, even though NULL = NULL is not true in a WHERE clause. So employees with no department appear together in a single row whose department value is NULL. Label it with COALESCE if the report needs a readable name.

Full answer

For grouping, SQL treats NULLs as "not distinct" from each other, the same rule DISTINCT uses. All rows with a NULL key land in one group.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT dept_id, COUNT(*) AS people
FROM employees
GROUP BY dept_id
ORDER BY dept_id;
dept_idpeople
NULL1
13
22
32

Lena has no department, so she forms a group of one with dept_id NULL. If more employees lacked a department, they would all be counted in that same row, not split into separate groups.

Reports usually want a readable label. Group by the raw key and label it in the select list:

sql
SELECT COALESCE(CAST(dept_id AS TEXT), 'Unassigned') AS dept, COUNT(*) AS people
FROM employees
GROUP BY dept_id
ORDER BY dept_id;
deptpeople
Unassigned1
13
22
32

Grouping by the label instead would be risky: if a real department were ever named 'Unassigned', the two would merge into one row.

Joining to departments changes things in a different way. An inner join drops Lena entirely, because her NULL dept_id matches no department. A LEFT JOIN keeps her, with a NULL department name.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT dept_id, COUNT(*) AS people
FROM employees
GROUP BY dept_id
ORDER BY dept_id;

SELECT COALESCE(CAST(dept_id AS TEXT), 'Unassigned') AS dept, COUNT(*) AS people
FROM employees
GROUP BY dept_id
ORDER BY dept_id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • GROUP BY collects all NULL keys into one group
  • This differs from = in WHERE, where NULL never equals NULL
  • Use COALESCE to label the NULL group
  • An inner join to the lookup table drops NULL-key rows

Common mistakes

  • Expecting each NULL to form its own group
  • Losing the NULL group by inner joining to a lookup table

Follow-ups

  • How would you put the NULL group last in the report?
  • What does COUNT(dept_id) show for that group?

Tips

  • Compare the rule with WHERE explicitly; that contrast is the point of the question