mediumcompany HR · Q3
DENSE_RANKdistinct valuesLEFT JOIN

Second highest salary per department

For every department, find the second highest distinct salary among its current employees.

  • if two people share the top salary, the second highest is the next salary below that, not the same number again. So salaries 100, 100, 80 give 80.
  • if a department has fewer than two distinct salaries (or no current employees at all), second_highest is NULL. The department still appears.
output columns
dept_namesecond_highest

uses ,

ctrl + enter to run on the sample data · ctrl + shift + enter to submit

run your query to see the output here. Submit checks it against the sample data, hidden test cases, random data and a large dataset.