UNION keyword
#
OverviewUNION
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
#
SyntaxUNION
will return distinct results.UNION ALL
will return all results including duplicates.
#
ExamplesLet's assume the following two tables listA
Description | ID |
---|---|
Red Pen | 1 |
Blue Pen | 2 |
Green Pen | 3 |
listB
Description | ID |
---|---|
Pink Pen | 1 |
Black Pen | 2 |
Green Pen | 3 |
will return
Description | ID |
---|---|
Red Pen | 1 |
Blue Pen | 2 |
Green Pen | 3 |
Pink Pen | 1 |
Black Pen | 2 |
will return
Description | ID |
---|---|
Red Pen | 1 |
Blue Pen | 2 |
Green Pen | 3 |
Pink Pen | 1 |
Black Pen | 2 |
Green Pen | 3 |