编写子查询的步骤: I.确定数据表 II.确定已知的关联字段 III.按条件逐步编写sql语句; |
--列出薪资高于在部门30工作的所有员工的薪资的员工的姓名和薪资、部门名称,部门人数; I.确定数据表 III. |
2.--列出与"SCOTT"从事相同工作的所有员工的姓名,工资,部门名称,部门人数,领导姓名; I.确定数据表 1.emp 员工姓名,工资, 2.dept 部门名称 3.emp 部门人数 3.emp 领导姓名 II.确定已知的关联字段 1.emp.deptno=dept.deptno 2.emp.empno=e.mgr 1.返回单行单列值,一般用于where或having子句上,但是现在没有统计需要,所有放到where子句中; select job from emp e where e.ename='SCOTT'; 2.找到符号要求的雇员信息 select * from emp where job=(select job from emp e where e.ename='SCOTT') 3.查找部门的部门信息,加入dept表,同时添加消除笛卡儿积的条件 select e.empno,e.ename,e.sal,d.dname from emp e,dept d where job=(select job from emp e where e.ename='SCOTT') and e.deptno=d.deptno 4.部门人数我可以进行单独的统计操作,而后在Form子句之后使用 select temp.deptno,count(*) as counts from emp temp group by temp.deptno select e.empno,e.ename,e.sal,d.dname,t.counts from emp e,dept d,emp m (select temp.deptno,count(*) as counts from emp temp group by temp.deptno) t where job=(select job from emp e where e.ename='SCOTT') and e.deptno=d.deptno and t.deptno=d.deptno 5.查找出雇员信息对应的领导姓名,使用emp表实现自身关联操作 select e.empno,e.ename,e.sal,d.dname,t.counts ,m.ename from emp e,dept d, (select temp.deptno,count(*) as counts from emp temp group by temp.deptno) t,emp m where e.job=(select job from emp e where e.ename='SCOTT') and e.deptno=d.deptno and t.deptno=d.deptno and e.mgr=m.empno 6.消除scott的数据 select e.empno,e.ename,e.sal,d.dname,t.counts ,m.ename from emp e,dept d, (select temp.deptno,count(*) as counts from emp temp group by temp.deptno) t,emp m where e.job=(select job from emp e where e.ename='SCOTT') and e.deptno=d.deptno and t.deptno=d.deptno and e.mgr=m.empno and e.ename<>'SCOTT' |
III.列出薪资比‘SMITH’ 或‘ALLEN’多的所有员工的员工编号,姓名,部门名称,其领导姓名,部门人数,部门平均工资,部门最高和最低工资; 1.确定要使用的数据表 1.emp --员工编号,姓名 2.dept ---部门名称 3.emp temp---领导姓名 4.emp --统计部门数据 2.确定已知的关联字段; 雇员和部门:emp.deptno=dept.deptno; 雇员和领导:emp.empno=temp.mgr 1.查询出smith和allen的薪资 select sal from emp t where t.ename in('SMITH','ALLEN'); 2.以上数据返回多行单列数据,应该在where子句中使用它; select e.empno,e.ename,e.sal from emp e where e.sal>any(select sal from emp t where t.ename in('SMITH','ALLEN')) and e.ename not in('SMITH','ALLEN') 3.查询领导信息 select e.empno,e.ename,e.sal,t.ename from emp e,emp t where e.sal>any(select sal from emp t where t.ename in('SMITH','ALLEN')) and e.ename not in('SMITH','ALLEN') and e.empno=t.mgr(+) 4.统计部门信息 select em.deptno, count(*) counts ,Min(sal) as Minsal ,Max(sal) as Maxsal,avg(sal) as avgsal from emp em group by em.deptno select e.empno,e.ename,e.sal,t.ename,d.dname,temp.counts,temp.minsal,temp.maxsal,temp.avgsal from emp e,emp t,dept d, (select em.deptno, count(*) counts ,Min(sal) as Minsal ,Max(sal) as Maxsal,avg(sal) as avgsal from emp em group by em.deptno )temp where e.sal>any(select sal from emp t where t.ename in('SMITH','ALLEN')) and e.ename not in('SMITH','ALLEN') and e.empno=t.mgr(+) and e.deptno=d.deptno |
IV.查询受雇日期早于其直接上级的所有员工的编号,姓名,部门名称,部门位置,部门人数 1.确定数据表 emp 员工相关信息 dept 部门信息 emp 统计部门人数 emp tmp 领导的雇佣日期作为自身关联使用 2.确定已知的关联字段 员工和部门 emp.deptno=dept.deptno 员工和领导 emp.mgr=tmp.empno 1.实现emp表的自身关联,查询受雇日期早于其直接上级的所有员工的编号,姓名; select e.empno,e.ename from emp e,emp tmp where e.mgr=tmp.empno(+) and e.hiredate<tmp.hiredate 2.找到部门信息 select e.empno,e.ename,d.dname,d.loc from emp e,emp tmp,dept d where e.mgr=tmp.empno(+) and e.hiredate<tmp.hiredate and e.deptno=d.deptno 3.找到部门人数 select e.empno,e.ename,d.dname,d.loc,temp.dcount from emp e,emp tmp,dept d, (select deptno,count(*) dcount from emp group by deptno) temp where e.mgr=tmp.empno(+) and e.hiredate<tmp.hiredate and e.deptno=d.deptno |
V.列出所有‘CLERK’(办事员)的姓名及部门名称,部门人数,工资等级 1.确定数据表 emp 查询姓名 dept 部门信息 emp 统计部门信息 salgrade表工资等级 2.确定关联字段 员工和部门 emp.deptno=dept.deptno 雇员和工资等级表 emp.sal between salrade.losal and salgrade.hisal 1.查询出办事的姓名 select * from emp e where job='CLERK' 2.查询部门信息 select e.ename,d.dname from emp e,dept d where job='CLERK' and e.deptno=d.deptno 3.统计部门人数 select e.ename,d.dname,temp.dcount from emp e,dept d, (select e.deptno,count(*) dcount from emp e group by e.deptno) temp where job='CLERK' and e.deptno=d.deptno and e.deptno=temp.deptno 4.查询工资等级 select e.ename,d.dname,temp.dcount,g.grade from emp e,dept d,salgrade g, (select e.deptno,count(*) dcount from emp e group by e.deptno) temp where job='CLERK' and e.deptno=d.deptno and e.deptno=temp.deptno and e.sal between g.losal and g.hisal |
Oracle子查询2
猜你喜欢
转载自blog.csdn.net/qq_29393273/article/details/86560427
今日推荐
周排行