Oracle学习之集合运算
时间:2021-07-01 10:21:17
帮助过:3人阅读
2 where deptno=10
3 union
4 select * from emp
5 where deptno=20;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-12月-80 880 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已选择8行。
使用UNION操作符统计scott用户下emp表中得各个部门的总工资,
和各部门各职位的工资
Sql代码
- SQL> break on deptno skip 2;
- SQL> select deptno,job,sum(sal) from emp group by deptno,job
- 2 union
- 3 select deptno,to_char(null),sum(sal) from emp group by deptno
- 4 union
- 5 select to_number(null),to_char(null),sum(sal) from emp;
-
- DEPTNO JOB SUM(SAL)
- 10 CLERK 1300
- MANAGER 2450
- PRESIDENT 5000
- 8750
-
-
- 20 ANALYST 6000
- CLERK 1980
- MANAGER 2975
- 10955
-
-
- 30 CLERK 950
- MANAGER 2850
- SALESMAN 5600
- 9400
-
-
- 29105
-
-
-
- 已选择13行。
使用UNION ALL操作符查询scott用户下dept表中的信息
Sql代码
- SQL> select * from dept
- 2 union all
- 3 select * from dept;
-
- DEPTNO DNAME LOC
- 10 ACCOUNTING NEW YORK
- 20 RESEARCH DALLAS
- 30 SALES CHICAGO
- 40 OPERATIONS BOSTON
- 10 ACCOUNTING NEW YORK
- 20 RESEARCH DALLAS
- 30 SALES CHICAGO
- 40 OPERATIONS BOSTON
-
- 已选择8行。
使用INTERSECT操作符查询scott用户下dept表和emp表中都存在的
部门号
Sql代码
- SQL> select deptno from emp
- 2 intersect
- 3 select deptno from dept;
-
- DEPTNO
- 10
- 20
- 30
使用MINUS操作符查询scott用户下dept表中存在而emp表中
不存在的部门号
Sql代码
- SQL> select deptno from dept
- 2 minus
- 3 select deptno from emp;
-
- DEPTNO
- 40
使用MINUS操作符查询scott用户下emp表中的员工工资属于
(1200-2000)这个档,而不属于(1400-3000)这个档的员工
Sql代码
- SQL> select empno,ename,job,sal
- 2 from emp
- 3 where sal between 1200 and 2000
- 4 minus
- 5 select empno,ename,job,sal
- 6 from emp
- 7 where sal between 1400 and 3000;
-
- EMPNO ENAME JOB SAL
- 7521 WARD SALESMAN 1250
- 7654 MARTIN SALESMAN 1250
- 7934 MILLER CLERK 1300
Oracle学习之集合运算
标签: