| Lesson 4 | UNION, INTERSECT, and MINUS |
| Objective | Compare the INTERSECT, MINUS, and UNION commands. |
Oracle SQL provides three set operators that combine the results of two queries:
UNION (and UNION ALL)INTERSECTMINUS (Oracle’s equivalent of the SQL standard EXCEPT)
All three operators work on complete result sets, not on individual rows in isolation.
Instead of joining tables horizontally (like JOIN), set operators stack compatible result sets vertically and then apply
set logic (union, intersection, or difference).
UNION combines two result sets and returns distinct rows.
UNION ALL combines the same result sets but keeps duplicates.
Both require the two queries to return the same number of columns with compatible data types.
Basic syntax:
SELECT column1, column2, column3
FROM table1
WHERE condition1
UNION
SELECT column1, column2, column3
FROM table2
WHERE condition2;
Key rules:
SELECT statements.UNION performs an implicit DISTINCT across all columns in the combined result.UNION ALL skips the distinct step and is often faster when duplicates are acceptable.Example: Using UNION to combine two subsets
SELECT employee_id, first_name, last_name, department_id
FROM employees
WHERE department_id = 10
UNION
SELECT employee_id, first_name, last_name, department_id
FROM employees
WHERE department_id = 20;
This query returns the set of distinct employees who belong to department 10 or department 20.
If an employee somehow appears in both groups, UNION ensures they appear only once in the final result.
Example: UNION ALL to retain duplicates
SELECT employee_id, first_name, last_name, department_id
FROM employees
WHERE department_id = 10
UNION ALL
SELECT employee_id, first_name, last_name, department_id
FROM employees
WHERE department_id = 20;
Here, UNION ALL returns all rows from both result sets, including duplicates.
Use this when duplicates are meaningful (for example, counting events) and you do not want the overhead of duplicate removal.
Ordering combined results
To sort the combined result set, apply ORDER BY at the end of the entire expression:
SELECT employee_id, first_name, last_name, department_id
FROM employees
WHERE department_id = 10
UNION
SELECT employee_id, first_name, last_name, department_id
FROM employees
WHERE department_id = 20
ORDER BY department_id, last_name;
The ORDER BY clause applies to the final set, not to the individual queries.
Oracle also supports two other set operators with distinct semantics:
| Command | Description |
UNION |
Combines two result sets and returns all distinct rows from both. |
INTERSECT |
Returns rows that appear in both result sets. |
MINUS |
Returns rows that appear in the first result set but not in the second. |
All three operators remove duplicates by default (that is, they operate on sets).
Only UNION has the special UNION ALL variant, which behaves like a multiset (bag) by preserving duplicates.
A helpful way to think about these operators is with Venn diagrams. Consider two result sets, A and B. The animation below shows which parts of A and B are returned by each operator:
Set operators impose some important structural rules on the queries they combine:
SELECT list, and corresponding columns must have compatible data types.
Example: Using INTERSECT
SELECT TO_CHAR(LAST_UPDATE_DATE, 'YYYY-MM') AS update_month,
UPDATED_BY_USER
FROM PRODUCT
INTERSECT
SELECT TO_CHAR(LAST_UPDATE_DATETIME, 'YYYY-MM') AS update_month,
CREATED_BY_USER
FROM PET_CARE_LOG
ORDER BY 1;
This query returns the (year–month, user) combinations that appear in both tables:
months in which a given employee both updated a product record and created at least one log entry in PET_CARE_LOG.
The next section walks through typical business scenarios and shows which operator to choose.
Consider three common questions and the operator that best answers each one:
PRODUCT table
and added at least one record in the PET_CARE_LOG table?”INTERSECT to find the overlap between two sets of employees.
UNION (or UNION ALL when duplicates are meaningful) to merge customer lists from the dog and cat sales.
MINUS to subtract Lester’s product set from Amy’s product set.
The same ideas apply to classic mailing list examples. Suppose you have two mailing lists, List A and List B:
Imagine two tables:
BOOKSHELF – books currently on the shelf.BOOK_ORDER – books that have been ordered but not yet received.First query: titles from the first half of the alphabet currently on the shelf:
SELECT Title
FROM BOOKSHELF
WHERE Title < 'M%';
Second query: all titles currently on order:
SELECT Title
FROM BOOK_ORDER;
To see the combined set of titles (on hand or on order), use UNION:
SELECT Title
FROM BOOKSHELF
WHERE Title < 'M%'
UNION
SELECT Title
FROM BOOK_ORDER;
The first query returns a certain number of titles from BOOKSHELF, the second returns another set from BOOK_ORDER.
The UNION result contains each distinct title that appears in either table.
If the same title appears in both tables, it appears only once in the final list.
If you needed to preserve duplicate rows for analysis (for example, counting how many times a title appears across both tables),
you would use UNION ALL instead.
By choosing the appropriate set operator and applying these rules, you can express many business questions in a clear, declarative way.
In the next lesson, you will build on these ideas and explore more advanced set operator techniques.