一、并集(union union all 两个集合中列的类型|顺序|数量要一致)
1.union(AUB-A∩B 将多个查询的结果组合到一个查询结果之中,并去掉重复值)
--工资大于1500,或者20号部门下的员工
select * from emp where sal > 1500 or deptno=20;
select * from emp where sal > 1500
union
select * from emp where deptno=20; --9条数据,去重
2.union all(AUB 将多个查询结果组合到一个查询结果中,包括重复值 )
--工资大于1500,或者20号部门下的员工
select * from emp where sal > 1500
union all
select * from emp where deptno=20; --12条记录,没有去重
3. 两个集合中列的类型顺序数量要一致,如果数量不足可以用null或者同类型数据补齐
--用null补齐
select ename,sal,deptno from emp where sal > 1500
union
select ename,sal,null from emp where deptno = 20;
--用同类型数据补齐
select ename,sal,deptno from emp where sal > 1500
union
select ename,sal,66 from emp where deptno = 20;
二、交集(INTERSECT A∩B)
--工资大于1500,并且20号部门下的员工
select * from emp where sal > 1500
intersect
select * from emp where deptno=20;
三、差集(MINUS A-B)
--1981年入职员工(不包括总裁和经理)
select * from emp where to_char(hiredate,'yyyy')='1981'
minus
select * from emp where job in('MANAGER','PRESIDENT')