题目
编写一个 sql 查询,获取 employee
表中第 n 高的薪水(salary)。
+----+--------+ | id | salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
例如上述 employee
表,n = 2 时,应返回第二高的薪水 200
。如果不存在第 n 高的薪水,那么查询应返回 null
。
+------------------------+ | getnthhighestsalary(2) | +------------------------+ | 200 | +------------------------+
解法
create function getnthhighestsalary(n int) returns int begin set n = n - 1; return ( select distinct salary from employee group by salary order by salary desc limit 1 offset n ); end
摘自:
如对本文有疑问, 点击进行留言回复!!
MySQL-关系代数-并、交、差、等值连接、自然连接、左连接。。。
【MySQL牛客】10.获取所有非manager的员工emp_no
网友评论