23rd May 2019: PostgreSQL 12 Beta 1 Released!

The results of two queries can be combined using the set operations union, intersection, and difference. The syntax is

UNION [ALL]`query1`

`query2`

INTERSECT [ALL]`query1`

`query2`

EXCEPT [ALL]`query1`

`query2`

* query1* and

`query2`

UNION`query1`

UNION`query2`

`query3`

which is executed as:

(UNION`query1`

) UNION`query2`

`query3`

`UNION`

effectively appends the result of * query2* to the result of

`query1`

`DISTINCT`

, unless `UNION ALL`

is used.`INTERSECT`

returns all rows that are both in the result of * query1* and in the result of

`query2`

`INTERSECT ALL`

is used.`EXCEPT`

returns all rows that are in the result of * query1* but not in the result of

`query2`

`EXCEPT ALL`

is used.In order to calculate the union, intersection, or difference of two queries, the two queries must be “union compatible”, which means that they return the same number of columns and the corresponding columns have compatible data types, as described in Section 10.5.