oracle 学习整理(四)

内容

1、select 语句的执行顺序
	2、分组和统计
	3、集合运算

1、oracle中select语句执行的优先级

select 列名, 聚合函数统计()	5	
from 表名					1			
where 筛选					2			对原表中拥有的数据进行筛选	
group by 分组				3
having 筛选					4			对分组的计算结果进行筛选
order by 列名 asc|desc;		6

2、分组和统计运算:

2.1 group by 对什么列进行分组,就只能查看这个列的信息,查看其他列的数据就会报错

查看emp表格里面,现在有几个不同的部门。
select deptno from scott.emp group by deptno;
查看emp表格里面,现在有几个不同的岗位。
select job from scott.emp group by job;

查看每个部门中,有几种不同的工作岗位。
select deptno,job from scott.emp group by deptno,job;

2.2 在分组中对分组的结果和数据进行计算:聚合函数

count()	数量的统计
sum()	统计总和
avg()	统计平均
max()	统计最大值
min()	统计最小值

2.3 练习

计算一下,每个部门的人数是几人?
select deptno,count(ename) from scott.emp group by deptno;

统计每个岗位中,工资最高的是多少?
select job,max(sal),min(sal),avg(sal),sum(sal),count(empno) 
from scott.emp group by job;

统计每个部门的工资总和,并且要在最后一行统计整个公司的工资总和。
结果结构
deptno		sum(sal)
10			xxx
20			yyy
30			zzz
     		qqq

select deptno,sum(sal) from scott.emp group by rollup(deptno);

rollup(分组的列名):在group by分组之后,对每个组进行统计,并且对分组的结果再次汇总统计。

练习:查询出10号部门和20号部门,各自的平均工资
select deptno,avg(sal) from scott.emp where deptno in (10,20) group by deptno;

查询MANAGER  CLERK  SALESMAN这三个岗位中,最高工资超过了2000的岗位是哪一个?
select job from scott.emp where job in('MANAGER','CLERK','SALESMAN')
group by job having max(sal)>2000;

2.4 对分组统计的结果再次进行筛选: having

查询每个部门中人数大于5个人的部门编号是多少?
select deptno from scott.emp group by deptno having count(empno)>5;
第二种写法也可以再嵌套一个where
select * from (
select deptno,count(*)n from emp group by deptno) where n>5

2.5 解释部分聚合函数

count():统计在组内有几行数据,只会统计格子不为空的行
count(1)和count(*),是一样的意思,都是对整个表格的列进行统计,只要有一个格子不为空,那么就可以被统计计数
count(列),单独对某一列进行统计

sum():统计某一列加起来的总和
sum(数字) :数字乘以行数的结果,对行数的倍数进行统计

2.6 where和having的区别?

where 是在分组之前进行数据筛选,having是在分组之后进行筛选;
where不能对分组的结果进行统计和计算,having可以对分组结果进行筛选;
先要对数据使用where进行初步的筛选,对筛选结果进行分组统计,然后再使用having对统计结果进行筛选,这样效率更高。

统计10号和20号部门的平均工资。
select deptno,avg(sal) from scott.emp where deptno in (10,20) group by deptno;
select deptno,avg(sal) from scott.emp group by deptno having deptno in(10,20);
第一种写法效率更高。

2.7 数据的排序:order by 列名 asc|desc

升序排序:asc
降序排序:desc
如果自己不去定义排序的方法,默认是升序排序。
查询emp表格,先按照部门进行升序的排序,如果部门相同,在同一个部门里面,按照工资进行降序的排序。
select * from scott.emp order by deptno,sal desc;

查询10号和20号部门的各自的工资总和,按照工资的总和进行升序排序。
select deptno,sum(sal) from scott.emp where deptno in(10,20) group by deptno
order by sum(sal) asc;

查询10号和20号部门最高工资和最低工资之间的差,然后按照差额进行降序排序。
select deptno,max(sal)-min(sal) s from scott.emp where deptno in(10,20)
group by deptno
order by s desc;

3 集合运算:

3.1 union:拼接了两个句子的执行结果,并且对结果进行了去重的处理,并且对去重的数据进行了升序的排序。

查询10号部门和20号部门,一起有哪些不同的工作岗位。
select job from scott.emp where deptno=10
union
select job from scott.emp where deptno=20;

3.2 union all:直接拼接了两个句子的执行结果。(效率高)

select job from scott.emp where deptno=10
union all
select job from scott.emp where deptno=20;

3.3 intersect:查询两个句子共有的数据

查询10号部门和20号部门,都有的工作岗位有哪些?
select job from scott.emp where deptno=10
intersect
select job from scott.emp where deptno=20;

3.4 minus:查询第一个句子有的但是第二个句子没有的结果

查询在10号部门有的,20号部门没有的工作岗位有哪些?
select job from scott.emp where deptno=10
minus
select job from scott.emp where deptno=20;

3.5 union 和 union all的区别是什么?

union会对结果进行去重和排序,union all是显示所有内容的拼接的结果。