Share of department payroll
A free hard SQL practice problem. Solve it in SQL and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.
- Difficulty
- Hard
- Type
- SQL query
- Languages
- SQL
Problem
For every employee with a department: department name, employee name, salary, their share of the department's total payroll as a percentage rounded to 1 decimal, and their salary minus the department's average salary rounded to a whole number. Order by department name, salary (highest first), then employee name.
Database schema
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT,
department_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER NOT NULL,
hired_on TEXT NOT NULL -- 'YYYY-MM-DD'
);
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers(id),
ordered_on TEXT NOT NULL, -- 'YYYY-MM-DD'
amount REAL NOT NULL
);Expected output for the sample data
| name | name | salary | share | vs_avg |
|---|---|---|---|---|
| Engineering | Chen Kim | 130000 | 38.2 | 16667 |
| Engineering | Amina Otieno | 120000 | 35.3 | 6667 |
| Engineering | Brian Silva | 90000 | 26.5 | -23333 |
| Sales | Emeka Mensah | 61000 | 50.4 | 500 |
| Sales | Dalia Haddad | 60000 | 49.6 | -500 |
Row order matters.
How checking works
Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and several datasets. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.