← Back to SQL, page 25

sql-0481 · Medium mid

Each employee's share of their department's salary bill comes out tiny. Find the mistake.

The denominator SUM(salary) OVER () is the company-wide total, not the department total. Use SUM(salary) OVER (PARTITION BY dept_id) so each salary is divided by its own department's bill, and multiply by 100.0 to avoid integer division.

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;
namedept_idsalarypct_of_dept
Asha112000022.0
Ravi19500017.4
Mei19500017.4
Tom27000012.8
Sara26200011.4
Diego35500010.1
Omar3480008.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;
namedept_idsalarypct_of_dept
Asha112000038.7
Ravi19500030.6
Mei19500030.6
Tom27000053.0
Sara26200047.0
Diego35500053.4
Omar34800046.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