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

Second highest salary per department

Easy·Acceptance 100%·Asked 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

Example

employee_id department_id salary 1 10 90000 2 10 90000 3 10 70000 4 20 50000 Output department_id second_salary 10 70000 20 null

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

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.