← Back to SQL, page 24

sql-0478 · Easy fresher

A query meant to show each department's head count shows 8 on every row. What is wrong?

COUNT(*) OVER () counts over the whole result, because an empty OVER clause has one partition containing every row. Add PARTITION BY dept_id to count within each department, or use GROUP BY when one row per department is wanted.

Full answer
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');

The query as written:

sql
SELECT name, dept_id, COUNT(*) OVER () AS dept_headcount FROM employees ORDER BY dept_id, name;
namedept_iddept_headcount
LenaNULL8
Asha18
Mei18
Ravi18
Sara28
Tom28
Diego38
Omar38

Every employee shows 8, the total number of employees. The window has no partition, so all rows share one frame.

A corrected version:

sql
SELECT name, dept_id, COUNT(*) OVER (PARTITION BY dept_id) AS dept_headcount FROM employees ORDER BY dept_id, name;
namedept_iddept_headcount
LenaNULL1
Asha13
Mei13
Ravi13
Sara22
Tom22
Diego32
Omar32

Partitioning by department gives 3 for Engineering, 2 for Sales and Support, and 1 for Lena, whose NULL department forms its own partition. The window keeps one row per employee; if the report needs one row per department, GROUP BY dept_id is simpler and cheaper.

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 name, dept_id, COUNT(*) OVER () AS dept_headcount FROM employees ORDER BY dept_id, name;

SELECT name, dept_id, COUNT(*) OVER (PARTITION BY dept_id) AS dept_headcount FROM employees ORDER BY dept_id, name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • OVER () is one partition for all rows
  • PARTITION BY splits the count
  • NULL keys form their own partition
  • GROUP BY when one row per group is wanted

Common mistakes

  • Forgetting PARTITION BY
  • Using a window when an aggregate would do

Follow-ups

  • How would you show each employee's share of the department head count?
  • When would you keep both detail and group totals?

Tips

  • Read the OVER clause carefully