Best way for top N


  • 0
    R
    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
      RETURN (
          # Write your MySQL query statement below.
          IF(N > (SELECT COUNT(DISTINCT SALARY) FROM Employee), (SELECT NULL from DUAL),
            (
                Select Salary
                from
                (
                    select DISTINCT Salary
                    from Employee
                    order by Salary DESC
                    LIMIT N
                ) as temp
                ORDER BY Salary   
                LIMIT 1
            )
          )
      );
    END
    

Log in to reply
 

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