集合运算:
并集: union (只取一遍)/union all (交集取两遍)
交集: intersect
差集: minus (A-B)
1、查询10和20号部门的员工
方法一、select * from emp where deptno=10 or deptno=20;
方法二、select * from emp where deptno in (10,20);
方法三、集合运算:
select * from emp where deptno=10
union (all)
select * from emp where deptno=20;
2、group by rullup(a,b)语句增强
select deptno,job,sum(sal) from emp group by deptno,job ;
+
select deptno,sum(sal) from emp group by deptno;
+
select sum(sal) from emp;
=select deptno, job,sum(sal) from emp group by rollup(deptno,job);
做集合运算要满足的条件:
1、参与运算的各个集合必须满足列数相同且类型一直
2、采用第一个集合的表头作为最后的表头
3、必须在最后一个集合后使用order by
4、括号
select deptno,job,sum(sal) from emp group by deptno,job
union
select deptno,to_char(null), sum(sal) from emp group by deptno
union
select to_number(null), to_char(null),sum(sal) from emp;
/*计算sql运行的时间*/set timing on
/*关闭sql运行的时间*/set timing off
/*打开(关闭)当前时间*/set time on(off)
sql原则:尽量不要使用集合运算,执行时间长