Oracle复杂查询

时间:2024-04-19 17:38:14

1:列出所有员工的姓名,部门名称,和工资

select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno = a2.deptno;

2:列出所有部门的详细信息和部门人数

select a2.deptno,a2.dname,a2.loc,count(a1.empno) from emp a1,dept a2 where a1.deptno(+) = a2.deptno group by a2.deptno,a2.dname,a2.loc;

3:列出所有员工的年工资,所在部门名称,按年薪升序排列

select a1.sal*12 ,a2.dname from emp a1,dept a2 where a1.deptno = a2.deptno order by a1.sal*12;

4:查出每个员工的上级主管及所在部门名称,并要求这些主管的薪水超过3000

select employee.ename,boss.ename ,a1.dname from emp employee,emp boss,dept a1 where employee.mgr = boss.empno and boss.deptno = a1.deptno and boss.sal >3000;

5:求出部门名称中带’S’字符的部门员工的工资合计,部门人数

SELECT d.deptno,NVL(SUM(sal),0),COUNT(empno)FROM emp e,dept d WHERE e.deptno(+)=d.deptno AND d.dname LIKE '%S%' GROUP BY d.deptno ;

6:列出部门名称和这些部门的员工信息(数量,平均工资),同时列出那些没有员工的部门

select d.dname,avg(e.sal),count(e.empno) from emp e,dept d where e.deptno(+)=d.deptno group by d.dname;

7:列出在部门”SALES”工作的员工姓名,基本工资,雇用日期,部门名称,假定不知道销售部的部门编号

select a1.ename,a1.sal,a1.hiredate,a2.dname from emp a1,dept a2 where a1.deptno = a2.deptno and a2.dname = 'SALES';

8:列出公司各个工资等级雇员的数量,平均工资

select grade,count(*),avg(sal) from emp, salgrade where sal between losal and hisal group by grade;

9:列出薪水高于在部门30工作的所有员工的薪金的员工姓名和薪金,部门名称

select a1.ename,a1.sal,a2.dname from emp a1, dept a2 where a1.deptno = a2.deptno and sal > all(select sal from emp where deptno = 30);

10:列出受雇日期早于直接上级的所有员工的编号,姓名,部门名称,部门位置,部门人数

SELECT e.empno,e.ename,d.dname,d.loc,temp.count

FROM emp e,emp m,dept d,(

SELECT deptno dno, COUNT(empno) count

FROM emp

GROUP BY deptno) temp

WHERE e.mgr = m.empno(+) AND e.hiredate < m.hiredate

AND e.deptno = d.deptno

AND e.deptno = temp.dno;

11:列出所有“clerk”的姓名及其部门名称,部门人数,工资等级

SELECT e.ename , d.dname ,temp.count,s.grade

FROM emp e, dept d,(

SELECT deptno dno,COUNT(empno) count

FROM emp

GROUP BY deptno) temp,salgrade s

WHERE job='CLERK'

AND e.deptno = d.deptno

AND d.deptno = temp.dno

AND e.sal BETWEEN s.losal AND s.hisal;

12:列出最低薪金大于1500的各种工作及从事此工作的全部雇员人数及所在部门名称,位置,平均工资

SELECT t.job,t.count,d.dname,e.ename,reg.avg

FROM dept d,(

SELECT e.job,COUNT(e.empno) count

FROM emp e

GROUP BY e.job

HAVING MIN(e.sal)>1500

)t,emp e,(

SELECT deptno dno,AVG(sal) avg

FROM emp

GROUP BY deptno

)reg

WHERE e.deptno = d.deptno AND e.job = t.job

AND e.deptno = reg.dno;

13:列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,公司的工资等级

select a1.ename,a2.dname,a1.mgr,a3.grade from emp a1,dept a2,salgrade a3,(select avg(sal) mysal from emp) a4 where a1.deptno = a2.deptno and sal between a3.losal and a3.hisal and a1.sal > a4.mysal;

14:列出与SCOTT从事相同工作的所有员工及部门名称,部门人数

SELECT e.empno,e.ename,e.job,d.dname,temp.count FROM emp e,dept d,(SELECT deptno dno,COUNT(empno) count FROM emp GROUP BY deptno) temp

WHERE e.job=(SELECT job FROM emp WHERE ename='SCOTT') AND e.ename<>'SCOTT' AND e.deptno=d.deptno AND temp.dno=e.deptno;

15:列出在每个部门工作的员工数量,平均工资,和平均服务年限

SELECT d.dname,count(e.empno),avg(e.sal),round(avg(sysdate-e.hiredate)/365) from emp e,dept d where e.deptno(+)=d.deptno GROUP BY d.dname;

16:列出各种工作的最低工资及此雇员姓名

select a1.ename,a1.job,a1.sal from emp a1,(select job,min(sal) min_sal from emp group by job) a2 where a1.job=a2.job and a1.sal=a2.min_sal;

17:列出各个部门的MANAGER的最低薪金,姓名,部门名称,部门人数

select e.sal,e.ename,d.dname, count from(select job,min(sal) sal,ename,deptno from emp where job='MANAGER'GROUP BY job,ename,deptno) e,

(select d.deptno,d.dname,count(e.empno) count fromemp e,dept d wheree.deptno=d.deptno GROUP BY d.deptno,d.dname) d

where e.deptno=d.deptno;

Oracle分页(根据ROWNUM分页)

select * from (select a1.*,rownum rn from (select ename,sal from emp order by sal) a1 where rownum<=10) where rn >=6;