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');With a scalar subquery for the total:
sql
SELECT d.name AS dept,
SUM(e.salary) AS dept_total,
ROUND(100.0 * SUM(e.salary) / (SELECT SUM(salary) FROM employees WHERE dept_id IS NOT NULL), 1) AS pct
FROM employees e
JOIN departments d ON d.id = e.dept_id
GROUP BY d.id, d.name
ORDER BY pct DESC;| dept | dept_total | pct |
|---|---|---|
| Engineering | 310000 | 56.9 |
| Sales | 132000 | 24.2 |
| Support | 103000 | 18.9 |
The same with a window function over the grouped result. SUM(salary) is the department total; wrapping it in SUM(...) OVER () adds those totals across all groups:
sql
SELECT d.name AS dept,
SUM(e.salary) AS dept_total,
ROUND(100.0 * SUM(e.salary) / SUM(SUM(e.salary)) OVER (), 1) AS pct
FROM employees e
JOIN departments d ON d.id = e.dept_id
GROUP BY d.id, d.name
ORDER BY pct DESC;| dept | dept_total | pct |
|---|---|---|
| Engineering | 310000 | 56.9 |
| Sales | 132000 | 24.2 |
| Support | 103000 | 18.9 |
Window functions run after GROUP BY, which is why an aggregate can sit inside a window aggregate like this. It also avoids scanning the table a second time.
Two details. Lena's NULL salary is ignored by SUM, and since she has no department she would form her own group with a NULL sum; the WHERE clause leaves her out so the percentages describe known salaries only. And 100.0 * comes first: 100 * SUM(salary) / total with integers would truncate each percentage to a whole number.
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 AS dept,
SUM(e.salary) AS dept_total,
ROUND(100.0 * SUM(e.salary) / (SELECT SUM(salary) FROM employees WHERE dept_id IS NOT NULL), 1) AS pct
FROM employees e
JOIN departments d ON d.id = e.dept_id
GROUP BY d.id, d.name
ORDER BY pct DESC;
SELECT d.name AS dept,
SUM(e.salary) AS dept_total,
ROUND(100.0 * SUM(e.salary) / SUM(SUM(e.salary)) OVER (), 1) AS pct
FROM employees e
JOIN departments d ON d.id = e.dept_id
GROUP BY d.id, d.name
ORDER BY pct DESC;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Department sum divided by overall sum
- Overall sum via scalar subquery or SUM(SUM(x)) OVER ()
- Window functions run after GROUP BY
- Use 100.0 to avoid integer division
Common mistakes
- Dividing by COUNT instead of the total sum
- Integer division turning shares into 0 or whole numbers
Follow-ups
- How would you show each employee's share of their department's total?
- Do the rounded percentages always add up to 100?
Tips
- Mention why the window version avoids a second scan