oracle查询实例
--求部门中那些人的薪水最高
select ename ,sal from emp
join (select max(sal) max_sal,deptno from emp group bydeptno) t
on (emp.sal = t.max_sal and emp.deptno = t.deptno)
--求部门平均薪水的等级
select deptno, avg_sal, grade from
(select deptno, avg(sal) avg_sal from emp group by deptno) t
join salgrade s on (t.avg_sal between s.losal and s.hisal)
--求部门平均的薪水等级
select deptno, avg(grade) from
(select ename,deptno,grade from emp join salgrade s on (emp.sal between s.losal and s.hisal)) t
group by deptno
--雇员中有哪些人是经理人
select ename from emp where empno in (select mgr from emp)
--不准用组函数,求薪水的最高值
使用自连接找右边表中比左边表高的薪水,则最高的薪水的就是没找出的那个
select distinct sal from emp
where sal not in
(select distinct e1.sal from emp e1 join emp e2 on (e1.sal < e2.sal))
--求平均薪水最高的部门编号
select deptno, avg_sal from
(select deptno,avg(sal) avg_sal from emp group by deptno)
where avg_sal =
(select max(avg_sal) from
(select deptno,avg(sal) avg_sal from emp group by deptno)
)
--求平均薪水最高的部门的部门名称
select dname from dept where deptno =
(
select deptno from
(select deptno,avg(sal) avg_sal from emp group by deptno)
where avg_sal =
(select max(avg_sal) from
(select deptno,avg(sal) avg_sal from emp group by deptno)
)
)