题解 | #通过 with 构建临时表完成查找#

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

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

with
    T as (
        select
            e.emp_no,
            e.first_name,
            e.last_name,
            s.salary
        from
            employees e
            left join salaries s on e.emp_no = s.emp_no
        where
            s.salary != (
                select
                    max(salary)
                from
                    salaries
            )
    )

select
    emp_no,
    salary,
    last_name,
    first_name
from
    T
where
    salary = (select max(salary) from T)

本题由于不能使用 order by 方法,因此无法使用上一题中的各种排序函数求解。 因此,可以考虑通过 max 来查找第二大元素:

  • 可以通过 with...as... 构建一张临时表 T;
  • T 中将 salary 等于最大值的行去除
  • 最后将 salary 等于 T 中当前 salary 的最大值即为所求。
全部评论

相关推荐

牛客40297450...:不是研究生强,是你强
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务