Skip to content

177. Nth Highest Salary

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
    WITH
      RankedEmployees AS (
        SELECT *, DENSE_RANK() OVER(ORDER BY salary DESC) AS `rank`
        FROM Employee
      )
    SELECT MAX(salary) AS SecondHighestSalary
    FROM RankedEmployees
    WHERE `rank` = N
  );
END