The UNION
and UNION ALL
operators are used to combine results from multiple SELECT
statements. Both require the same number of columns and compatible data types in each query.
Example:
SELECT customer_id FROM Customers
UNION
SELECT customer_id FROM Orders;
Example:
SELECT customer_id FROM Customers
UNION ALL
SELECT customer_id FROM Orders;
If a customer_id
appears in both Customers and Orders:
UNION
, it will appear once.UNION ALL
, it will appear twice (or more if it exists multiple times).