SQL problemINTERMEDIATEDifficulty: INTERMEDIATEWindow functions~8 min

Second highest salary per department

Return the second highest salary in each department. If a department has fewer than two distinct salaries, it should not appear.

Handle ties correctly: if two people share the highest salary, the second highest is the next distinct value below it.

Given

employees

column type
employee_id int
department_id int
name text
salary numeric

Starting point

sql
SELECT
    -- your query here
FROM employees

Go deeper on this

Cheat sheetsFree

SQL Interview Pack

40 SQL problems with worked solutions and the reasoning interviewers are listening for.

240 downloads
Email required
SQL Interview

What is the difference between RANK, DENSE_RANK and ROW_NUMBER?

They differ only in how they handle ties: ROW_NUMBER never ties, RANK ties and skips, DENSE_RANK ties and does not skip.

BeginnerDifficulty: Beginner