I am going to ignore the fact that depending on your data, the query as you've written it, may not return the results you expect. Instead, I'll focus on fixing the syntax errors:
- You need to redefine the default semi colon (
;
) delimiter to something else so that the semi colon inside the function body doesn't get misinterpreted. (Maybe you're doing that already, but since you're not showing it, I mention it in case)
- There shouldn't be a comma separating
LIMIT
and OFFSET
- MySQL seems to expect a single value as the
OFFSET
parameter. It doesn't seem to like a calculated expression the way you have it. In that case, you just need to calculate the desired value before using it in the SQL statement.
Applying the above, here is what it would look like:
delimiter //
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N = N - 1;
RETURN (
# Write your MySQL query statement below.
SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET N
);
END
//