Oracle Learning's set operation

Source: Internet
Author: User

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
  1. Sql> SELECT * from EMP
  2. 2 where deptno=10
  3. 3 Union
  4. 4 SELECT * FROM EMP
  5. 5 where deptno=20;
  6. EMPNO ename JOB MGR hiredate SAL COMM DEPTNO
  7. ---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
  8. 7369 SMITH Clerk 7902 1 July-December-80 880 20
  9. 7566 JONES MANAGER 7839 February-April-81 2975 20
  10. 7782 CLARK MANAGER 7839 September-June-81 2450 10
  11. 7788 SCOTT ANALYST 7566 1 September-April-87 3000 20
  12. 7839 KING President 1 July-November-81 5000 10
  13. 7876 ADAMS Clerk 7788 2 March-May-87 1100 20
  14. 7902 FORD ANALYST 7566 March-December-81 3000 20
  15. 7934 MILLER Clerk 7782 2 March-January-82 1300 10
  16. 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
  1. Sql> break on deptno Skip 2;
  2. sql> Select Deptno,job,sum (SAL) from the emp GROUP by deptno,job
  3. 2 Union
  4. 3 Select Deptno,to_char (null),sum (SAL) from the emp GROUP by Deptno /c10>
  5. 4 Union
  6. 5 Select To_number (null), TO_CHAR (null),sum (sal) from EMP;
  7. DEPTNO JOB SUM (SAL)
  8. ---------- --------- ----------
  9. Ten Clerk 1300
  10. MANAGER 2450
  11. President 5000
  12. 8750
  13. ANALYST 6000
  14. Clerk 1980
  15. MANAGER 2975
  16. 10955
  17. 950 Clerk
  18. MANAGER 2850
  19. Salesman 5600
  20. 9400
  21. 29105
  22. 13 rows have been selected.

Use the UNION ALL operator to query the information in the Dept table under the Scott user

SQL code
  1. Sql> SELECT * from dept
  2. 2 Union All
  3. 3 SELECT * FROM dept;
  4. DEPTNO dname LOC
  5. ---------- -------------- -------------
  6. Ten ACCOUNTING NEW YORK
  7. DALLAS
  8. SALES CHICAGO
  9. OPERATIONS BOSTON
  10. Ten ACCOUNTING NEW YORK
  11. DALLAS
  12. SALES CHICAGO
  13. OPERATIONS BOSTON
  14. 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
    1. Sql> Select Deptno from emp
    2. 2 intersect
    3. 3 Select Deptno from dept;
    4. DEPTNO
    5. ----------
    6. 10
    7. 20
    8. 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
    1. Sql> Select Deptno from dept
    2. 2 minus
    3. 3 Select Deptno from EMP;
    4. DEPTNO
    5. ----------
    6. 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
  1. sql> Select Empno,ename,job,sal
  2. 2 from EMP
  3. 3 where Sal between
  4. 4 minus
  5. 5 Select Empno,ename,job,sal
  6. 6 from EMP
  7. 7 where Sal between 1400 and 3000;
  8. EMPNO ename JOB SAL
  9. ---------- ---------- --------- ----------
  10. 7521 WARD salesman 1250
  11. 7654 MARTIN salesman 1250
  12. 7934 MILLER Clerk 1300

Oracle Learning's set operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.