Skip to content
RED SAGSkills

Second-Highest Salary per Department

For each department, find the second-highest distinct salary. If several people share the top salary, the second-highest is the next lower value. Departments with fewer than two distinct salaries are left out. NULL salaries are ignored.

Return columns dept, second_salary. Row order does not matter.

Schema

CREATE TABLE employees (
  emp_id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept TEXT NOT NULL,
  salary INTEGER
);

Sample data

employees

emp_idnamedeptsalary
1AdelEngineering14000
2BeaEngineering17500
3CemEngineering12000
4DinaFinance11000
5EmreFinance9500
6FaySales8000
7GusEngineering15200
Hint 1

DENSE_RANK gives tied salaries the same rank and does not skip the next number.

Hint 2

Several people can share the second-highest salary, so remove duplicate rows.

Good practice for: Data Analyst, Backend Developer, Database Administrator, Data Engineer

SQL (SQLite)

Run checks your query on the sample data (Ctrl + '). Submit also checks hidden databases.