← Back to SQL, page 15

sql-0300 · Medium mid

Can you nest one window function inside another, such as RANK() OVER (ORDER BY SUM(x) OVER (...))?

No. Window functions cannot be nested inside other window functions; both SQLite and PostgreSQL reject it. Compute the inner window in a subquery or CTE, then apply the outer window to its result. Aggregates can appear inside window functions, because GROUP BY runs first.

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 purpose
text
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 purpose
text
Error: window functions are not allowed in window definitions

Split 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;
namedept_totaldept_rank
Asha3100001
Mei3100001
Ravi3100001
Sara1320002
Tom1320002
Diego1030003
Omar1030003

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