数据库SQL实践18:获取当前薪水第二多的员工的emp_no以及其对应的薪水salary,不准使用order by

思想:

题目要求查找当前薪水(to_date='9999-01-01')排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by。首先通过条件e.emp_no=s.emp_no将两表内连接相连,然后通过条件to_date='9999-01-01'取出员工当前薪水,再者通过子查询找出当前最高薪水select max (salary) from salaries where to_date='9999-01-01',最后通过条件salary not in
(select max (salary) from salaries where to_date='9999-01-01')且max(salary)找出第二高薪水。

select e.emp_no, max(s.salary) as salary, e.last_name, e.first_name
from employees e inner join salaries s on e.emp_no=s.emp_no
where to_date='9999-01-01'and salary not in
(select max (salary) from salaries where to_date='9999-01-01');

 

你可能感兴趣的:(牛客网数据库SQL实践)