Accepted Nth Highest Salary without using LIMIT offset,limit


  • 0
    0

    Here is the code flow:

    1. First I grouped by the salary and arranged them in descending order.
    2. Now I decrease the value ( provided as argument ) and created an intermediate table (Salary, Position of the salary)
    3. Finally I select that salary the position of which is zero and that is the expected salary
          CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN
                SET @count := N;
          RETURN (
        
                SELECT tt.Salary
                FROM 
                
                (
                
                        SELECT (@count := @count -1) AS c,t.Salary
                        
                        FROM 
                        (
                        	SELECT
        
                        		Salary
                        	FROM
                        		Employee
        					GROUP BY Salary
                        	ORDER BY
                        		Salary DESC
                        ) AS t
                        WHERE @count >=1
                
                ) AS tt
                
                WHERE tt.c=0
              
          );
        END

Log in to reply
 

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