oracle 表连接与子查询示例

--求部门中哪些人的薪水最高
select ename,sal from emp
join (select max(sal) max_sal,deptno from emp group by deptno) t
on (emp.sal = t.max_sal and emp.deptno = t.deptno);
--根据部门求出平均薪水的等级
select avg_sal,deptno,grade from (select avg(sal) avg_sal , deptno from emp group by deptno) t
join salgrade s
on(t.avg_sal between s.losal and s.hisal);
--根据部门求出所在部门薪水的平均等级
select t.deptno,avg(t.grade) from
(select ename,deptno,grade from emp join salgrade s on(emp.sal between s.losal and s.hisal)) t

group by t.deptno;


--左连接
select * from emp e left join dept d on (e.deptno = d.deptno);
--右链接
select * from emp e right join dept d on (e.deptno = d.deptno);
--全连接
select * from emp e full join dept d on (e.deptno = d.deptno);

你可能感兴趣的:(oracle 表连接与子查询示例)