Salary against department total

window functions

Salary against department total

Microsoft SQL Interview Question

Microsoft's finance team wants to compare each person's salary with the total salary bill of their department, side by side.

For every employee, show their name, department and salary, plus the total salary of their department as department_total. Keep one row for each employee.

Asked of

  • Data Analyst
  • BI Analyst
  • Analytics Engineer
  • Data Engineer

employeesTable16 rows

Column NameType
employee_idBIGINT
nameVARCHAR
departmentVARCHAR
salaryBIGINT
manager_idBIGINT
hire_dateDATE

employeesExample Input

employee_idnamedepartmentsalarymanager_idhire_date
1Ravi KumarExecutive240000NULL2018-01-15
2Mei LinEngineering18500012018-06-04
3Sofia RossiEngineering14200022019-02-18
4James ParkEngineering12800022019-09-02
7Diego AlvarezAnalytics16800012018-10-22
8Hannah WeberAnalytics13400072020-01-13

Example Output

namedepartmentsalarydepartment_total
Diego AlvarezAnalytics168000302000
Hannah WeberAnalytics134000302000
James ParkEngineering128000455000
Mei LinEngineering185000455000
Ravi KumarExecutive240000240000
Sofia RossiEngineering142000455000

Explanation

Mei Lin, Sofia Rossi and James Park work in Engineering, and their salaries add up to 455,000. That total appears on each of their three rows. Ravi Kumar is the only person in Executive, so that department total equals Ravi Kumar's own salary.

The example above is a small slice of the data. Your query runs against the full tables.