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');The query as written:
sql
SELECT name, dept_id, salary, ROUND(100.0 * salary / SUM(salary) OVER (), 1) AS pct_of_dept FROM employees WHERE dept_id IS NOT NULL ORDER BY dept_id, salary DESC;| name | dept_id | salary | pct_of_dept |
|---|---|---|---|
| Asha | 1 | 120000 | 22.0 |
| Ravi | 1 | 95000 | 17.4 |
| Mei | 1 | 95000 | 17.4 |
| Tom | 2 | 70000 | 12.8 |
| Sara | 2 | 62000 | 11.4 |
| Diego | 3 | 55000 | 10.1 |
| Omar | 3 | 48000 | 8.8 |
Every share is computed against the total of all departments, 545000, so Asha shows 22.0 percent instead of her share of Engineering. The column name promises one thing and the window computes another.
A corrected version:
sql
SELECT name, dept_id, salary, ROUND(100.0 * salary / SUM(salary) OVER (PARTITION BY dept_id), 1) AS pct_of_dept FROM employees WHERE dept_id IS NOT NULL ORDER BY dept_id, salary DESC;| name | dept_id | salary | pct_of_dept |
|---|---|---|---|
| Asha | 1 | 120000 | 38.7 |
| Ravi | 1 | 95000 | 30.6 |
| Mei | 1 | 95000 | 30.6 |
| Tom | 2 | 70000 | 53.0 |
| Sara | 2 | 62000 | 47.0 |
| Diego | 3 | 55000 | 53.4 |
| Omar | 3 | 48000 | 46.6 |
With the partition, shares within each department add up to about 100: Asha is 38.7 percent of Engineering, Tom 53.0 percent of Sales and Diego 53.4 percent of Support. Rounding each share can make the sum 99.9 or 100.1, which reports should either accept or correct by adjusting the largest share.
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, ROUND(100.0 * salary / SUM(salary) OVER (), 1) AS pct_of_dept FROM employees WHERE dept_id IS NOT NULL ORDER BY dept_id, salary DESC;
SELECT name, dept_id, salary, ROUND(100.0 * salary / SUM(salary) OVER (PARTITION BY dept_id), 1) AS pct_of_dept FROM employees WHERE dept_id IS NOT NULL ORDER BY dept_id, salary DESC;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Window totals need the right partition
- OVER () is the grand total
- Multiply by 100.0 first
- Rounded shares may not sum to exactly 100
Common mistakes
- Mixing grand totals and group totals
- Integer division in percentages
Follow-ups
- How would you show both department and company shares?
- How would you fix rounding so shares sum to 100?
Tips
- Check that shares in a group sum to 100