SQLMedium58% interview frequency · Last seen 2025-10
Second Highest Salary by Department
Problem
Given an `Employee` table:
| id | name | salary | department_id |
|----|--------|--------|---------------|
| 1 | Alice | 90000 | 1 |
| 2 | Bob | 85000 | 1 |
| 3 | Carol | 95000 | 2 |
Write a SQL query to return the second highest distinct salary per department. If a department has fewer than two distinct salaries, exclude it from results.
Return columns: `department_id`, `second_highest_salary`.
Common follow-ups
- How would you find the Nth highest salary?
- What indexes would you add for performance at scale?
Solution
# Solution locked # Sign in to unlock expert solutions # with multi-language code and analysis
Sign in to unlock
Create a free account to preview problems. Subscribe for full access to our curated bank — expert tutorials, follow-ups, and practice tools you won't find on public sites.