SELECT e.empno,e.ename,e.jobFROM emp eWHERE e.job=( SELECT job FROM emp WHERE ename='SCOTT') AND e.ename<>'SCOTT';
**第三步:**要查询出该支援对应的部门名称;
SELECT e.empno,e.ename,e.job,d.dnameFROM emp e,dept dWHERE e.job=( SELECT job FROM emp WHERE ename='SCOTT') AND e.ename<>'SCOTT' AND e.deptno=d.deptno;
SELECT e.empno,e.ename,e.job,d.dname,temp.count,temp.avgFROM emp e,dept d,( SELECT deptno,COUNT(empno) count,AVG(sal) avg FROM emp GROUP BY deptno) tempWHERE e.job=( SELECT job FROM emp WHERE ename='SCOTT') AND e.ename<>'SCOTT' AND e.deptno=d.deptno AND e.deptno=temp.deptno;
雇员与工资等级:emp.sal BETWEEN salgrade.losal AND salgrade.hisal;
**第一步:**查询所有在30部门工作的员工工资
SELECT sal FROM emp WHERE deptno=30;
**第二步:**查询出所有大于30部门员工工资的雇员信息,一定使用>ALL;
SELECT e.ename,e.salFROM emp eWHERE e.sal>ALL( SELECT sal FROM emp WHERE deptno=30);
**第三步:**高于公司平均工资
SELECT e.ename,e.salFROM emp eWHERE e.sal>ALL( SELECT sal FROM emp WHERE deptno=30) AND e.sal>(SELECT AVG(sal) FROM emp);
**第四步:**引入dept表查询出部门名称
SELECT e.ename,e.sal,d.dnameFROM emp e,dept dWHERE e.sal>ALL( SELECT sal FROM emp WHERE deptno=30) AND e.sal>(SELECT AVG(sal) FROM emp) AND e.deptno=d.deptno;
**第五步:**获取部门平均工资
SELECT e.ename,e.sal,d.dname,temp.avgFROM emp e,dept d,( SELECT deptno,AVG(sal) avg FROM emp GROUP BY deptno) tempWHERE e.sal>ALL( SELECT sal FROM emp WHERE deptno=30) AND e.sal>(SELECT AVG(sal) FROM emp) AND e.deptno=d.deptno AND e.deptno=temp.deptno;
**第六步:**工资等级直接引入salgrade表
SELECT e.ename,e.sal,d.dname,temp.avg,s.gradeFROM emp e,dept d,( SELECT deptno,AVG(sal) avg FROM emp GROUP BY deptno) temp,salgrade sWHERE e.sal>ALL( SELECT sal FROM emp WHERE deptno=30) AND e.sal>(SELECT AVG(sal) FROM emp) AND e.deptno=d.deptno AND e.deptno=temp.deptno AND e.sal BETWEEN s.losal AND s.hisal;
雇员与工作:emp.sal BETWEEN salgrade.losal AND salgrade.hisal;
**第一步:**查询出所有雇员编号、姓名、职位、薪金
SELECT e.empno,e.ename,e.job,e.salFROM emp e;
**第二步:**查询出员工所对应职位的人数
SELECT e.empno,e.ename,e.job,e.sal,empjobtemp.countFROM emp e,( SELECT job,COUNT(*) count FROM emp GROUP BY job) empjobtempWHERE e.job=empjobtemp.job;
**第三步:**领导姓名、领导职位
SELECT e.empno,e.ename,e.job,e.sal,empjobtemp.count,m.ename,m.jobFROM emp e,( SELECT job,COUNT(*) count FROM emp GROUP BY job) empjobtemp,emp mWHERE e.job=empjobtemp.job AND e.mgr=m.empno(+);
**第四步:**领导所在职位人数,应该按照领导的职位分组:
首先要求筛选出所有的领导:
SELECT DISTINCT mgr FROM emp;
领导的职位要根据以上的返回结果根据job分组:
SELECT jobFROM empWHERE empno IN ( SELECT DISTINCT mgr FROM emp);
随后以上面的职位信息进行分组的数据的筛选:
SELECT job,COUNT(*)FROM empWHERE job IN ( SELECT job FROM emp WHERE empno IN ( SELECT DISTINCT mgr FROM emp))GROUP BY job;
需要将以上的查询安放到之前的查询之中:
SELECT e.empno,e.ename,e.job,e.sal,empjobtemp.count,m.ename,m.job,mgrjobtemp.countFROM emp e,( SELECT job,COUNT(*) count FROM emp GROUP BY job) empjobtemp,emp m,( SELECT job,COUNT(*) count FROM emp WHERE job IN ( SELECT job FROM emp WHERE empno IN ( SELECT DISTINCT mgr FROM emp)) GROUP BY job) mgrjobtempWHERE e.job=empjobtemp.job AND e.mgr=m.empno(+) AND m.job=mgrjobtemp.job;
**第五步:**要获取雇员的部门信息则引入部门表
SELECT e.empno,e.ename,e.job,e.sal,empjobtemp.count,m.ename,m.job,mgrjobtemp.count,d.dnameFROM emp e,( SELECT job,COUNT(*) count FROM emp GROUP BY job) empjobtemp,emp m,( SELECT job,COUNT(*) count FROM emp WHERE job IN ( SELECT job FROM emp WHERE empno IN ( SELECT DISTINCT mgr FROM emp)) GROUP BY job) mgrjobtemp,dept dWHERE e.job=empjobtemp.job AND e.mgr=m.empno(+) AND m.job=mgrjobtemp.job AND e.deptno=d.deptno;
**第六步:**雇员部门人数
SELECT e.empno,e.ename,e.job,e.sal,empjobtemp.count,m.ename,m.job,mgrjobtemp.count,d.dname,empdepttemp.countFROM emp e,( SELECT job,COUNT(*) count FROM emp GROUP BY job) empjobtemp,emp m,( SELECT job,COUNT(*) count FROM emp WHERE job IN ( SELECT job FROM emp WHERE empno IN ( SELECT DISTINCT mgr FROM emp)) GROUP BY job) mgrjobtemp,dept d,( SELECT deptno dno,COUNT(*) count FROM emp GROUP BY deptno) empdepttempWHERE e.job=empjobtemp.job AND e.mgr=m.empno(+) AND m.job=mgrjobtemp.job AND e.deptno=d.deptno AND e.deptno=empdepttemp.dno;
**第七步:**要求查询出领导对应的部门信息
SELECT e.empno,e.ename,e.job,e.sal,empjobtemp.count,m.ename,m.job,mgrjobtemp.count,d.dname,empdepttemp.count,d2.dnameFROM emp e,( SELECT job,COUNT(*) count FROM emp GROUP BY job) empjobtemp,emp m,( SELECT job,COUNT(*) count FROM emp WHERE job IN ( SELECT job FROM emp WHERE empno IN ( SELECT DISTINCT mgr FROM emp)) GROUP BY job) mgrjobtemp,dept d,( SELECT deptno dno,COUNT(*) count FROM emp GROUP BY deptno) empdepttemp,dept d2WHERE e.job=empjobtemp.job AND e.mgr=m.empno(+) AND m.job=mgrjobtemp.job AND e.deptno=d.deptno AND e.deptno=empdepttemp.dno AND m.deptno=d2.deptno;
**第八步:**查询出领导所在的部门人数
要查询出领导所在的部门编号:
SELECT deptno dno,COUNT(*) countFROM empWHERE deptno IN ( SELECT deptno FROM emp WHERE mgr IN ( SELECT mgr FROM emp))GROUP BY deptno;
将两个查询整合
SELECT e.empno,e.ename,e.job,e.sal,empjobtemp.count, m.ename,m.job,mgrjobtemp.count,d.dname,empdepttemp.count, d2.dname,mgrdepttemp.countFROM emp e,( SELECT job,COUNT(*) count FROM emp GROUP BY job) empjobtemp,emp m,( SELECT job,COUNT(*) count FROM emp WHERE job IN ( SELECT job FROM emp WHERE empno IN ( SELECT DISTINCT mgr FROM emp)) GROUP BY job) mgrjobtemp,dept d,( SELECT deptno dno,COUNT(*) count FROM emp GROUP BY deptno) empdepttemp,dept d2,( SELECT deptno dno,COUNT(*) count FROM emp WHERE deptno IN ( SELECT deptno FROM emp WHERE mgr IN ( SELECT mgr FROM emp)) GROUP BY deptno) mgrdepttempWHERE e.job=empjobtemp.job AND e.mgr=m.empno(+) AND m.job=mgrjobtemp.job AND e.deptno=d.deptno AND e.deptno=empdepttemp.dno AND m.deptno=d2.deptno AND m.deptno=mgrdepttemp.dno;
**第九步:**雇员工资等级,直接引入salgrade表
SELECT e.empno,e.ename,e.job,e.sal,empjobtemp.count, m.ename,m.job,mgrjobtemp.count,d.dname,empdepttemp.count, d2.dname,mgrdepttemp.count,s.gradeFROM emp e,( SELECT job,COUNT(*) count FROM emp GROUP BY job) empjobtemp,emp m,( SELECT job,COUNT(*) count FROM emp WHERE job IN ( SELECT job FROM emp WHERE empno IN ( SELECT DISTINCT mgr FROM emp)) GROUP BY job) mgrjobtemp,dept d,( SELECT deptno dno,COUNT(*) count FROM emp GROUP BY deptno) empdepttemp,dept d2,( SELECT deptno dno,COUNT(*) count FROM emp WHERE deptno IN ( SELECT deptno FROM emp WHERE mgr IN ( SELECT mgr FROM emp)) GROUP BY deptno) mgrdepttemp,salgrade sWHERE e.job=empjobtemp.job AND e.mgr=m.empno(+) AND m.job=mgrjobtemp.job AND e.deptno=d.deptno AND e.deptno=empdepttemp.dno AND m.deptno=d2.deptno AND m.deptno=mgrdepttemp.dno AND e.sal BETWEEN s.losal AND s.hisal;
**第十步:**找到对应等级的人数,直接追加一个子查询即可
SELECT e.empno,e.ename,e.job,e.sal,empjobtemp.count, m.ename,m.job,mgrjobtemp.count,d.dname,empdepttemp.count, d2.dname,mgrdepttemp.count,s.grade,empsaltamp.countFROM emp e,( SELECT job,COUNT(*) count FROM emp GROUP BY job) empjobtemp,emp m,( SELECT job,COUNT(*) count FROM emp WHERE job IN ( SELECT job FROM emp WHERE empno IN ( SELECT DISTINCT mgr FROM emp)) GROUP BY job) mgrjobtemp,dept d,( SELECT deptno dno,COUNT(*) count FROM emp GROUP BY deptno) empdepttemp,dept d2,( SELECT deptno dno,COUNT(*) count FROM emp WHERE deptno IN ( SELECT deptno FROM emp WHERE mgr IN ( SELECT mgr FROM emp)) GROUP BY deptno) mgrdepttemp,salgrade s,( SELECT s1.grade grd,COUNT(e1.empno) count FROM emp e1,salgrade s1 WHERE e1.sal BETWEEN s1.losal AND s1.hisal GROUP BY s1.grade) empsaltampWHERE e.job=empjobtemp.job AND e.mgr=m.empno(+) AND m.job=mgrjobtemp.job AND e.deptno=d.deptno AND e.deptno=empdepttemp.dno AND m.deptno=d2.deptno AND m.deptno=mgrdepttemp.dno AND e.sal BETWEEN s.losal AND s.hisal AND s.grade=empsaltamp.grd;