Full answer
When a query computes several window functions over the same window, repeating the definition is noisy and easy to get subtly wrong. The WINDOW clause names it:
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, dept_id, salary,
ROW_NUMBER() OVER w AS rn,
RANK() OVER w AS rnk,
SUM(salary) OVER w AS running_dept_total
FROM employees
WHERE dept_id IS NOT NULL
WINDOW w AS (PARTITION BY dept_id ORDER BY salary DESC, name)
ORDER BY dept_id, rn;| name | dept_id | salary | rn | rnk | running_dept_total |
|---|---|---|---|---|---|
| Asha | 1 | 120000 | 1 | 1 | 120000 |
| Mei | 1 | 95000 | 2 | 2 | 215000 |
| Ravi | 1 | 95000 | 3 | 3 | 310000 |
| Tom | 2 | 70000 | 1 | 1 | 70000 |
| Sara | 2 | 62000 | 2 | 2 | 132000 |
| Diego | 3 | 55000 | 1 | 1 | 55000 |
| Omar | 3 | 48000 | 2 | 2 | 103000 |
All three functions use the same partition and order, defined once. A named window can also be extended: OVER (w ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) adds a frame to the base definition, as long as w itself has no frame.
The clause sits after HAVING and before ORDER BY. It changes nothing about the result; it is purely about readability and consistency. In code review, a named window makes it obvious that the rank and the running total are computed over the same ordering.
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, salary,
ROW_NUMBER() OVER w AS rn,
RANK() OVER w AS rnk,
SUM(salary) OVER w AS running_dept_total
FROM employees
WHERE dept_id IS NOT NULL
WINDOW w AS (PARTITION BY dept_id ORDER BY salary DESC, name)
ORDER BY dept_id, rn;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- WINDOW w AS (...) names a window definition
- Several functions can use OVER w
- A named window can be extended with a frame
- Placed after HAVING, before ORDER BY
Common mistakes
- Copying slightly different window definitions into each function
- Adding a frame to a named window that already has one
Follow-ups
- Can a named window be based on another named window?
- Does the WINDOW clause affect performance?
Tips
- Use it when three or more functions share a window