SQLPathLab
Set Operations · Intermediate

UNION ALL

combining result sets and keeping duplicates.

Overview

UNION ALL appends query results and preserves every row, including duplicates. Because it does not deduplicate, it is usually more direct and suits partitions, monthly tables, or sources where repeated values are valid. Column counts and corresponding types must still be compatible. If duplicates appear, determine whether they are business facts or already present in an input query.

Basic syntax

SELECT name FROM customers UNION ALL SELECT name FROM suppliers;

Examples

UNION ALL example

Combine customer and supplier names while preserving duplicates so the result can be compared directly with UNION.

Sample table

Table:customers
customers
name
Alice
Bob
Cindy
Table:suppliers
suppliers
name
Alpha
Beta
Alice

Run result

result
name
Alice
Bob
Cindy
Alpha
Beta
Alice

Common issues

Can the SELECT branches return different column counts?

No. Corresponding positions need the same number of type-compatible result columns.

Why can UNION be slower?

UNION removes duplicates; when duplicates are acceptable, UNION ALL is usually more direct.

Where should ORDER BY go?

To sort the combined result, place ORDER BY at the end of the complete set query.

Tags

Filtering

Was this explanation clear?