Oracle练习

1.在scott表中,层次查询老板下面的所有员工

select level,empno,ename,mgr from emp

connect by prior empno=mgr

start with mgr is null

order by 1;

执行结果:

层次查询是单表查询,但是没有办法在这里显示出老板姓名

2.在scott表中,查询各个老板手下员工的最低工资,最低工资不能低于1000,没有管理者的不包含在内

   显示列名为老板工号,老板姓名,员工最低工资

--1.显示老板工号和员工最低工资
select a.mgr "老板工号",min(a.sal) from emp a where a.mgr is not null group by a.mgr having min(a.sal) >=1000;
--2.根据老板工号查询老板姓名
select a.empno "员工工号",a.ename "员工姓名",b.empno "老板工号",b.ename "老板姓名" from emp a,emp b where a.mgr=b.empno;
--3.合在一起
select a.mgr "老板工号",b.ename "老板姓名",min(a.sal)
from emp a,emp b
where a.mgr is not null
and a.mgr=b.empno
group by a.mgr,b.ename
having min(a.sal) >=1000;

也可采用外连接

--左连接
select a.mgr,b.ename,min(a.sal) from emp a
right join emp b
on a.mgr=b.empno
where a.mgr is not null group by a.mgr,b.ename having min(a.sal) >=1000;

执行结果:

原文地址:https://www.cnblogs.com/shizunatsu/p/11161626.html