Software

UNION vs. UNION ALL With SQL Examples

UNION combines two query results and removes duplicate rows; UNION ALL keeps every row. The same queries run both ways, with row counts and shared rules.

Both stack the rows of two SELECT statements into one result. UNION removes duplicate rows from the combined result; UNION ALL keeps every row, duplicates included. Use UNION ALL unless you specifically need de-duplication: it is faster because the database does not have to compare rows.

Two sources of orders

CREATE TABLE store_orders (customer TEXT, city TEXT);
CREATE TABLE web_orders   (customer TEXT, city TEXT);
INSERT INTO store_orders VALUES ('Ada','London'), ('Grace','Arlington'), ('Ada','London');
INSERT INTO web_orders   VALUES ('Grace','Arlington'), ('Linus','Helsinki');

Ada bought twice in store; Grace bought both in store and online.

SELECT customer, city FROM store_orders
UNION            -- or UNION ALL
SELECT customer, city FROM web_orders;
CompareThe same query with UNION and with UNION ALL (SQLite 3.51.0 output)

UNION (3 rows)

Duplicates removed across and within both inputs: Ada's second store order disappears too.

customercity
AdaLondon
GraceArlington
LinusHelsinki

UNION ALL (5 rows)

Every row from both queries, in order. Highlighted rows are the ones UNION removed.

customercity
AdaLondon
GraceArlington
AdaLondon
GraceArlington
LinusHelsinki

The count query confirmed it: union_rows = 3, union_all_rows = 5.

Note that UNION also removed the duplicate inside store_orders. It de-duplicates the whole result, not just rows that appear in both inputs. If you want “customers who appear in either table, once each”, UNION is right. If you want “all orders from both channels”, UNION ALL is right and UNION silently undercounts.

Rules both share

  • Same number of columns in each SELECT. Breaking this rule failed in our run with: SELECTs to the left and right of UNION do not have the same number of result columns.
  • Columns match by position, not by name. The result uses the first query’s column names.
  • Compatible types per column (strict databases such as PostgreSQL enforce this; SQLite is lenient).
  • One ORDER BY at the very end applies to the combined result. Without it, row order is not guaranteed, even if it looks sorted.

Performance

UNION must sort or hash the full result to find duplicates. On large results that is expensive in memory and time. If the inputs cannot overlap (for example, different date ranges or a column that distinguishes the source), UNION ALL gives the same rows without that cost. A common pattern is to label the source:

SELECT 'store' AS channel, customer FROM store_orders
UNION ALL
SELECT 'web', customer FROM web_orders;

UNION vs JOIN

UNION stacks rows vertically (more rows, same columns). JOIN combines tables horizontally (same rows matched, more columns), as with the customers and orders in the primary key vs. foreign key example. Related operators INTERSECT and EXCEPT return rows present in both queries or in the first only. For filtering out values, including the NULL trap, see SQL not equal.

Sources

  1. SELECT: compound select statements — SQLite
  2. SQLite Foreign Key Support — SQLite

How we made this

Reproduced in a local lab. The two tables and all queries were run in the sqlite3 shell (SQLite 3.51.0). Result tables, counts and the column-mismatch error are quoted verbatim.

Performed by Claude, an AI assistant working in the BinConvert repository (SQLite 3.51.0), November 16, 2026.

This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; all queries were re-run and the results compared. Review: automated test suite and source check, November 16, 2026.

Spotted an error? Tell us and we will correct it.

Copied to clipboard!