How to combine two tables in SQL
By the Analistable team · Updated · 2 min read
Use a JOIN to put columns from two tables side by side, matched on a key: SELECT … FROM orders o JOIN customers c ON c.customer_id = o.customer_id. Use UNION ALL to stack rows from two tables with the same columns: SELECT … FROM orders_2025 UNION ALL SELECT … FROM orders_2026. UNION (without ALL) also removes duplicate rows.
Part of our guide: How to merge data in pandas, R and SQL
JOIN or UNION?
| Tables | You want | Use |
|---|---|---|
| Orders and Customers | Order rows with customer names | JOIN |
| Orders 2025 and Orders 2026 | One list of all orders | UNION ALL |
| Two mailing lists | One list without repeats | UNION |
JOIN: side by side
SELECT o.order_id, o.amount, c.name
FROM orders o
LEFT JOIN customers c ON c.customer_id = o.customer_id;LEFT JOIN keeps every order; INNER JOIN keeps only orders with a known customer. More tables and pitfalls: joining multiple tables.
UNION ALL: stacked
SELECT order_id, customer_id, amount, '2026' AS source FROM orders
UNION ALL
SELECT order_id, customer_id, amount, '2025' AS source FROM orders_2025;- Both SELECTs need the same number of columns, in the same order, with compatible types. Columns are matched by position, not name.
- Column names come from the first SELECT.
- A literal column (
'2025' AS source) records where each row came from.
UNION vs UNION ALL
| Operator | Rows returned | Notes |
|---|---|---|
| UNION ALL | 6 | Keeps every row, faster |
| UNION | 5 | Removes the duplicate O-9001 row; sorts or hashes to do it |
Use UNION ALL unless you specifically need duplicates removed — UNION does extra work and can hide data problems.
pandas equivalents: merge for JOIN, concat for UNION ALL — see merging DataFrames in pandas.
Worked example
| order_id | customer_id | amount | source |
|---|---|---|---|
| O-9001 | C-101 | 120 | 2026 |
| O-9002 | C-103 | 75 | 2026 |
| O-9003 | C-101 | 80 | 2026 |
| O-9004 | C-104 | 60 | 2026 |
| O-8001 | C-102 | 90 | 2025 |
| O-9001 | C-101 | 120 | 2025 |
With the source column, the two O-9001 rows are no longer identical, so even UNION would keep both. Add the source column only after deciding how duplicates should be treated.
Combine and then join
A common pattern is to stack first and join second: UNION ALL the yearly order tables into one, then LEFT JOIN customers once. Write it with a common table expression:
WITH all_orders AS (
SELECT order_id, customer_id, amount FROM orders
UNION ALL
SELECT order_id, customer_id, amount FROM orders_2025
)
SELECT c.name, SUM(a.amount) AS total
FROM all_orders a
LEFT JOIN customers c ON c.customer_id = a.customer_id
GROUP BY c.name;Frequently asked questions
- How do I combine two tables with the same columns in SQL?
- Use UNION ALL between two SELECT statements that list the same columns in the same order.
- What is the difference between UNION and UNION ALL?
- UNION removes duplicate rows from the combined result; UNION ALL keeps them all and is faster.
- How do I combine two tables without a common column?
- If you want every combination, use CROSS JOIN. If they share columns but not a key, stack them with UNION ALL.