CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
SELECT e1.Salary
FROM (SELECT DISTINCT Salary FROM Employee) e1
WHERE (SELECT COUNT(*) FROM (SELECT DISTINCT Salary FROM Employee) e2 WHERE e2.Salary > e1.Salary) = N  1
LIMIT 1
);
END
No Variable, No Limit X,1, Just one query, 808ms


@chriswho when I remove
limit 1
, it returns more than one row. So maybe there are more than one highest score we should only keep one result.By the way, this is my similarly solution:
BEGIN RETURN ( # Write your MySQL query statement below. select distinct Salary from Employee e where N=(select count(distinct Salary) from Employee where Salary>=e.Salary) limit 1 ); END

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
SELECT e1.Salary
FROM (SELECT DISTINCT Salary FROM Employee) e1
WHERE N1=(SELECT COUNT(DISTINCT e2.Id) FROM Employee e2 WHERE e2.Salary>e1.Salary)
);
END
This is a slight variation but I can't figure out why it doesn't work! I'm counting the number of distinct Id instead of counting number of rows. Why does this produce an error?
