Overview

UNION is used to combine the results of two or more SELECT statements. To work properly:

  • Each select statement should return the same number of column
  • Each column should have the same type
  • Columns should be in the same order

Syntax

Flow chart showing the syntax of the UNION keyword

  • UNION will return distinct results.
  • UNION ALL will return all results including duplicates.

Examples

Let’s assume the following two tables listA

DescriptionID
Red Pen1
Blue Pen2
Green Pen3

listB

DescriptionID
Pink Pen1
Black Pen2
Green Pen3
  1. liastA UNION listB

will return

DescriptionID
Red Pen1
Blue Pen2
Green Pen3
Pink Pen1
Black Pen2
  1. liastA UNION ALL listB

will return

DescriptionID
Red Pen1
Blue Pen2
Green Pen3
Pink Pen1
Black Pen2
Green Pen3