Analistable

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?

Choosing how to combine
TablesYou wantUse
Orders and CustomersOrder rows with customer namesJOIN
Orders 2025 and Orders 2026One list of all ordersUNION ALL
Two mailing listsOne list without repeatsUNION

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

orders (4 rows) combined with orders_2025 (2 rows, one identical to O-9001)
OperatorRows returnedNotes
UNION ALL6Keeps every row, faster
UNION5Removes 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

orders UNION ALL orders_2025 (with a source column)
order_idcustomer_idamountsource
O-9001C-1011202026
O-9002C-103752026
O-9003C-101802026
O-9004C-104602026
O-8001C-102902025
O-9001C-1011202025

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.

Related guides