CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN SET N = N-1; RETURN ( # Where Salary is your column name and Employee your table SELECT DISTINCT(Salary) FROM Employee ORDER BY Salary DESC LIMIT N, 1 ); END