SQL UNION and UNION ALL

Last updated: August 26, 2026.

UNION combines compatible query results and removes duplicate rows. UNION ALL keeps duplicates and usually avoids the cost of de-duplication.

Combine current and archived records

SELECT customer_id, order_date, order_total
FROM current_orders
UNION ALL
SELECT customer_id, order_date, order_total
FROM archived_orders
ORDER BY order_date DESC, customer_id;

Requirements

RuleMeaning
Same number of columnsEach SELECT must return the same column count
Compatible typesCorresponding columns must be type-compatible
Output namesThe first SELECT normally supplies result-column names
Final orderingPlace ORDER BY after the last SELECT to sort the combined result

Use UNION when duplicate elimination is a requirement; otherwise prefer UNION ALL. Add a source label when readers need to know which query produced each row.

admin

admin

Leave a Reply

Your email address will not be published.