← Back to SQL, page 7

sql-0126 · Medium mid

What is the WINDOW clause, and when does it help?

The WINDOW clause names a window definition once, for example WINDOW w AS (PARTITION BY dept_id ORDER BY salary DESC), so several functions can use OVER w. It removes repetition and guarantees every function uses the same partition and order. SQLite (3.28+) and PostgreSQL support it.

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;
namedept_idsalaryrnrnkrunning_dept_total
Asha112000011120000
Mei19500022215000
Ravi19500033310000
Tom2700001170000
Sara26200022132000
Diego3550001155000
Omar34800022103000

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