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 )