← PL/SQL Topics

SQL Integration

Explain UNION, UNION ALL, INTERSECT and MINUS in Oracle SQL

UNION, INTERSECT, MINUS

Oracle Fusion / EBS · Technical · PL/SQL

GeneralHigh confidence

Set operators for combining, intersecting, or subtracting the results of two compatible queries.

Grounded in: Curated Oracle knowledge layer — PL/SQL

How it works

UNION combines two result sets and removes duplicates — an implicit sort/dedup that costs more than UNION ALL, so use UNION ALL whenever duplicates genuinely can't occur or don't matter. INTERSECT returns only rows present in both result sets. MINUS returns rows from the first result set that don't appear in the second (Oracle's name for what ANSI SQL calls EXCEPT). All three require the same number of columns, with compatible datatypes in the same position, across both queries.

How it works
SELECT employee_id FROM employees WHERE department_id = 90
UNION
SELECT employee_id FROM employees WHERE salary > 15000;

SELECT employee_id FROM employees WHERE department_id = 90
INTERSECT
SELECT employee_id FROM employees WHERE salary > 15000;

SELECT employee_id FROM employees WHERE department_id = 90
MINUS
SELECT employee_id FROM employees WHERE salary > 15000;

Related Questions

Have a follow-up, or a different question?

Continue in Ask Oracle AI