SQL UNION ALL Function

Quick summary: The SQL UNION ALL operator combines result sets and keeps duplicate rows.

SQL UNION ALL Syntax

SELECT_statement1 UNION ALL SELECT_statement2
SQL

SQL UNION ALL Basic examples

SELECT 1 UNION ALL SELECT 1;
SQL
Output:
1, 1

Keeps duplicate rows.

SQL UNION ALL Real-world usage

SELECT id FROM logs_2025 UNION ALL SELECT id FROM logs_2026;
SQL
Output:
All log IDs

Combines partitioned tables efficiently.

SQL UNION ALL Edge cases

SELECT NULL UNION ALL SELECT NULL;
SQL
Output:
NULL, NULL

NULL values are preserved.

SQL UNION ALL Common mistakes

Using UNION ALL when uniqueness is required

Duplicates are not removed.

Incorrect
UNION ALL
Correct
UNION

Choose based on uniqueness needs.

SQL UNION ALL Frequently Asked Questions

What does UNION ALL do in SQL?

Combines results without removing duplicates.

Use case of UNION ALL?

Faster merging of datasets.

Difference from UNION?

Does not remove duplicates.

Performance?

Faster than UNION.

Common mistake?

Expecting unique results.

Column count must match?

Yes.

Order guaranteed?

No.

Used in ETL?

Yes.

Supports ORDER BY?

Yes (final query).

Handles NULL?

Yes.

Used in analytics?

Yes.

Best practice?

Prefer when duplicates are allowed.

SQL UNION ALL Related SQL Keywords