177. Nth Highest Salary

60 篇文章 1 订阅

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.

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

代码

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE M INT;
SET M=N-1;
  RETURN (
      # Write your MySQL query statement below.
      SELECT DISTINCT Salary 
      FROM Employee 
      ORDER BY Salary DESC 
      LIMIT M, 1
  );
END

sql中limit使用方法

1、下面是几种limit的方法:

在数据库中很多地方都会用到,比如当你数据库查询记录有几万、几十万时使用limit查询效率非常快,只需要查询出你需要的数据就可以了·再也不用全表查询导致查询数据库崩溃的情况。

select * from Customer LIMIT 10;--检索前10行数据,显示1-10条数据
select * from Customer LIMIT 1,10;--检索从第2行开始,累加10条id记录,共显示id为2....11
select * from Customer limit 5,10;--检索从第6行开始向前加10条数据,共显示id为6,7....15
select * from Customer limit 6,10;--检索从第7行开始向前加10条记录,显示id为7,8...16

2、sql中update的用法

需求:如果我想更新id=1的status为1,id不为1的status为0 ,且id有外键

update AccountStatus a set a.statusSource=(case when a.statusSource =1 then 1  else  0 end )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值