[LeetCode-DATABASE] 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.

题意:查询第二高的工资,没有则返回NULL。

思路1:直接排序去重,这里空要返回NULL,所以用IFNULL()函数。

SQL:

select distinct IFNULL((select distinct Salary from Employee order by Salary desc limit 1,1),null) from Employee;

了解到用嵌套的SELECT可以取代IFNULL函数:

select (select distinct Salary from Employee order by Salary desc limit 1,1);

思路2:找到最大的。再在剩下的中找最大的。因为聚集函数可以直接返回NULL

SQL:

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

时间: 2024-11-17 21:37:42

[LeetCode-DATABASE] Second Highest Salary的相关文章

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: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: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][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][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 | +----+--------+

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

题目链接: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】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