Nth Highest Salary LeetCode Solution

Problem – Nth Highest Salary LeetCode Solution

SQL Schema

Table: Employee

+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| salary      | int  |
+-------------+------+
id is the primary key column for this table.
Each row of this table contains information about the salary of an employee.

Write an SQL query to report the nth highest salary from the Employee table. If there is no nth highest salary, the query should report null.

The query 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                   |
+------------------------+

Nth Highest Salary LeetCode Solution in MySQL

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N = N-1;
  RETURN (
      SELECT DISTINCT(salary) from Employee order by salary DESC
      LIMIT 1 OFFSET N
      
  );
END

Nth Highest Salary LeetCode Solution in MS SQL Server

CREATE FUNCTION getNthHighestSalary(@N INT) RETURNS INT AS
BEGIN
    RETURN (
		SELECT TOP 1
			Salary
		FROM (
			SELECT
				Salary,
				DENSE_RANK() OVER (ORDER BY Salary DESC) rn
			FROM
				Employee E
		) T
		WHERE rn = @N
    );
END

Nth Highest Salary LeetCode Solution in Oracle

CREATE FUNCTION getNthHighestSalary(N IN NUMBER)
RETURN NUMBER IS
v_count number;
result NUMBER;
BEGIN
   select count(*) into v_count from Employee;

   IF N > v_count THEN
      result := null;
   ELSE      
      SELECT
            max(salary) as salary /* i take max to avoid dups if there are a few the same results*/
      INTO result
      FROM (
        SELECT 
              salary
             ,dense_rank() over(order by salary desc) as rn
        FROM
             Employee
      ) T
      WHERE rn=N;
                 
   END IF;
    
    RETURN result;
END;
Nth Highest Salary LeetCode Solution Review:

In our experience, we suggest you solve this Nth Highest Salary LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Nth Highest Salary LeetCode Solution

Find on Leetcode

Conclusion:

I hope this Nth Highest Salary LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.