Accepted solution


  • 0
    X

    Sub-query will do the ranking and then simply select the object with its rank = n

    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
      RETURN (
          # Write your MySQL query statement below.
          SELECT e2.Salary as SecondHighestSalary 
          FROM (
          SELECT e.Salary, (SELECT COUNT(DISTINCT e1.Salary) FROM Employee e1
          WHERE e1.Salary >= e.Salary) as "Rank"
          FROM Employee e) e2
          WHERE e2.Rank = n
          LIMIT 1
          
      );
    END
    

Log in to reply
 

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