CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
select distinct e1.Salary
from Employee e1 left join Employee e2 on e1.Salary<e2.Salary
group by e1.Salary
having count(distinct e2.Salary)=N-1
);
END
以为是count会算null值,但是count会忽略空值
给函数加一个分支。
如果是NULL,走其他语句
用分析函数
select salary
from (select distinct dense_rank(salary) over(order by salary desc nulls last) nn,
salary
from employee)
where nn = n