1168ms solution without filesort and temptables (loop)


  • 0
    K

    Using where:

    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
        DECLARE mx INT;
        DECLARE v1 INT DEFAULT N;
        
        SET mx = (SELECT MAX(`Salary`) FROM `Employee`);
    
        loop1: LOOP
          SET v1 = v1 - 1;      
          
          IF v1 > 0 THEN
            SET mx = (SELECT MAX(`Salary`) FROM `Employee` WHERE `Salary` < mx);
            ITERATE loop1;
          END IF;
          
          LEAVE loop1; 
        END LOOP loop1;
    
      RETURN mx;
    END

Log in to reply
 

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