← Back to SQL, page 4

sql-0073 · Medium mid

Why does SELECT dept_id, name, COUNT(*) FROM employees GROUP BY dept_id fail in PostgreSQL but run in SQLite?

In a grouped query each output row stands for a whole group, so every selected column must be either in GROUP BY or inside an aggregate. name is neither, and a department has several names, so PostgreSQL refuses. SQLite accepts it and returns the name from an arbitrary row of each group, which is a silent source of wrong answers. MySQL rejects it by default through ONLY_FULL_GROUP_BY.

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:

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, name, COUNT(*) FROM employees GROUP BY dept_id;  -- error on purpose
text
Error: column "employees.name" must appear in the GROUP BY clause or be used in an aggregate function

SQLite allows these "bare columns" and picks a value from some row in the group:

sql
SELECT dept_id, name, COUNT(*) AS people
FROM employees
WHERE dept_id IS NOT NULL
GROUP BY dept_id
ORDER BY dept_id;
dept_idnamepeople
1Asha3
2Tom2
3Diego2

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:

sql
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_idpeoplenames
13Asha, Mei, Ravi
22Sara, Tom
32Diego, 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.

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, 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