【数据库查询】176. Second Highest Salary

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiang_1603/article/details/87987669

176. Second Highest Salary

Easy

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 query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

返回第二大的工资,ORDER BY之后LIMIT 1, 1 但题目还要求如果没有第二大要返回null,官方题解是在SELECT后边用SELECT建一个临时表,查不到相应数据就会返回null

# Write your MySQL query statement below
SELECT
    (SELECT DISTINCT Salary
     FROM Employee
     ORDER BY Salary DESC
     LIMIT 1,1) AS SecondHighestSalary;

猜你喜欢

转载自blog.csdn.net/jiang_1603/article/details/87987669