Nth Highest Salary - Problem

You're working with a company's Employee database that contains salary information. Your task is to find the nth highest distinct salary from the Employee table.

The Employee table structure:

Column NameType
idint
salaryint

Important points:

  • id is the primary key with unique values
  • You need to find distinct salaries (duplicates don't count)
  • If there are fewer than n distinct salaries, return null
  • The ranking is from highest to lowest (1st highest, 2nd highest, etc.)

Example: If salaries are [100, 200, 300, 200], the distinct salaries in order are [300, 200, 100]. So the 2nd highest distinct salary is 200.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Employee table: [{"id": 1, "salary": 100}, {"id": 2, "salary": 200}, {"id": 3, "salary": 300}], n = 2
โ€บ Output: 200
๐Ÿ’ก Note: The distinct salaries in descending order are [300, 200, 100]. The 2nd highest distinct salary is 200.
example_2.sql โ€” With Duplicates
$ Input: Employee table: [{"id": 1, "salary": 100}, {"id": 2, "salary": 200}, {"id": 3, "salary": 300}, {"id": 4, "salary": 200}], n = 2
โ€บ Output: 200
๐Ÿ’ก Note: The distinct salaries are [300, 200, 100]. Even though 200 appears twice, it's still the 2nd highest distinct salary.
example_3.sql โ€” Not Enough Salaries
$ Input: Employee table: [{"id": 1, "salary": 100}], n = 2
โ€บ Output: null
๐Ÿ’ก Note: There is only 1 distinct salary (100), but we need the 2nd highest. Since there aren't enough distinct salaries, we return null.

Constraints

  • The Employee table contains at least 0 rows
  • 1 โ‰ค n โ‰ค 500
  • salary values can be any integer
  • Return null if there are fewer than n distinct salaries
  • id values are unique (primary key constraint)

Visualization

Tap to expand
๐Ÿ† Database Ranking Championship๐Ÿฅ‡$3001st Place๐Ÿฅˆ$2002nd Place๐Ÿฅ‰$1003rd Place$200Tied 2ndDENSE_RANK() MagicSalary $300 โ†’ Rank 1Salary $200 โ†’ Rank 2Salary $200 โ†’ Rank 2Salary $100 โ†’ Rank 3No rank gaps!Ties handled perfectlyQuery: WHERE rank = n๐ŸŽฏ Key Insight: Why DENSE_RANK() Winsโ€ข Automatically handles salary ties by giving same rankโ€ข Maintains consecutive ranking (1,2,3) with no gapsโ€ข Database-optimized window function for best performanceโ€ข Perfect match for 'nth distinct highest' requirementRanking Logic
Understanding the Visualization
1
Gather All Scores
Collect all employee salaries from the database table
2
Apply DENSE_RANK()
Assign consecutive ranks to distinct salary values, handling ties appropriately
3
Filter by Position
Find all salaries that have exactly the nth rank
4
Return Result
Return the salary value or null if the position doesn't exist
Key Takeaway
๐ŸŽฏ Key Insight: DENSE_RANK() is the perfect tool for finding nth distinct highest values because it automatically handles duplicates while maintaining consecutive ranking, exactly matching our requirement for distinct salary positions.
Asked in
Google 85 Amazon 72 Microsoft 68 Meta 45 Apple 35 Oracle 92
89.4K Views
Very High Frequency
~15 min Avg. Time
2.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