[LeetCode][SQL]Second Highest Salary

https://leetcode.com/problems/second-highest-salary/

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.



首先MySQL不支持top语法,要用limit代替。

select Salary from Employee order by Salary desc limit 1,1

这样写的话如果不存在,select出来是空,题目需要是null(掀桌。

Input:    {"headers": {"Employee": ["Id", "Salary"]}, "rows": {"Employee": [[1, 100]]}}
Output:    {"headers": ["Salary"], "values": []}
Expected:    {"headers": ["SecondHighestSalary"], "values": [[null]]}

于是就需要在外面再包一层。为什么要加distinct呢,是因为不加过不了这个case:

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 100    |
+----+--------+

最终解法:
select (select distinct Salary from Employee order by Salary desc limit 1,1) as Salary;
时间: 2024-10-19 19:52:38

[LeetCode][SQL]Second Highest Salary的相关文章

[LeetCode][SQL]Nth Highest Salary

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 highe

LeetCode:Department Highest Salary - 部门内最高工资

1.题目名称 Department Highest Salary(部门内最高工资) 2.题目地址 https://leetcode.com/problems/rising-temperature 3.题目内容 表Employee包括四列:Id.Name.Salary.DepartmentId +----+-------+--------+--------------+ | Id | Name  | Salary | DepartmentId | +----+-------+--------+--

LeetCode 176 Second Highest Salary mysql,select 嵌套 难度:1

https://leetcode.com/problems/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 th

LeetCode:Second Highest Salary - 第二高的工资

1.题目名称 Second Highest Salary(第二高的工资) 2.题目地址 https://leetcode.com/problems/second-highest-salary/ 3.题目内容 现在有一张记录了Id(主键)和Salary(工资)的表,求出其中第二高的工资.如果不存在第二高的工资,返回null. +----+--------+ | Id | Salary | +----+--------+ | 1  | 100    | | 2  | 200    | | 3  | 

【Leetcode】 Second Highest Salary

题目链接:https://leetcode.com/problems/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 Empl

LeetCode:Nth Highest Salary - 第N高的工资

1.题目名称 Nth Highest Salary(第N高的工资) 2.题目地址 https://leetcode.com/problems/nth-highest-salary/ 3.题目内容 与这道题目相比,上一道题目"Second Highest Salary"是本题在N=2时的特例.两道题目的表结构完全一样,Employee表的结构如下: +----+--------+ | Id | Salary | +----+--------+ | 1  | 100    | | 2  |

【leetcode】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 sala

Leetcode 176 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 

[LeetCode] Department Highest Salary 系里最高薪水

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id. +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Jo