25.【较难】获取员工其当前的薪水比其manager当前薪水还高的相关信息

获取员工其当前的薪水比其manager当前薪水还高的相关信息

http://www.nowcoder.com/questionTerminal/f858d74a030e48da8e0f69e21be63bef

法一:一表多用

select de.emp_no,
       dm.emp_no as manager_no,
       s1.salary as emp_salary,
       s2.salary as manager_salary
from dept_emp de,dept_manager dm,salaries s1,salaries s2
where de.dept_no=dm.dept_no
and de.emp_no=s1.emp_no
and dm.emp_no=s2.emp_no
and s1.to_date='9999-01-01'
and s2.to_date='9999-01-01'
and s1.salary>s2.salary

13ms 3452KB

法二:分别构建员工工资表和manager工资表,再用部门号联结

员工工资表

SELECT de.dept_no, de.emp_no, s.salary AS emp_salary
FROM dept_emp de, salaries s
WHERE de.emp_no=s.emp_no
AND s.to_date='9999-01-01'

manager工资表

SELECT dm.dept_no, dm.emp_no AS manager_no, s.salary AS manager_salary
FROM dept_manager dm, salaries s
WHERE dm.emp_no=s.emp_no
AND s.to_date='9999-01-01'

最终SQL

SELECT a.emp_no, b.manager_no, a.emp_salary, b.manager_salary
FROM
(SELECT de.dept_no, de.emp_no, s.salary AS emp_salary
FROM dept_emp de, salaries s
WHERE de.emp_no=s.emp_no
AND s.to_date='9999-01-01') a,
(SELECT dm.dept_no, dm.emp_no AS manager_no, s.salary AS manager_salary
FROM dept_manager  dm, salaries s
WHERE dm.emp_no=s.emp_no
AND s.to_date='9999-01-01') b
WHERE a.dept_no=b.dept_no
AND a.emp_salary>b.manager_salary;
数据分析阿宇君的SQL题解 文章被收录于专栏

数据分析的SQL题目

全部评论
忽略了员工信息表包含经理的信息,该题没问题但是若要求员工工资大于等于经理工资就错了,不够严谨
2 回复 分享
发布于 2021-07-20 16:53
为什么没有判断员工和经理的过程啊。。。求教求教
点赞 回复 分享
发布于 2021-09-22 17:31
SELECT a.emp_no ,b.emp_no manager_no ,a.salary emp_salary ,b.salary manager_salary from (#员工薪水表 select de.emp_no ,de.dept_no ,s.salary from dept_emp de join salaries s on de.emp_no=s.emp_no left join dept_manager dm on de.emp_no=dm.emp_no where de.to_date='9999-01-01' and s.to_date='9999-01-01' and dm.emp_no is null )a join (#经理薪水表 select dm.emp_no ,dm.dept_no ,s.salary from dept_manager dm join salaries s on dm.emp_no=s.emp_no where dm.to_date='9999-01-01' and s.to_date='9999-01-01' )b on a.dept_no=b.dept_no and a.salary>b.salary
点赞 回复 分享
发布于 2022-01-18 18:46
1.是因为有最后一行代码Where a.emp_salary>b.manager_salary所以不需要另外判断员工吗? 2.为什么不使用join...on...呢?
点赞 回复 分享
发布于 2022-04-24 11:08

相关推荐

85 3 评论
分享
牛客网
牛客企业服务