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;| name | total | rnk | share_pct |
|---|---|---|---|
| Engineering | 310000 | 1 | 56.9 |
| Sales | 132000 | 2 | 24.2 |
| Support | 103000 | 3 | 18.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;| name | total | rnk |
|---|---|---|
| Engineering | 310000 | 1 |
| Sales | 132000 | 2 |
| Support | 103000 | 3 |
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