leetcode 177 Nth Highest Salary

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条。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值