SQL Keywords

SQL UNION

What does the SQL UNION operator do?

UNION merges the result sets of two or more SELECT statements into a single distinct list.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL UNION:

SQL UNION Full Explanation

UNION is a set operator that concatenates rows from multiple SELECT statements and removes exact duplicates in the combined output. Each SELECT must return the same number of columns, and corresponding columns must be type-compatible. The operator applies DISTINCT semantics automatically, so any repeated rows are eliminated. Column names in the final result are taken from the first SELECT. Optional ORDER BY or LIMIT clauses apply to the entire unioned set when placed after the last SELECT. Because UNION performs duplicate elimination, it can be slower than UNION ALL, which skips that step.

SQL UNION Syntax

SELECT column_list
FROM table1
[WHERE condition]
UNION
SELECT column_list
FROM table2
[WHERE condition]
[ORDER BY column|position]
[LIMIT n OFFSET m];

SQL UNION Parameters

Example Queries Using SQL UNION

-- Combine active users from two regions
SELECT id, email
FROM users_us
WHERE active = true
UNION
SELECT id, email
FROM users_eu
WHERE active = true;

-- Union three tables and sort
SELECT product_id, name
FROM products_2022
UNION
SELECT product_id, name
FROM products_2023
UNION
SELECT product_id, name
FROM products_2024
ORDER BY product_id;

Expected Output Using SQL UNION

  • Both queries return a single result set that lists unique rows across the specified tables
  • Duplicate rows appearing in multiple source tables are shown only once

Use Cases with SQL UNION

  • Combine partitioned tables into one analytics view
  • Merge historical and current data for reporting
  • De-duplicate results coming from multiple sources
  • Build consolidated lists such as subscriber or product catalogs

Common Mistakes with SQL UNION

  • Using UNION when duplicates are acceptable, causing unnecessary performance cost
  • Mismatching column counts or data types between SELECT statements
  • Forgetting that ORDER BY must occur after the final SELECT, not each one
  • Assuming column aliases from later SELECTs will overwrite names from the first SELECT

Related Topics

First Introduced In

SQL-92 standard

Frequently Asked Questions

What is the difference between UNION and UNION ALL?

UNION removes duplicate rows after merging result sets, while UNION ALL keeps all rows and runs faster because it skips de-duplication.

Do column names need to match in a UNION?

Only the number and data types must align. The final column names come from the first SELECT; later names are ignored.

Where should ORDER BY be placed in a UNION query?

Put ORDER BY after the last SELECT so it sorts the entire combined output. Using ORDER BY inside individual SELECTs usually raises an error.

Does UNION hurt performance compared to UNION ALL?

Yes. UNION performs duplicate elimination, which adds sorting or hashing overhead. Prefer UNION ALL when duplicates are acceptable.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!