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');sql
SELECT name, RANK() OVER (ORDER BY SUM(salary) OVER (PARTITION BY dept_id) DESC) FROM employees; -- error on purposetext
Error: misuse of window function SUM()sql
SELECT name, RANK() OVER (ORDER BY SUM(salary) OVER (PARTITION BY dept_id) DESC) FROM employees; -- error on purposetext
Error: window functions are not allowed in window definitionsSplit the steps:
sql
WITH t AS (
SELECT name, dept_id, SUM(salary) OVER (PARTITION BY dept_id) AS dept_total FROM employees WHERE dept_id IS NOT NULL
)
SELECT name, dept_total, DENSE_RANK() OVER (ORDER BY dept_total DESC) AS dept_rank
FROM t ORDER BY dept_rank, name;| name | dept_total | dept_rank |
|---|---|---|
| Asha | 310000 | 1 |
| Mei | 310000 | 1 |
| Ravi | 310000 | 1 |
| Sara | 132000 | 2 |
| Tom | 132000 | 2 |
| Diego | 103000 | 3 |
| Omar | 103000 | 3 |
The CTE computes each employee's department total; the outer query ranks those totals. This is the same staging pattern used to filter on window results: each window layer gets its own query level.
Contrast with RANK() OVER (ORDER BY SUM(salary)) in a grouped query, which is allowed, because SUM there is an ordinary aggregate evaluated by GROUP BY before any window function runs.
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');
-- error on purpose
SELECT name, RANK() OVER (ORDER BY SUM(salary) OVER (PARTITION BY dept_id) DESC) FROM employees;
WITH t AS (
SELECT name, dept_id, SUM(salary) OVER (PARTITION BY dept_id) AS dept_total FROM employees WHERE dept_id IS NOT NULL
)
SELECT name, dept_total, DENSE_RANK() OVER (ORDER BY dept_total DESC) AS dept_rank
FROM t ORDER BY dept_rank, name;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- Window functions cannot be nested
- Use a CTE or subquery for each window layer
- Aggregates inside windows are allowed with GROUP BY
- Both SQLite and PostgreSQL reject nesting
Common mistakes
- Trying to rank a window result in the same SELECT
- Confusing window nesting with aggregate nesting
Follow-ups
- How would you rank departments by headcount and salary together?
- Why is SUM(SUM(x)) OVER () allowed?
Tips
- Separate the two layers clearly