In short
Online orders in one table, store bills in another, and the founder wants one combined list. UNION stacks them, and choosing UNION ALL instead of UNION can be the difference between correct and quietly missing rows.
Both stack the results of two queries with matching columns. UNION removes duplicates, which costs extra work, while UNION ALL keeps every row.

Think of it like this
Merging two wedding guest lists
The bride's family and the groom's family each send a guest list. If you just staple them together, some people appear twice: that's UNION ALL. If someone patiently strikes out every repeated name, you get one clean list, but it takes longer: that's UNION. And if two different people share the name 'Rahul Sharma', the striking-out removes a real guest.
Watch it happen
Stack everything, or stack and de-duplicate
UNION ALL
- Keeps every row from both queries
- No extra sorting or comparing
- Faster on big results
- Right choice when rows are genuinely different events
UNION
- Removes rows that are identical in every column
- Needs a sort or hash step to find duplicates
- Slower on large data
- Right choice for a unique list, like all customer IDs
Default to UNION ALL. Use UNION only when you truly want duplicates removed, and know which rows count as duplicates.
The big ideas
What you need to know
Columns must line up
Both queries need the same number of columns, in the same order, with compatible types.
Names come from the first
The result's column names are taken from the first SELECT. Alias them there.
One ORDER BY at the end
ORDER BY applies to the combined result and goes after the last SELECT.
Add a source column
A literal like 'online' AS channel tells you which query each row came from.
See it in code
Walk through the code
1SELECT order_id AS ref_no,2 order_date AS sale_date,3 amount,4 'online' AS channel5FROM online_orders6WHERE order_date >= DATE '2026-11-01'7 8UNION ALL9 10SELECT bill_no, bill_date, total, 'store'11FROM store_bills12WHERE bill_date >= DATE '2026-11-01'13 14ORDER BY sale_date, ref_no;1/4First query: online orders. Its aliases name the final columns.
All November sales from the website and the Mumbai stores in one list. UNION and UNION ALL work the same way in PostgreSQL, MySQL and SQL Server.
Save yourself the pain
Common mistakes
Oops
Using UNION to combine two months of orders.
Do this instead
Two genuinely different ₹499 orders with identical columns would collapse into one. Use UNION ALL.
Oops
SELECT name, city in one query and SELECT city, name in the other.
Do this instead
Columns are matched by position, not by name. Keep the same order in both.
Oops
Adding ORDER BY to each SELECT.
Do this instead
Only one ORDER BY is allowed, at the very end, and it sorts the combined result.
Your mission
One combined list
Quick check
Query A returns 10 rows and query B returns 6 rows. 2 rows are identical in both. How many rows does A UNION ALL B return?
Go deeper
- 1Combine online and store orders into one list with UNION ALL.
- 2Use UNION only when you actually need duplicates removed.
- 3Make sure column order and types match in both queries.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.