Write a SQL query to get the nth highest salary from the Employee
table.
+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
For example, given the above Employee table, the nth highest salary where n = 2 is 200
. If there is no nth highest salary, then the query should return null
.
题目大意,给出一张表 若有第n大的Salary 则求出,没有则返回
create function getNthHighestSalary(N INT)
returns INT
Begin
declare M INT;
set M=N-1;
return(select distinct Salary from Employee Order by Salary desc limit M,1);
End
我提的问题
数据库问题 题目大意: 编写SQL查询获取雇员表中的第n高薪水值。 如Employee 表 Id Salary 1 100 2 200 3 300 例如,给定上面的雇员表,当n为2时,第n高薪水为200.如果没有第n高薪水,查询返回null。 代码已经给出 Creat function getNthHighestSalary(N INT) Returns INT Begin Declare M INT ; Set M=N-1; Return (Select distinct Salary from Employee order by Salary desc limit M,1 ); END 这道nth highest salary 我有些疑问想问问,因为刚学,好多知识都停留在书本,用起来的时候还是不是很得心应手。 一,在mysql中sql也可以建立函数吗?二、m与n的含义是什么?我的理解是‘m是要返回n的前一列’,‘n是要返回的第n大的salary’,不知道这样理解对不对。三、getNthHighestSalary(N INT)其中的N是形式参数吗? 四,能否就这道题,解释一下limit 的用法。 先谢谢回复的各位
1应该说所有的数据库管理系统都支持“函数”。2你看m的声明就知道了,就是n-1,没什么其他的意义,只是因为limit x,y,x是从0开始。3只要理解limit就可以了。limit m,1表示第m+1条(其实就是n)开始,取1条。