Union/union all/Intersect/Minus usage in Oracle

Union, performs a union operation on two result sets, excluding duplicate rows, and sorts by default rules at the same time;
Union All, performs a union operation on two result sets, including duplicate rows, without sorting;
Intersect, perform the intersection operation on the two result sets, excluding duplicate rows, and sort by default rules at the same time;
Minus, performs a difference operation on two result sets, excluding duplicate rows, and performs sorting by default rules.
The order by clause must be written in the last result set, and its collation will change the sorting result after the operation; and it is valid for Union, Union All, Intersect, and Minus.
The header will use the fields of the first join block.
1.Union
In union usage, the field types of the two select statements match, and the number of fields must be the same.
The two tables connected by UNION are the same table. Using UNION will filter duplicate rows and display the data of one table.
The two tables in the following SQL query statement are exactly the same, so the query displays the information of a single table.
SCOTT@bys1>select deptno,dname as "aa",loc from dept union select deptno,dname,loc from dept;
    DEPTNO aa LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
 2.Union All
UNION ALL, its usage is the same as union, except that union contains distinct function, it will remove duplicate records from two tables,
And union all will not, so in terms of efficiency, union all will be higher, but it is not used much in practice.
In terms of efficiency, UNION ALL is much faster than UNION, so if it can be confirmed that the two result sets combined do not contain duplicate data, then UNION ALL is used.
Try to use union all, because union needs to be sorted and duplicate records are removed, which is inefficient. 
The two tables in the following SQL query statement are exactly the same, so the query displays the information in the table twice.
SCOTT@bys1>select deptno,dname,loc as "aa" from dept union allselect deptno,dname,loc as "bb" from dept;
    DEPTNO DNAME          aa
---------- -------------- -------------
        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
 3.intersect
Perform the intersection operation on the two result sets, excluding duplicate rows, and sort the default rules at the same time; return the data contained in the two tables together, and take the intersection as the final return result.
The two tables in the following SQL query statement are exactly the same, so the query displays the information of a single table.
SCOTT@bys1>select deptno,dname,loc as "aa" from dept intersect select deptno,dname,loc as "bb" from dept;
    DEPTNO DNAME          aa
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
 4.minus
Perform a difference operation on the two result sets, excluding duplicate rows, while sorting by default rules.
The contents of the first result set are subtracted from the second result set, and the remaining contents are used as the final return result.
The two tables in the following SQL query statement are exactly the same, so the query displays no information.
SCOTT@bys1>select deptno,dname,loc as "aa" from dept minus select deptno,dname,loc as "bb" from dept;
 
no rows selected

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326908984&siteId=291194637