Skip to content

Conversation

@Priyam2773
Copy link
Owner

PR Title Format: 176.NthHighestSalary.cpp

Intuition

The problem asks to find the Nth highest salary from the Employee table. A direct approach is to use SQL concepts like ORDER BY with LIMIT and OFFSET, but in LeetCode-style C++ SQL implementation, we use a subquery to skip the top (N-1) distinct salaries and then select the next highest.

The key idea is:

Sort distinct salaries in descending order.

Skip the first (N-1) salaries.

Return the next one.

Approach

We use an SQL query wrapped in a C++ function:

The inner query selects all distinct salaries in descending order.

The LIMIT 1 OFFSET N-1 ensures we fetch the Nth highest.

If N is greater than the number of distinct salaries, we return NULL.

In LeetCode’s context, the function must return a query string.

Code Solution (C++)
// 176.NthHighestSalary.cpp

class Solution {
public:
string nthHighestSalary(int N) {
return "SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET " + to_string(N - 1);
}
};

Related Issues

Closes SjxSubham#176

By submitting this PR, I confirm that:

This is my original work not totally AI generated

I have tested the solution thoroughly on LeetCode

I have maintained proper PR description format

This is a meaningful contribution, not spam

Implement a function to retrieve the Nth highest salary from the Employee table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants