[LeetCode]-DataBase-Nth Highest Salary
Posted 练子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode]-DataBase-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高的工资
CREATE TABLE Employee(
Id TINYINT UNSIGNED,
Salary DECIMAL(10,2)
)ENGINE=MyISAM CHARSET=utf8;
-- sql 使用 limit 和 ORDER BY
DROP FUNCTION IF EXISTS getNthHighestSalary;
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
以上是关于[LeetCode]-DataBase-Nth Highest Salary的主要内容,如果未能解决你的问题,请参考以下文章