Database Leetcode 176 Second Highest Salary

Database Leetcode 176 Second Highest Salary

目录:

  1. 题目

  2. 解题思路

  3. 他山之石

1. 题目

 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.

2. 解题思路

简单粗暴直接用limit,而我却用的max函数配合子查询

select max(Salary) SecondHighestSalary from Employee where Salary < (select max(Salary) from Employee)

3. 他山之石

简单粗暴的limit

SELECT distinct(Salary) as SecondHighestSalary FROM Employee UNION SELECT NULL ORDER BY SecondHighestSalary DESC LIMIT 1,1;

标签: none

添加新评论