取得比普通员工(员工代码没有在mgr字段上出现的)的最高薪水还要高的领导人姓名
作者:互联网
首先获得所有领导的名单
select e.empno,e.ename,e.sal from emp e
join
(select DISTINCT e.mgr as mgr from emp e where e.mgr is not null) e1
on e.empno = e1.mgr
order by e.sal;
然后获得所有普通员工(非领导职务)的名单
select
b.empno,b.ename,b.sal
from
emp a
right join
emp b
on
a.mgr = b.empno
where a.ename is null;
第三步,找出普通员工的最高薪水
select max(sal) as maxsal from (select
b.empno,b.ename,b.sal
from
emp a
right join
emp b
on
a.mgr = b.empno
where a.ename is null) c;
第四步,找出领导中薪水比普通员工最高薪水高的员工
select e.empno,e.ename,e.sal from emp e
join
(select DISTINCT e.mgr as mgr from emp e where e.mgr is not null) e1
on e.empno = e1.mgr
join
(select max(sal) as maxsal from (select
b.empno,b.ename,b.sal
from
emp a
right join
emp b
on
a.mgr = b.empno
where a.ename is null) c) d
on e.sal>d.maxsal;
结果是所有领导工资都比普通员工高。
感觉代码好像太繁琐了,希望以后通过学习能简化这个查询语句。
标签:ename,sal,empno,mgr,字段,emp,普通员工,select 来源: https://blog.csdn.net/Jolence1/article/details/116030651