My two solutions


  • 0
    C

    638 ms:

    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
      RETURN (
          select Salary
          from
          (
              select Salary, (@rank := @rank + 1) as rank
              from
              (
                  select distinct Salary
                  from Employee
                  order by Salary desc
              ) A, (select @rank := 0) as Init
          ) as t
          where rank = N
      );
    END
    

    595 ms:

    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
      set N = N-1;
      RETURN (
          select distinct Salary
          from Employee
          order by Salary desc
          limit N, 1
      );
    END
    

Log in to reply
 

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