← Problems13. Second highest salary per departmentEasySQL
00:00 / 20:00

Second highest salary per department

Easy24% solve itAsked at Microsoft, Deloitte, Google

Return the second highest salary in each department. If a department has fewer than two distinct salaries, return null for that department. Ties on salary count once.

Input schema

employees employee_id bigint department_id int salary numeric

Constraints

  • Output one row per department present in employees, ordered by department_id
  • Duplicate salaries in a department are one distinct value
  • Up to 2,000,000 rows

Example 1basic two departments

Input · employees
salaryemployee_iddepartment_id
90000110
70000210
50000320
40000420
Output
department_idsecond_salary
1070000
2040000

Example 2duplicate salaries count once

Input · employees
salaryemployee_iddepartment_id
90000110
90000210
70000310
Output
department_idsecond_salary
1070000

Topics

window functionsaggregation

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.sql
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.