当前位置: 代码迷 >> MySQL >> 标题2:MySQL-Second Highest Salary
  详细解决方案

标题2:MySQL-Second Highest Salary

热度:195   发布时间:2016-05-05 17:05:49.0
题目2:MySQL----------Second Highest Salary

Write a SQL query to get the second highest salary from the Employee table.

+----+--------+| Id | Salary |+----+--------+| 1  | 100    || 2  | 200    || 3  | 300    |+----+--------+

For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should return null.

题目答案:
# Write your MySQL query statement belowSELECT max(Salary) FROM Employee WHERE Salary < (SELECT max(Salary) FROM Employee)

题目解析:

SELECT max(Salary) FROM Employee WHERE Salary < (SELECT max(Salary) FROM Employee)

Using max() will return a NULL if the value doesn't exist. So there is no need to UNION a NULL. Of course, if the second highest value is guaranteed to exist, using LIMIT 1,1 will be the best answer.







  相关解决方案