Skip to main content

Command Palette

Search for a command to run...

LeetCode 177: Nth Highest Salary

How to Solve LeetCode 177: Nth Highest Salary

Updated
2 min read
V

I'm Varchasv, a Data Engineer working on enterprise data integration.

Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.

What I'm doing:

  • Solving 2 Leetcode problems daily (SQL + DSA).
  • Blogging about each problem.
  • Building in public.

My Goal - Land a better data engineering role by mid-2026.

Follow my journey !!

Date: February 02, 2026
Category: SQL
Time Taken: 10 minute
Difficulty: Medium


Problem Statement

Write a solution to find the n<sup>th</sup> highest distinct salary from the Employee table. If there are less than n distinct salaries, return null.

Link: Nth Highest Salary


Approach 1 (Window Functions) [Best Solution]

  • The first solution that came to mind is using window functions. We can use DENSE_RANK() to assign the same rank to identical salaries.

  • Then, we use this as a subquery within a larger query to get the DISTINCT highest salary, as we only need the Nth highest salary, not how many people have that salary.

  • Since we use it as a subquery, the outer query will automatically return NULL if there is no Nth highest salary.

Solution Code:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
    SELECT DISTINCT salary AS getNthHighestSalary
    FROM (
        SELECT 
            id, 
            salary, 
            DENSE_RANK() OVER(ORDER BY salary DESC) AS rnk
        FROM Employee) AS ranks
    WHERE rnk = N
  );
END

Approach 2 (Window Functions) [Faster and indexable]

  • We can also use OFFSET and LIMIT together to find the Nth highest salary, but this method is more technical and not as straightforward.

  • We will use SET to adjust N to N-1 because OFFSET starts at 0. So, the 2nd highest salary would be at Offset 1.

Solution Code:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N = N - 1;
RETURN (
    SELECT DISTINCT salary AS getNthHighestSalary
    FROM Employee
    ORDER BY salary DESC LIMIT 1 OFFSET N
  );
END

Pattern: Window Functions

Mistakes I Made: I did not use DISTINCT originally and my answer gave multiple rows where it expected only 1.

Series: 90 Days of Data Engineering Progress: 28/90 problems completed

Tags: #DEQuest #LeetCode #SQL #DataEngineering #BuildInPublic

90 Days of Data Engineering

Part 29 of 48

Solving 90 problems over 18 weeks. Daily posts Mon-Fri. Goal: Switch to development role.

Up next

LeetCode 704: Binary Search

Solving LeetCode 704: A Guide to Binary Search