My Solution with user defined variable


  • 0
    J
    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
      RETURN (
        SELECT DISTINCT r.Salary 
        From 
            (SELECT 
                e.Id as Id,
                e.Salary as Salary,
                @rank := @rank + (@prev <> @prev := e.Salary) as Rank
            FROM 
                (SELECT * FROM Employee ORDER BY Salary DESC) e,
                (SELECT @rank := 0, @prev := -1  ) init) r
        WHERE
            r.Rank = n
      );
    END
    

Log in to reply
 

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