编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。

解法一:实际解答时先删掉函数再创建,避免报错的麻烦。
#DROP FUNCTION IF EXISTS getNthHighestSalary;
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N = N-1;
RETURN (
# Write your MySQL query statement below.
select if(count(salary)>=1,max(salary),null) as a from (
select salary from employee group by salary ORDER BY salary desc limit N,2) as t
);
END
本文介绍了一种使用SQL查询获取Employee表中第N高薪水的方法。通过创建一个函数getNthHighestSalary,该函数接收一个整数参数N,返回表中第N高的薪水。如果不存在第N高的薪水,则返回null。
5308

被折叠的 条评论
为什么被折叠?



