数据库查询-编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary)
程序员文章站
2024-03-08 15:59:22
...
解题思路:
找出第二高的薪水,先找出表里的最大值e1.Salary,再找出表里的最大值e2.Salary,两者进行比较,e1.Salary<e2.Salary,找出的e1.Salary即是第二高的薪水
select max(Salary) as SecondHighestSalary from Employee e1 where
(select max(Salary) from Employee e2 where e1.Salary<e2.Salary)
另外一种简洁的方法
select max(Salary) as SecondHighestSalary from Employee
where Salary<(select max(Salary) from Employee)
第三种方法
select IFNULL((select Distinct Salary from Employee order by Salary DESC limit 1,1),null)
as SecondHighestSalary
上一篇: sqlserver查看库中表的相关信息(表大小、行数、空间等)
下一篇: mongodb的入门学习