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 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
Department Salary Ranking ProcessSales Department BuildingFloor 1: 90K (Emp 4)Floor 2: 80K (Emp 2,3) ✓Floor 3: 70K (Emp 1)IT Department BuildingFloor 1: 65K (Emp 6,7)Floor 2: 55K (Emp 5) ✓Marketing BuildingFloor 1: 55K (Emp 9)Floor 2: 50K (Emp 8) ✓Window Function Query Execution:DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary DESC)1. PARTITION BY dept: Creates separate ranking context for each department2. ORDER BY salary DESC: Ranks from highest to lowest salary3. DENSE_RANK(): Handles ties by assigning same rank to equal salariesFinal Result Processing:• Filter WHERE salary_rank = 2 to get second-highest earners• Include ALL employees with rank 2 (handles ties automatically)📊 Output: [(2,'Sales'), (3,'Sales'), (5,'IT'), (8,'Marketing')]
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

n
2n
Linearithmic
Space Complexity
O(n)

Space for ranking computation and result storage

n
2n
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
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28 Apple 22 Netflix 18
42.3K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen