T-SQL reference › UNION

UNION vs UNION ALL in SQL Server

UNION stacks rows. UNION ALL keeps duplicates. UNION distincts them, which means a sort. If the two piles cannot overlap, use ALL.

This week and a one-off extract

SELECT order_id, total_amount, 'live' AS source FROM Orders
WHERE order_date >= CAST(GETDATE() AS date)
UNION ALL
SELECT order_id, total_amount, 'archive' FROM OrdersArchive
WHERE order_date >= CAST(GETDATE() AS date);

Column count and types must match. Names come from the first SELECT.

Try it on Harbour & Co

Amy Nair (Finance Lead) needs this now: “Three numbers for shipped orders, all time: how many, total revenue, average order.” Write the query, run it, send the results. On the paid plan.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets