</>
Skip to content
SQL lessons (30/54)

SQL — Union

Basic UNION

SELECT name FROM employees
UNION
SELECT name FROM contractors;

UNION ALL

-- Includes duplicates
SELECT name FROM employees
UNION ALL
SELECT name FROM contractors;

UNION with ORDER BY

SELECT name, salary FROM full_time
UNION
SELECT name, salary FROM part_time
ORDER BY salary DESC;

UNION in subquery

SELECT * FROM (
  SELECT id, name FROM employees
  UNION
  SELECT id, name FROM contractors
) AS all_people;

INTERSECT

-- Common rows
SELECT name FROM employees
INTERSECT
SELECT name FROM managers;

EXCEPT / MINUS

-- Rows in first but not second
SELECT name FROM employees
EXCEPT
SELECT name FROM terminated;

Mini Practice

Write SQL code that:

  1. Uses UNION to combine results
  2. Uses UNION ALL to include duplicates
  3. Uses INTERSECT for common rows
  4. Uses EXCEPT for different rows

Up Next

In the next lesson, you'll learn about CTE — Common Table Expressions.

Related Topics

Frequently Asked Questions about Union

What is Union in SQL?

Union is a fundamental concept in SQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Union?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Union.

Why is Union important in SQL?

Union is essential for SQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.