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