Full answer
After grouping by department, Engineering is one row standing for Asha, Ravi and Mei. Which single name should that row show? There is no correct answer, so standard SQL forbids the question.
PostgreSQL enforces the rule:
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, name, COUNT(*) FROM employees GROUP BY dept_id; -- error on purposeError: column "employees.name" must appear in the GROUP BY clause or be used in an aggregate functionSQLite allows these "bare columns" and picks a value from some row in the group:
SELECT dept_id, name, COUNT(*) AS people
FROM employees
WHERE dept_id IS NOT NULL
GROUP BY dept_id
ORDER BY dept_id;| dept_id | name | people |
|---|---|---|
| 1 | Asha | 3 |
| 2 | Tom | 2 |
| 3 | Diego | 2 |
The query runs and looks fine, which is the danger. Nothing promises which name you get, and it can change with the plan. (SQLite does make one exception, covered separately: with a single MIN or MAX aggregate, bare columns come from the row holding that minimum or maximum.)
Decide what you actually want and say it:
SELECT dept_id,
COUNT(*) AS people,
GROUP_CONCAT(name, ', ') AS names
FROM (SELECT dept_id, name FROM employees WHERE dept_id IS NOT NULL ORDER BY name)
GROUP BY dept_id
ORDER BY dept_id;| dept_id | people | names |
|---|---|---|
| 1 | 3 | Asha, Mei, Ravi |
| 2 | 2 | Sara, Tom |
| 3 | 2 | Diego, Omar |
Other valid fixes: add name to GROUP BY if you want one row per person (then the count is per person), or use a window function if you want every employee alongside their department's count. PostgreSQL also allows a column that is functionally dependent on a grouped primary key: grouping by departments.id lets you select departments.name without listing it.
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, name, COUNT(*) AS people
FROM employees
WHERE dept_id IS NOT NULL
GROUP BY dept_id
ORDER BY dept_id;
SELECT dept_id,
COUNT(*) AS people,
GROUP_CONCAT(name, ', ') AS names
FROM (SELECT dept_id, name FROM employees WHERE dept_id IS NOT NULL ORDER BY name)
GROUP BY dept_id
ORDER BY dept_id;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- Every selected column must be grouped or aggregated
- PostgreSQL and MySQL (by default) reject bare columns
- SQLite allows them and returns a value from an arbitrary row
- Fix by aggregating, grouping, or using a window function
Common mistakes
- Trusting a bare column result because the test data happened to look right
- Adding columns to GROUP BY until the error goes away, without checking the meaning
Follow-ups
- How would you show each employee with their department head count?
- What is functional dependency in GROUP BY?
Tips
- Ask 'which name would that be?' out loud; it explains the rule