十一、子查询
1、子查询概述
学习子查询的原因
事例:查询工资比SCOTT高的员工信息
思路:1、scott的工资
select sal from emp where ename='SCOTT';
2、比3000高的员工信息
select * from emp where sal>3000;
子查询方式:
select * from emp where sal> (select sal from emp where ename='SCOTT');

2、子查询的使用
子查询注意的10个问题
(a)子查询语法中的小括号

(b)子查询的书写风格
格式:
select *
from emp
where sal> (select sal
from emp
where ename='SCOTT');
(c)可以使用子查询的位置:where,select,having,from
select事例
单行子查询,只返回一条记录
事例:
select empno,ename,sal,(select job from emp where empno=7830) 第四列 from emp;

多行子查询,返回多条记录

having事例:
10号部门的平均工资:
select deptno,avg(sal) from emp group by deptno having avg(sal)>(select max(sal) from emp where deptno=30);

from事例:
select * from(select empno,ename,sal from emp);

(d)不可以使用子查询的位置:group by
不允许的情况:
select avg(sal) from emp group by (select deptno from emp);
(e)强调:from后面的子查询
(1)查询员工信息:员工号,姓名,月薪
select * from (select empno as 员工号,ename as 姓名,sal as 月薪 from emp);
(2)查询员工信息:员工号,姓名,月薪,年薪
select * from (select empno as 员工号,ename as 姓名,sal as 月薪,sal*12 as 年薪 from emp);

(f)主查询和子查询可以不是同一张表
事例:
查询部门名称是SALES的员工信息
select * from emp where deptno=(select deptno from dept where dname='SALES');

select e.* from emp e,dept d where e.deptno=d.deptno and d.dname='SALES';
(g)一般不在子查询中,使用排序,但在Top-N分析问题中,必须对子查询排序
事例:
找到员工表中工资最高的前三名,如下格式:
ROWNUM EMPNO ENAME SAL
1 7839 KING 5000
2 7788 SCOTT 3000
3 7902 FORD 3000
---------rownum 行号 伪劣
select rownum,empno,ename,sal from emp where rownum<=3 order by sal desc;

行号需要注意的两个问题:
行号永远按照默认的顺序生成
行号只能使用<,<=;不能使用>,>=

(h)一般先执行子查询,再执行主查询,但相关子查询例外
事例:
找到员工表中薪水大于本部门平均薪水的员工
select empno,ename,sal,(select avg(nvl(sal,0)) from emp where deptno=e.deptno) as 平均薪水
from emp e
where sal>(select avg(nvl(sal,0)) from emp where deptno=e.deptno);
3、子查询的类型
(i)单行子查询只能使用单行操作符,多行子查询只能使用多行操作符
单行操作符
操作符 含义
= equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
<> Not equal to

多行操作符
操作符 含义
IN 等于列表中的任何一个
ANY 和子查询返回的任意一个值比较
ALL 和子查询返回的所有值比较

单行子查询:
事例:
查询员工信息,要求:
A、职位与7566员工一样
B、薪水大于7782员工的薪水
select * from emp where job =(select job from emp where empno=7566) and sal >(select sal from emp where empno=7782);
注意:单行子查询只能使用单行操作符
事例:
查询工资最低的员工信息
select * from emp where sal=(select min(sal) from emp);
事例:
查询最低工资大于20号部门最低工资的部门号和部门的最低工资
select deptno,min(sal) from emp group by deptno having min(sal)>(select min(sal) from emp where deptno=20);

多行子查询:
多行操作符:in
事例:
查询部门名称是SALES和ACCOUNTING的员工信息
select * from emp where deptno in (select deptno from dept where dname='SALES' or dname='ACCOUNTING');

select e.* from emp e, dept d where e.deptno=d.deptno and (d.dname='SALES' or dname='ACCOUNTING');

多行操作符:any
事例:
查询工资比30号部门任意一个员工高的员工信息
select * from emp where sal > any(select sal from emp where deptno=30);
select * from emp where sal > (select min(sal) from emp where deptno=30);

多行操作符:all
事例:
查询工资比30号部门所有员工高的员工信息
select * from emp where sal > all(select sal from emp where deptno=30);
select * from emp where sal > (select max(sal) from emp where deptno=30);

