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;| name | dept_id | dept_headcount |
|---|---|---|
| Lena | NULL | 8 |
| Asha | 1 | 8 |
| Mei | 1 | 8 |
| Ravi | 1 | 8 |
| Sara | 2 | 8 |
| Tom | 2 | 8 |
| Diego | 3 | 8 |
| Omar | 3 | 8 |
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;| name | dept_id | dept_headcount |
|---|---|---|
| Lena | NULL | 1 |
| Asha | 1 | 3 |
| Mei | 1 | 3 |
| Ravi | 1 | 3 |
| Sara | 2 | 2 |
| Tom | 2 | 2 |
| Diego | 3 | 2 |
| Omar | 3 | 2 |
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