Using The Set Operators
Using The Set Operators
Set operators are used to join the results of two (or more) SELECT statements.The SET operators
available in Oracle 11g are UNION,UNION ALL,INTERSECT,and MINUS.
The UNION set operator returns the combined results of the two SELECT statements.Essentially,it
removes duplicates from the results i.e. only one row will be listed for each duplicated result.To
counter this behavior,use the UNION ALL set operator which retains the duplicates in the final
result.INTERSECT lists only records that are common to both the SELECT queries; the MINUS set
operator removes the second query's results from the output if they are also found in the first
query's results. INTERSECT and MINUS set operations produce unduplicated results.
All the SET operators share the same degree of precedence among them.Instead,during query
execution, Oracle starts evaluation from left to right or from top to bottom.If explicitly parentheses
are used, then the order may differ as parentheses would be given priority over dangling operators.
Points to remember -
https://www.tutorialspoint.com/sql_certificate/using_the_set_operators.htm 1/5
9/3/21, 12:00 PM Using the Set Operators
UNION
When multiple SELECT queries are joined using UNION operator, Oracle displays the combined
result from all the compounded SELECT queries,after removing all duplicates and in sorted order
(ascending by default), without ignoring the NULL values.
Consider the below five queries joined using UNION operator.The final combined result set
contains value from all the SQLs. Note the duplication removal and sorting of data.
UNION
UNION
UNION
UNION
NUM
-------
To be noted, the columns selected in the SELECT queries must be of compatible data type. Oracle
throws an error message when the rule is violated.
UNION
ERROR at line 1:
UNION ALL
UNION and UNION ALL are similar in their functioning with a slight difference. But UNION ALL
gives the result set without removing duplication and sorting the data. For example,in above query
UNION is replaced by UNION ALL to see the effect.
Consider the query demonstrated in UNION section. Note the difference in the output which is
generated without sorting and deduplication.
https://www.tutorialspoint.com/sql_certificate/using_the_set_operators.htm 2/5
9/3/21, 12:00 PM Using the Set Operators
UNION ALL
UNION ALL
UNION ALL
UNION ALL
NUM
-------
INTERSECT
Using INTERSECT operator, Oracle displays the common rows from both the SELECT statements,
with no duplicates and data arranged in sorted order (ascending by default).
For example,the below SELECT query retrieves the salary which are common in department 10
and 20.As per ISO SQL Standards, INTERSECT is above others in precedence of evaluation of set
operators but this is not still incorporated by Oracle.
SELECT SALARY
FROM employees
WHERE DEPARTMENT_ID = 10
INTRESECT
SELECT SALARY
FROM employees
WHERE DEPARTMENT_ID = 20
SALARY
---------
1500
1200
2000
MINUS
Minus operator displays the rows which are present in the first query but absent in the second
query, with no duplicates and data arranged in ascending order by default.
https://www.tutorialspoint.com/sql_certificate/using_the_set_operators.htm 3/5
9/3/21, 12:00 PM Using the Set Operators
SELECT JOB_ID
FROM employees
WHERE DEPARTMENT_ID = 10
MINUS
SELECT JOB_ID
FROM employees
JOB_ID
-------------
HR
FIN
ADMIN
There may be the scenarios where the compound SELECT statements may have different count
and data type of selected columns. Therefore, to match the column list explicitly, NULL columns are
inserted at the missing positions so as match the count and data type of selected columns in each
SELECT statement. For number columns, zero can also be substituted to match the type of the
columns selected in the query.
In the below query, the data type of employee name (varchar2) and location id (number) do not
match. Therefore, execution of the below query would raise error due to compatibility issue.
FROM employees
UNION
FROM departments;
ERROR at line 1:
Explicitly, columns can be matched by substituting NULL for location id and Employee name.
FROM employees
UNION
FROM departments;
https://www.tutorialspoint.com/sql_certificate/using_the_set_operators.htm 4/5
9/3/21, 12:00 PM Using the Set Operators
The ORDER BY clause can appear only once at the end of the query containing compound
SELECT statements.It implies that individual SELECT statements cannot have ORDER BY clause.
Additionally, the sorting can be based on the columns which appear in the first SELECT query only.
For this reason, it is recommended to sort the compound query using column positions.
The compund query below unifies the results from two departments and sorts by the SALARY
column.
FROM employees
WHERE department_id=10
UNION
FROM employees
WHERE department_id=20
ORDER BY 3;
https://www.tutorialspoint.com/sql_certificate/using_the_set_operators.htm 5/5