oracle子查询

子查询是指嵌入在其它sql语句中的select语句,也叫嵌套查询

单行子查询

单行子查询是指只返回一行数据的子查询语句

?       请思考:如何显示与SMITH同一部门的所有员工?

①先查询出SMITH在那个部门

select deptno from emp where ename='SMITH';

②查询出同一部门的人员

select * from emp where deptno=(select deptno from emp where ename='SMITH');

多行子查询

多行子查询是指返回多行数据的子查询

?       请思考:如何查询和部门10的工作相同的雇员的名字、岗位、工资、部门号

①先查询出10号部门有哪些岗位

select distinct job from emp where deptno=10;

②显示和它的岗位有一个相同的员工

select ename,job,sal,deptno from emp where job in(select distinct job from emp where deptno=10);

 

在多行子查询中使用all操作符

?       请思考:如何显示工资比30号部门的所有员工的工资高的员工的姓名、工资和部门号

select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30);

扩展要求:大家想想还有没有别的查询方法

select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);

 

在多行子查询中使用any操作符

?       请思考:如何显示工资比30号部门的任意一个员工的工资高的员工的姓名、工资和部门号

select ename,sal,deptno from emp where sal>any(select sal from emp where deptno=30);

扩展要求:大家想想还有没有别的查询方法

select ename,sal,deptno from emp where sal>(select min(sal) from emp where deptno=30);

 

多列子查询

单行子查询是指子查询只返回单列、单行数据,多行子查询是指返回单列多行数据,都是针对单列而言的,而多列子查询则是指查询返回多个列数据的子查询语句

?       请思考:如何查询与SMITH的部门和岗位相同的所有雇员

思路:先查询出SMITH所在部门和他的岗位

select job from emp where ename='SMITH');

 

SQL> select * from emp where deptno=(select deptno from emp where ename='SMITH') and job=(select job from emp where ename='SMITH');     //没错,但不好不推存

 

select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH');

 

from字句中使用子查询

?       请思考:如何显示高于自己部门平均工资的员工的信息

这里要用到数据查询的小技巧,把一个子查询当作一个临时表使用。

解法1:

思路:各个部门的平均工资是多少?

select avg(sal),deptno from emp group by deptno;

把上面查询的结果当作一个临时表来对待

select t2.ename,t2.sal,t1.myavg,t2.deptno  from emp t2,(select avg(sal) myavg ,deptno from emp group by deptno) t1 where t2.deptno=t1.deptno and t2.sal >t1.myavg;

解法2:

select e1.* from emp e1 where e1.sal>(select avg(sal) from emp where deptno=e1.deptno);

 

?       请思考:查找每个部门工资最高的人的详细资料

 select * from emp,(select max(sal) maxsal,deptno from emp group by deptno) t2 where emp.sal=t2.maxsal and emp.deptno=t2.deptno;

思路:得到所有的员工,进行筛选,每拿到一个员工,判断该员工的工资是否是他们部门的最高工资。

select * from emp e where sal=(select max(sal) from emp where deptno=e.deptno);

 

?       请思考:显示每个部门的信息(编号,名称和人员数量)

思路:先查出各个部门有多少人

select deptno,count(*) from emp group by deptno;

把上面的结果当作临时表对待

select t1.dname,t2.num from dept t1,(select deptno,count(*) num from emp group by deptno) t2 where t1.deptno=t2.deptno(+);

(+):外连接

 

总结:

这里需要说明的是当在from字句中使用子查询时,该子查询会被当作一个临时表来对待。

当在from子句中使用子查询时,必须给子查询指定别名。

 

分页查询

分页查询是我们学习任何数据库必须掌握的一个要点,

mysql:select * from 表名 where 条件 limit 从第几条取,取几条

sql server:select  top 4  *  from 表名 where id not in(select top 4 id from 表名 where 条件);//排除前4条,再取4条,这个案例实际上是取出第5条至第8条。

oracle:select t2.* from (select t1.*,rownum rn from(select * from emp) t1 where rownum<=6) t2 where rn>=4;

说明:上面的这个sql语句是oracle数据库效率比较好的查询方法,在百万级别都可以及时响应。

oracle使用三层过滤

第一层:select * from emp;  //查询出满足条件的所有记录

第二层:select t1.*,rownum rn from(select * from emp) t1 where rownum<=6;  //删选后面的记录

第三层:select t2.* from (select t1.*,rownum rn from(select * from emp) t1 where rownum<=6) t2 where rn>=4;      //删除掉前面的记录

 

实际上我们可以吧上面的sql当作一个分页模板来使用:

6:表示取到第几条

4:表示从第几条开始取

如果我们需要针对不同的情况分页,请在最内层进行处理,包括多表。

 

?       请思考:请按照入职时间的先后顺序,查询从第7到第10个人是谁?

select t2.* from (select t1.*,rownum rn from(select * from emp order by hiredate) t1 where rownum<=10) t2 where rn>=7;

 

看看分页查询的效率:

模拟10万数据的表

从原有表生成新表:

create table mytest as select empno,ename,sal,comm,deptno from emp;

自我复制:

insert into mytest(empno,ename,sal,comm,deptno) select empno,ename,sal,comm,deptno from mytest;

 

用查询结果创建新表

这个命令是一种快捷的建表方法:

create table mytable(id,name,sal,job,deptno) as select empno,ename,sal,job,deptnofrom emp;

 

自我复制数据(蠕虫复制)

有时,为了对某个sql语句进行效率测试,我们需要海量数据时,可以使用此法为表创建海量数据。

 

insert into mytable(id,name,sal,job,deptno) select empno,ename,sal,job,deptno from emp;

 

合并查询

有时在实际应用中,为了合并多个select语句的结果,可以使用集合操作符union、unionall、intersect、minus

1)union

该操作符用于取得连个结果集的并集。当使用该操作符是,会自动丢失掉结果集的重复行。

select ename,sal,job from emp where sal>2500 union

select ename,sal,job from emp where job='MANAGER';

2)union all

该操作符与union相似,但是它不会取消重复行,而且不会排序。

select ename,sal,job from emp where sal>2500 union all

select ename,sal,job from emp where job='MANAGER';

 

3)intersect

使用该操作符用于取得两个结果集的交集

select ename,sal,job from emp where sal>2500 intersect

select ename,sal,job from emp where job='MANAGER';

4)minus

使用该操作符用于取得两个结果集的差集,它只会显示存在第一个集合中,而不存在第二个集合中的数据。

select ename,sal,job from emp where sal>2500 minus

select ename,sal,job from emp where job='MANAGER';

posted @ 2019-07-01 14:07  樊伟胜  阅读(2810)  评论(0)    收藏  举报