Second Highest Salary II - Problem
Second Highest Salary II
Imagine you're working as a data analyst for a large corporation's HR department. Your company wants to identify high-performing employees who earn the second-highest salary in each department for potential promotions or special recognition.
Given an
The Challenge:
• Some departments might have only one unique salary level (no second-highest)
• Multiple employees can have the same salary
• We need to rank salaries within each department separately
Goal: Return employee IDs and departments for all second-highest earners, ordered by employee ID ascending.
Imagine you're working as a data analyst for a large corporation's HR department. Your company wants to identify high-performing employees who earn the second-highest salary in each department for potential promotions or special recognition.
Given an
employees table with employee IDs, salaries, and departments, your task is to find all employees who earn the second-highest salary within their respective departments. If multiple employees share the same second-highest salary, include them all!The Challenge:
• Some departments might have only one unique salary level (no second-highest)
• Multiple employees can have the same salary
• We need to rank salaries within each department separately
Goal: Return employee IDs and departments for all second-highest earners, ordered by employee ID ascending.
Input & Output
example_1.sql — Basic Department Analysis
$
Input:
employees = [[1,70000,'Sales'],[2,80000,'Sales'],[3,80000,'Sales'],[4,90000,'Sales'],[5,55000,'IT'],[6,65000,'IT'],[7,65000,'IT']]
›
Output:
[[2,'Sales'],[3,'Sales'],[5,'IT']]
💡 Note:
Sales: 90K(rank 1) > 80K(rank 2) > 70K(rank 3). Employees 2,3 both earn 80K (2nd highest). IT: 65K(rank 1) > 55K(rank 2). Employee 5 earns 55K (2nd highest).
example_2.sql — Department with Single Employee
$
Input:
employees = [[1,50000,'HR'],[2,60000,'Finance'],[3,70000,'Finance']]
›
Output:
[[2,'Finance']]
💡 Note:
HR has only one employee, so no second-highest salary exists. Finance: 70K(rank 1) > 60K(rank 2). Employee 2 earns the second-highest salary in Finance.
example_3.sql — All Employees Same Salary
$
Input:
employees = [[1,50000,'Tech'],[2,50000,'Tech'],[3,50000,'Tech']]
›
Output:
[]
💡 Note:
All employees in Tech department earn the same salary (50K), so they all have rank 1. There is no second-highest salary, hence empty result.
Visualization
Tap to expand
Understanding the Visualization
1
Partition by Department
Separate employees into department-specific groups for independent analysis
2
Rank Salaries
Within each department, assign ranks based on salary (highest = rank 1)
3
Handle Ties
DENSE_RANK ensures employees with same salary get the same rank
4
Filter Second-Highest
Select only employees with rank 2 (second-highest salary)
5
Sort Results
Order final results by employee ID for consistent output
Key Takeaway
🎯 Key Insight: Window functions like DENSE_RANK() with PARTITION BY allow us to perform complex ranking operations across different groups in a single, efficient database operation, making department-wise salary analysis both elegant and performant.
Time & Space Complexity
Time Complexity
O(n log n)
Single scan with sorting for ranking within partitions
⚡ Linearithmic
Space Complexity
O(n)
Space for ranking computation and result storage
⚡ Linearithmic Space
Constraints
- 1 ≤ employees.length ≤ 1000
- 1 ≤ emp_id ≤ 104
- 1 ≤ salary ≤ 106
- 1 ≤ dept.length ≤ 50
- emp_id values are unique
- dept consists of lowercase and uppercase English letters
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code