← Back to SQL, page 6

sql-0113 · Easy fresher

What is a window function, and how is it different from GROUP BY?

A window function computes a value across a set of rows related to the current row, written with OVER (...), but it keeps every row in the output. GROUP BY collapses each group into one row. So AVG(salary) OVER (PARTITION BY dept_id) can show each employee next to their department's average, which GROUP BY alone cannot.

Full answer

GROUP BY answers "one row per department". A window function answers "every employee, plus something about their department".

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');

With GROUP BY, the individual employees disappear:

sql
SELECT dept_id, ROUND(AVG(salary), 0) AS dept_avg
FROM employees GROUP BY dept_id ORDER BY dept_id;
dept_iddept_avg
NULLNULL
1103333.0
266000.0
351500.0

With a window, each employee stays and gets the department figures alongside:

sql
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;
namedept_idsalarydept_avgdept_size
LenaNULLNULLNULL1
Asha1120000103333.03
Mei195000103333.03
Ravi195000103333.03
Tom27000066000.02
Sara26200066000.02
Diego35500051500.02
Omar34800051500.02

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.

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 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