题解 | #获取当前薪水第二多的员工的emp_no以及其对应的薪水salary#

获取当前薪水第二多的员工的emp_no以及其对应的薪水salary

http://www.nowcoder.com/practice/c1472daba75d4635b7f8540b837cc719

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

-- 窗口函数
select b.emp_no,b.salary,e.last_name,e.first_name from
employees as e inner join 
(selcet emp_no,salary,dense_rank() over (partition by to_date order by salary desc) as rk
from salaries
where to_date = '9999-01-01') as b
on e.emp_no = b.emp_no
where rk = 2;

 -- 自连接和count函数计算排名
select e.emp_no,s.salary,e.last_name,e.first_name
from employees as e inner join salaries as s
    on e.emp_no = s.emp_no
where s.salary = 
    (
        select s1.salary
        from salaries as s1 join salaries as s2

        -- <=代表比某个薪水值小的分到一组
        on s1.salary <= s2.salary
        group by s1.salary
        having count(distinct s2.salary) = 2
    );






全部评论

相关推荐

已老实求offer😫:有点像徐坤(没有冒犯的意思哈)
点赞 评论 收藏
分享
头像
11-21 11:39
四川大学 Java
是红鸢啊:忘了还没结束,还有字节的5k 违约金
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务