← Back to SQL, page 7

sql-0125 · Medium mid

Rank departments by their total salary bill. Can a window function be applied to the result of GROUP BY?

Yes. Window functions run after GROUP BY, so they can take aggregates as input: RANK() OVER (ORDER BY SUM(salary) DESC). The grouped rows become the input rows of the window.

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 d.name,
       SUM(e.salary)                         AS total,
       RANK() OVER (ORDER BY SUM(e.salary) DESC) AS rnk,
       ROUND(100.0 * SUM(e.salary) / SUM(SUM(e.salary)) OVER (), 1) AS share_pct
FROM employees e
JOIN departments d ON d.id = e.dept_id
GROUP BY d.id, d.name
ORDER BY rnk;
nametotalrnkshare_pct
Engineering310000156.9
Sales132000224.2
Support103000318.9

The query groups employees into departments, computes SUM(salary) per group, and then ranks those group rows. SUM(SUM(salary)) OVER () in the same query adds up the grouped sums to get a grand total for the share column.

Nesting reads oddly at first, so remember the order: the inner SUM belongs to GROUP BY and produces one value per department; the outer function belongs to the window and works across department rows.

A CTE makes the two steps explicit if a team finds the nested form hard to read:

sql
WITH totals AS (
  SELECT d.name, SUM(e.salary) AS total
  FROM employees e JOIN departments d ON d.id = e.dept_id
  GROUP BY d.id, d.name
)
SELECT name, total, RANK() OVER (ORDER BY total DESC) AS rnk
FROM totals
ORDER BY rnk;
nametotalrnk
Engineering3100001
Sales1320002
Support1030003
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 d.name,
       SUM(e.salary)                         AS total,
       RANK() OVER (ORDER BY SUM(e.salary) DESC) AS rnk,
       ROUND(100.0 * SUM(e.salary) / SUM(SUM(e.salary)) OVER (), 1) AS share_pct
FROM employees e
JOIN departments d ON d.id = e.dept_id
GROUP BY d.id, d.name
ORDER BY rnk;

WITH totals AS (
  SELECT d.name, SUM(e.salary) AS total
  FROM employees e JOIN departments d ON d.id = e.dept_id
  GROUP BY d.id, d.name
)
SELECT name, total, RANK() OVER (ORDER BY total DESC) AS rnk
FROM totals
ORDER BY rnk;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Window functions run after GROUP BY
  • They can take aggregates as arguments
  • SUM(SUM(x)) OVER () gives a grand total of group sums
  • A CTE separates the steps for readability

Common mistakes

  • Thinking a window function and GROUP BY cannot be in the same query
  • Ranking before grouping and getting one rank per employee

Follow-ups

  • How would you show only the top two departments?
  • How would you add each department's share of the total?

Tips

  • Describe the evaluation order to justify the nested aggregate