Salary rank within department

window functions

Salary rank within department

Netflix SQL Interview Question

Netflix's compensation team wants to see where each person's salary sits within their own department.

For every employee, show their name, department and salary, plus their rank within their department as salary_rank, where 1 is the highest salary. Employees with equal salaries share the same rank, and the next rank is skipped.

Asked of

  • Data Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

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

namedepartmentsalarysalary_rank
Diego AlvarezAnalytics1680001
Hannah WeberAnalytics1340002
James ParkEngineering1280003
Mei LinEngineering1850001
Ravi KumarExecutive2400001
Sofia RossiEngineering1420002

Explanation

In Engineering, Mei Lin has the highest salary and ranks 1, followed by Sofia Rossi at 2 and James Park at 3. The ranking starts again in each department, so Diego Alvarez is also 1 in Analytics.

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