Nth Highest Salary


  • 0
    L

    two ways all can beat everyone~~~~

    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
      RETURN (
          
          # select distinct getNthHighestSalary
          # from
          #     (select e.salary as getNthHighestSalary,
          #           (select count(distinct salary)
          #                      from employee
          #                      where salary >= e.salary) rank 
          #     from employee e) t
          # where rank = N
          
          select distinct e1.salary as getNthHighestSalary
          from employee e1, employee e2
          where e1.salary <= e2.salary
          group by e1.id
          having count(distinct e2.salary) = N
          
          
      );
    END
    

Log in to reply
 

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