Solution Nth Highest Salary


  • 0
    S
    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
      #row offset starts at 0, so subtract N by 1.
      declare off_set int;
      set off_set = N - 1;
      RETURN (
          #return null if row does not exist, otherwise return result of nested select.
          #order results by descending, limit to 1 row, and take offset like an array.
          select IFNULL(
            (select distinct salary 
             from Employee 
             order by salary desc limit 1 offset off_set), 
          NULL)
      );
    END
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.