Table: Employee
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | salary | int | +-------------+------+ id is the primary key (column with unique values) for this table. Each row of this table contains information about the salary of an employee.
Write a solution to find the nth highest salary from the Employee table. If there is no nth highest salary, return null.
The result format is in the following example.
Example 1:
Input: Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ n = 2 Output: +------------------------+ | getNthHighestSalary(2) | +------------------------+ | 200 | +------------------------+
Example 2:
Input: Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | +----+--------+ n = 2 Output: +------------------------+ | getNthHighestSalary(2) | +------------------------+ | null | +------------------------+
Problem Overview: Given an Employee table containing salaries, return the Nth highest distinct salary. If the table does not contain N unique salaries, the query should return NULL. The main challenge is handling duplicate salaries while correctly identifying the Nth rank.
Approach 1: ORDER BY with LIMIT and OFFSET (Time: O(n log n), Space: O(1))
This approach sorts all salaries in descending order and skips the first N-1 distinct entries using LIMIT and OFFSET. The query typically wraps the ordered results inside a subquery and selects the Nth row. Sorting dominates the cost, giving O(n log n) time complexity, while the query itself uses constant extra space.
The key detail is ensuring duplicate salaries don't affect ranking. This is usually handled with DISTINCT before applying the sort. The database engine performs the sort operation, then offsets rows until it reaches the desired position. This solution is concise and works well when you simply need the Nth value from a sorted dataset.
Problems like this commonly appear in SQL and database interview rounds because they test understanding of sorting, filtering, and result pagination.
Approach 2: Subquery with DENSE_RANK Window Function (Time: O(n log n), Space: O(n))
This method assigns a rank to each salary using the DENSE_RANK() window function ordered by salary descending. Employees with the same salary receive the same rank, which naturally handles duplicates. After ranking, a subquery filters rows where rank = N.
The database first sorts salaries to compute the window function, which takes O(n log n) time. Storing ranking metadata requires additional memory, leading to O(n) space. Despite this overhead, the logic is clearer and explicitly models the ranking problem.
Window functions are heavily used in analytical queries and appear frequently in advanced SQL window function problems. This approach scales well when additional ranking logic or grouped analytics are required.
Recommended for interviews: Both solutions are valid, but interviewers often prefer the DENSE_RANK() solution because it demonstrates strong knowledge of SQL window functions and ranking semantics. The LIMIT/OFFSET approach shows you understand sorting and pagination, but the ranking-based solution better handles duplicates and mirrors how real analytical queries are written.
This approach uses the SQL query constructs to fetch the nth highest salary directly from the database. We will utilize ORDER BY, DISTINCT, LIMIT, and OFFSET to achieve this.
Steps:
SELECT DISTINCT.ORDER BY salary DESC.LIMIT and OFFSET to skip the first n-1 results and take the nth result if it exists.This function is a stub that generates an SQL query string. The actual execution with parameters would be performed using an SQL interface in C like SQLite or MySQL Connector C API.
C++
Java
Python
C#
JavaScript
In this approach, we will use the DENSE_RANK() window function available in SQL to assign ranks to salaries based on their value.
Steps:
DENSE_RANK by partitioning properly.n.This function generates an SQL query string that uses the DENSE_RANK function to assign ranks and filter out the nth highest salary.
C++
Java
Python
C#
JavaScript
| Approach | Complexity |
|---|---|
| Use SQL Query with LIMIT and OFFSET for Nth Highest Salary |
|
| Use Subquery with DENSE_RANK to Identify Nth Highest Salary |
|
| Approach | Time | Space | When to Use |
|---|---|---|---|
| ORDER BY with LIMIT and OFFSET | O(n log n) | O(1) | When you only need the Nth value after sorting and want a concise query |
| Subquery with DENSE_RANK Window Function | O(n log n) | O(n) | When duplicate values must share the same rank or when writing analytical SQL queries |
Lec-70: Find Nth(1st,2nd,3rd....N) Highest Salary in SQL | Imp for Competitive & Placement exam • Gate Smashers • 622,466 views views
Watch 9 more video solutions →Practice Nth Highest Salary with our built-in code editor and test cases.
Practice on FleetCode