码迷,mamicode.com
首页 > 数据库 > 详细

[LeetCode]-DataBase-Nth Highest Salary

时间:2016-03-21 19:58:44      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/lianliang/p/5303069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!