UNION

SQL · SET Operators

Theory

UNION stacks the results of two SELECT queries on top of each other and removes duplicate rows. Both queries must return the same number of columns in the same order with compatible types; the column names come from the first query. SELECT name, city FROM employees UNION SELECT name, city FROM contractors gives one list of people, with anyone present in both tables shown once.

Removing duplicates means the engine must sort or hash the combined rows. UNION ALL skips that step and keeps every row, so it is faster and is the right choice when duplicates are impossible or wanted (for example appending monthly tables). Columns are matched by position, not by name, so listing columns in a different order silently mixes data. A single ORDER BY at the end sorts the whole combined result.

Why we use it

Similar data often lives in separate tables: employees and contractors, this year's and last year's orders. UNION combines them into one result so you can analyse them together.

When to use it

Use UNION when you need a combined list without duplicates and UNION ALL when you want every row or know there are none. To combine columns side by side from related tables, use a JOIN instead.

How it works

  1. Run the first SELECT
  2. Run the second SELECT
  3. Same column count and types? Yes: Stack the rows together. No: Error: queries do not line up.
  4. UNION ALL? Yes: Keep every row. No: Remove duplicate rows.
  5. Apply ORDER BY to the combined result

Syntax

SELECT columns FROM t1
UNION
SELECT columns FROM t2;

Example

SELECT name FROM employees
UNION
SELECT name FROM contractors;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic