Full answer
GROUP BY answers "one row per department". A window function answers "every employee, plus something about their department".
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');With GROUP BY, the individual employees disappear:
SELECT dept_id, ROUND(AVG(salary), 0) AS dept_avg
FROM employees GROUP BY dept_id ORDER BY dept_id;| dept_id | dept_avg |
|---|---|
| NULL | NULL |
| 1 | 103333.0 |
| 2 | 66000.0 |
| 3 | 51500.0 |
With a window, each employee stays and gets the department figures alongside:
SELECT name, dept_id, salary,
ROUND(AVG(salary) OVER (PARTITION BY dept_id), 0) AS dept_avg,
COUNT(*) OVER (PARTITION BY dept_id) AS dept_size
FROM employees
ORDER BY dept_id, salary DESC, name;| name | dept_id | salary | dept_avg | dept_size |
|---|---|---|---|---|
| Lena | NULL | NULL | NULL | 1 |
| Asha | 1 | 120000 | 103333.0 | 3 |
| Mei | 1 | 95000 | 103333.0 | 3 |
| Ravi | 1 | 95000 | 103333.0 | 3 |
| Tom | 2 | 70000 | 66000.0 | 2 |
| Sara | 2 | 62000 | 66000.0 | 2 |
| Diego | 3 | 55000 | 51500.0 | 2 |
| Omar | 3 | 48000 | 51500.0 | 2 |
PARTITION BY dept_id splits rows into groups for the calculation without merging them. An empty OVER () treats the whole result as one partition. Lena has no department, so her partition is the NULL group with only herself in it.
Window functions are evaluated after WHERE, GROUP BY and HAVING, and before ORDER BY and LIMIT. That position explains two rules: you cannot use a window function in WHERE, and you can apply one to the result of a GROUP BY.
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, ROUND(AVG(salary), 0) AS dept_avg
FROM employees GROUP BY dept_id ORDER BY dept_id;
SELECT name, dept_id, salary,
ROUND(AVG(salary) OVER (PARTITION BY dept_id), 0) AS dept_avg,
COUNT(*) OVER (PARTITION BY dept_id) AS dept_size
FROM employees
ORDER BY dept_id, salary DESC, name;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- OVER (...) makes an aggregate a window function
- Window functions keep every input row
- PARTITION BY groups rows for the calculation only
- They run after GROUP BY and HAVING, before ORDER BY
Common mistakes
- Expecting PARTITION BY to reduce the number of rows
- Putting a window function in WHERE
Follow-ups
- How would you show each employee's salary as a percentage of their department total?
- Can you combine GROUP BY and a window function in one query?
Tips
- Show the same data with GROUP BY and with OVER side by side