PostgreSQL 9.5.13 Documentation | |||
---|---|---|---|

Prev | Up | Chapter 7. Queries | Next |

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

UNION [ALL]query1query2INTERSECT [ALL]query1query2EXCEPT [ALL]query1query2

` query1` and

UNIONquery1UNIONquery2query3

which is executed as:

(UNIONquery1) UNIONquery2query3

`UNION` effectively appends the result of
` query2` to the result of

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

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

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.