176. Second Highest Salary

前端之家收集整理的这篇文章主要介绍了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 query should return 200 as the second highest salary. If there is no second highest salary,then the query should return null.

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

从表里面查询出第二大数值

首先要用distinct去重,因为第二大的数值可能是有重复的,然后从高到低排序,取第二个数据就是。但是要注意的是可能表里面只有一条数据,如果想要返回NULL的话,可以在最外层再加一个select查询

select 
 (
    select distinct salary from Employee order by salary desc limit 1,1
 ) as SecondHighestSalary
原文链接:https://www.f2er.com/note/411432.html

猜你在找的程序笔记相关文章