First, set operation operator
UNION: (unions) returns two sets all records with a duplicate value removed
UNION All: (set) returns two sets all records with duplicate values removed
INTERSECT: (intersection) returns all records of two sets, repeating only once
minus: (difference set) returns all that belong to the first collection but do not belong to the second collection
Recording
each set in a set operation must have the same number of columns, and the type is consistent, and the node of the set Operation
Fruit will be The header of the first collection is used as the final header, and the order by must be placed in each
After collection
Second, set operation
Use the union operator to query employee information for 10-and 20-door parts of the Scott table
SQL code
- Sql> SELECT * from EMP
- 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 1 July-December-80 880 20
- 7566 JONES MANAGER 7839 February-April-81 2975 20
- 7782 CLARK MANAGER 7839 September-June-81 2450 10
- 7788 SCOTT ANALYST 7566 1 September-April-87 3000 20
- 7839 KING President 1 July-November-81 5000 10
- 7876 ADAMS Clerk 7788 2 March-May-87 1100 20
- 7902 FORD ANALYST 7566 March-December-81 3000 20
- 7934 MILLER Clerk 7782 2 March-January-82 1300 10
- 8 rows have been selected.
Use the union operator to count the total wages of each department in the EMP table under the Scott user,
and various departments of the job bit of wages
SQL code
- Sql> break on deptno Skip 2;
- sql> Select Deptno,job,sum (SAL) from the emp GROUP by deptno,job
- 2 Union
- 3 Select Deptno,to_char (null),sum (SAL) from the emp GROUP by Deptno /c10>
- 4 Union
- 5 Select To_number (null), TO_CHAR (null),sum (sal) from EMP;
- DEPTNO JOB SUM (SAL)
- ---------- --------- ----------
- Ten Clerk 1300
- MANAGER 2450
- President 5000
- 8750
- ANALYST 6000
- Clerk 1980
- MANAGER 2975
- 10955
- 950 Clerk
- MANAGER 2850
- Salesman 5600
- 9400
- 29105
- 13 rows have been selected.
Use the UNION ALL operator to query the information in the Dept table under the Scott user
SQL code
- Sql> SELECT * from dept
- 2 Union All
- 3 SELECT * FROM dept;
- DEPTNO dname LOC
- ---------- -------------- -------------
- Ten ACCOUNTING NEW YORK
- DALLAS
- SALES CHICAGO
- OPERATIONS BOSTON
- Ten ACCOUNTING NEW YORK
- DALLAS
- SALES CHICAGO
- OPERATIONS BOSTON
- 8 rows have been selected.
use the Intersect operator to query the Dept table and the EMP table that are present in the Scott user.
Department number
SQL code
- Sql> Select Deptno from emp
- 2 intersect
- 3 Select Deptno from dept;
- DEPTNO
- ----------
- 10
- 20
- 30
use the minus operator to query the presence of the Scott user under the Dept table and in the EMP table
non-existent department number
SQL code
- Sql> Select Deptno from dept
- 2 minus
- 3 Select Deptno from EMP;
- DEPTNO
- ----------
- 40
use the minus operator to query the employee wages in the EMP table under the Scott user
(1200-2000) This file, not the employee of (1400-3000) this file
SQL code
- sql> Select Empno,ename,job,sal
- 2 from EMP
- 3 where Sal between
- 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 Learning's set operation