(j)注意:子查询中有null问题
单行:
子查询不返回任何行(错误的例子)
select ename,job from emp where job (select job from emp where ename='Tom');
事例:
查询不是老板的员工(错误)
select * from emp where empno not in (select mgr from emp);
正确
select * from emp where empno not in (select mgr from emp where mgr is not null);
4、综合事例
目标:
(1)灵活运用select语句解决实际的问题
(2)通过四个具体的示例,演示如何编写高效的查询语句
示例一:
分页查询显示员工信息:显示员工号,姓名,月薪
要求:(1)每页显示四条记录
(2)显示第二页的员工
(3)按照月薪降序排列
注意:rownum只能使用<,<=,不能使用>,>=
Oracle 通过拼接子查询方式实现分页操作
select r,empno,ename,sal
from (select rownum r,empno,ename,sal
from (select rownum,empno,ename,sal from emp order by sal desc) e1
where rownum<=8) e2
where r>=5;
查看伪列的行号
select rownum,r,empno,ename,sal
from (select rownum r,empno,ename,sal
from (select rownum,empno,ename,sal from emp order by sal desc) e1
where rownum<=8) e2
where r>=5;

示例二:
找到员工表中薪水大于本部门平均薪水的员工.
方法一:
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) as 平均薪水
from emp e
where sal>(select avg(sal) from emp where deptno=e.deptno);
方法二:
select e.empno,e.ename,e.sal, d.avgsal as 平均薪水
from emp e,(select deptno ,avg(sal) avgsal from emp group by deptno) d
where e.deptno=d.deptno and e.sal>d.avgsal;

执行计划设置:
explain plan for
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) as 平均薪水
from emp e
where sal>(select avg(sal) from emp where deptno=e.deptno);
查看执行计划:
select * from table(dbms_xplan.display);

执行计划设置:
explain plan for
select e.empno,e.ename,e.sal, d.avgsal as 平均薪水
from emp e,(select deptno ,avg(sal) avgsal from emp group by deptno) d
where e.deptno=d.deptno and e.sal>d.avgsal;
查看执行计划:
select * from table(dbms_xplan.display);

结论:相关子查询更加的快捷

示例三:
按部门统计员工人数,按照如下格式输出:
Total 1980 1981 1982 1987
======== ======= ========== ======== =========
14 1 10 1 2

注意:员工的入职年份已知

方法一(函数方式):
select count(*) Total,
sum(decode(to_char(hiredate,'YYYY'),'1980',1,0)) "1980",
sum(decode(to_char(hiredate,'YYYY'),'1981',1,0)) "1981",
sum(decode(to_char(hiredate,'YYYY'),'1982',1,0)) "1982",
sum(decode(to_char(hiredate,'YYYY'),'1987',1,0)) "1987"
from emp;

方法二(子查询方式):
select
(select count(*) from emp) Total,
(select count(*) from emp where to_char(hiredate,'yyyy')='1980') "1980",
(select count(*) from emp where to_char(hiredate,'yyyy')='1981') "1981",
(select count(*) from emp where to_char(hiredate,'yyyy')='1982') "1982",
(select count(*) from emp where to_char(hiredate,'yyyy')='1987') "1987"
from dual;

示例四:
============选课表===============
create table pm_ci(
CI_ID varchar2(20) not null,
STU_IDS varchar2(100)
);
insert into pm_ci values('1','1,2,3,4');
insert into pm_ci values('2','1,4');

==========学生表========
create table pm_stu(
STU_ID varchar2(20) not null,
STU_NAME varchar2(20)
);
insert into pm_stu values('1','张三');
insert into pm_stu values('2','李四');
insert into pm_stu values('3','王五');
insert into pm_stu values('4','赵六');

要求查询以下结果:
CI_ID STU_NAME
==================== ===================
1 张三,李四,王五,赵六
2 张三,赵六

提示:
1、需要进行两个表的连接查询,为两个表都取别名
2、使用instr(a,b)函数,该函数的含义为:如果字符串b在字符串a的里面,则返回的是b在a中的位置,即返回值大于0.
3、需要用到分组查询
4、使用wm_concat(cols)函数对学生姓名用逗号进行拼接.

解法:
select ci_id,wm_concat(stu_name) stu_name from (select ci_id,stu_name from pm_ci c,pm_stu s where instr(c.stu_ids,s.stu_id)>0) t group by ci_id;

Oracle学习笔记(八)的更多相关文章

  1. Oracle学习笔记八 表空间

    表空间 表空间是一个或多个数据文件的集合,所有的数据对象都存放在指定的表空间中,但主要存放的是表, 所以称作表空间 .   分区表 当表中的数据量不断增大,查询数据的速度就会变慢,应用程序的性能就会下 ...

  2. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  3. Learning ROS forRobotics Programming Second Edition学习笔记(八)indigo rviz gazebo

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...

  4. oracle学习笔记第一天

    oracle学习笔记第一天 --oracle学习的第一天 --一.几个基础的关键字   1.select select (挑选) 挑选出显示的--列--(可以多列,用“,”隔开,*表示所有列),为一条 ...

  5. python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑

    python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件?当我们点开下载页时, 一般 ...

  6. Go语言学习笔记八: 数组

    Go语言学习笔记八: 数组 数组地球人都知道.所以只说说Go语言的特殊(奇葩)写法. 我一直在想一个人参与了两种语言的设计,但是最后两种语言的语法差异这么大.这是自己否定自己么,为什么不与之前统一一下 ...

  7. Oracle学习笔记——点滴汇总

    Oracle学习笔记——点滴汇总 http://www.botangdb.com/ Oracle GI = Grid Infrastructure = ASM + Cluster

  8. Oracle学习笔记之四sp1,Oracle 11g的常用函数

    从Oracle学习笔记之四,SQL语言入门中摘出来的,独立成一章节 3.1 字符类函数 ASCII(c)和CHR(i)    分别用于返回一个字符的ASCII码和返回给定ASCII值所对应的字符. C ...

  9. Oracle学习笔记之四,SQL语言入门

    1. SQL语言概述 1.1 SQL语言特点 集合性,SQL可以的高层的数据结构上进行工作,工作时不是单条地处理记录,而对数据进行成组的处理. 统一性,操作任务主要包括:查询数据:插入.修改和删除数据 ...

随机推荐

  1. Rails Cookie和session使用

    Rails通过cookies方法来操作cookie.这和session的操作有点相似 class CommentsController < ApplicationController def n ...

  2. Web验证方式(4)--JWT

    OAuth协议中说到的AccessToken可以是以下两种: 1.任意只起到标识作用的字符串:这种情况下Resource Server处理请求时需要去找Authorization Server获取用户 ...

  3. Oracle使用startup与startup force启动的区别

    1. startup 就是正常启动数据库,没什么好说的. 2. startup force 是shutdown abort + startup的组合,即强制关闭数据库+ 正常启动数据库,想快速重启数据 ...

  4. android 点击返回键 以及 加载activity 生命周期 记录。。。,一目了然

    简叙 Activity 生命周期及android 返回按钮捕捉   @Override protected void onPostCreate(Bundle savedInstanceState) { ...

  5. java wab----遇到经常用到集合list/map/

    List与Vector的区别: vector适用:对象数量变化少,简单对象,随机访问元素频繁 list适用:对象数量变化大,对象复杂,插入和删除频] List首先是链表,它的元素不是连续的.vecto ...

  6. zufeoj 1018 阶乘第k位是多少(sprintf)

    题目描述     n的阶乘定义为n!=1*2*3*……*n  如3!=6     n!通常最后会有很多0,如5!=120  最后有一个0,现在统计n!去除末尾的0后,最后k位是多少 输入     第一 ...

  7. ROS注册级别LEVEL0-6,原来使用GRE通道是不要钱滴

    GRE通道是没有个数限制的.如果只做一个分公司的PPTP,L2TP,等等,也是不用钱滴. 跑OSPF就不行了,必须要给钱.

  8. Python普通方法、静态方法、类方法

    开始 # -*-coding:utf-8-*- # 普通方法,类方法,静态方法的区别 __metaclass__ = type class Tst: name = 'tst' data = 'this ...

  9. vs2017运行时修改代码Changes are not allowed while code is running.

    vs2017代码运行时不允许进行更改 工具->选项->调试->常规->启用编辑并继续不选择“启用编辑并继续”,这样就可以在调试时修改cs代码了.

  10. volyaire重振Infiniband

    InfiniBand简 称IB,DoSTOR存储小字典里的解释是,一种新的I/O总线技术,用于取代目前的PCI总线.IB主要应用在企业网络和数据中心,也可以应用在高速线 速路由器.交换机.大型电信设备 ...