177. Nth Highest Salary


  • 0
    A

    Solution


    Approach: Using IFNULL and LIMIT clause [Accepted]

    • Sort the distinct salary in descend order and then utilize the LIMIT clause to get the Nth highest salary.

    • use IFNULL to solve the 'NULL' problem

    mysql

    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
          DECLARE offset_tmp INT;
          SET offset_tmp=N-1;
      RETURN (
            select ifnull((select distinct Salary from Employee order by Salary desc limit 1 offset offset_tmp),null) as getNthHighestSalary
      );
    END
    

Log in to reply
 

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