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 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|