标签:write sql UNC rom cti col loading select pre
编写一个 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 declare m int; SET m = N - 1; RETURN ( # Write your MySQL query statement below. SELECT ( SELECT DISTINCT Salary FROM employee ORDER BY Salary DESC LIMIT m,1 ) AS getNthHighestSalary ); END
SELECT * FROM employee ORDER BY Salary ASC LIMIT 3,2;
从当前的第三个之后的第四位开始往后数两位
SELECT * FROM employee ORDER BY Salary ASC LIMIT 2 OFFSET 3;
从当前数据中选取2个,从第三位往后数,就是第四位开始
标签:write sql UNC rom cti col loading select pre
原文地址:https://www.cnblogs.com/mengjie1001/p/9640641.html