Here is the code flow:
- First I grouped by the salary and arranged them in descending order.
- Now I decrease the value ( provided as argument ) and created an intermediate table (Salary, Position of the salary)
- 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