Skip to content

Commit 129ac7d

Browse files
committed
Doc: improve description of UNION/INTERSECT/EXCEPT syntax.
queries.sgml failed to mention the rather important point that INTERSECT binds more tightly than UNION or EXCEPT. I thought it could also use more discussion of the role of parentheses in these constructs. Per gripe from Christopher Painter-Wakefield. Discussion: https://postgr.es/m/163338891727.12510.3939775743980651160@wrigleys.postgresql.org
1 parent 9cb9c91 commit 129ac7d

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

doc/src/sgml/queries.sgml

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,17 +1649,9 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
16491649
<replaceable>query1</replaceable> INTERSECT <optional>ALL</optional> <replaceable>query2</replaceable>
16501650
<replaceable>query1</replaceable> EXCEPT <optional>ALL</optional> <replaceable>query2</replaceable>
16511651
</synopsis>
1652-
<replaceable>query1</replaceable> and
1652+
where <replaceable>query1</replaceable> and
16531653
<replaceable>query2</replaceable> are queries that can use any of
1654-
the features discussed up to this point. Set operations can also
1655-
be nested and chained, for example
1656-
<synopsis>
1657-
<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable> UNION <replaceable>query3</replaceable>
1658-
</synopsis>
1659-
which is executed as:
1660-
<synopsis>
1661-
(<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable>) UNION <replaceable>query3</replaceable>
1662-
</synopsis>
1654+
the features discussed up to this point.
16631655
</para>
16641656

16651657
<para>
@@ -1693,6 +1685,47 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
16931685
the corresponding columns have compatible data types, as
16941686
described in <xref linkend="typeconv-union-case">.
16951687
</para>
1688+
1689+
<para>
1690+
Set operations can be combined, for example
1691+
<synopsis>
1692+
<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable> EXCEPT <replaceable>query3</replaceable>
1693+
</synopsis>
1694+
which is equivalent to
1695+
<synopsis>
1696+
(<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable>) EXCEPT <replaceable>query3</replaceable>
1697+
</synopsis>
1698+
As shown here, you can use parentheses to control the order of
1699+
evaluation. Without parentheses, <literal>UNION</literal>
1700+
and <literal>EXCEPT</literal> associate left-to-right,
1701+
but <literal>INTERSECT</literal> binds more tightly than those two
1702+
operators. Thus
1703+
<synopsis>
1704+
<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable> INTERSECT <replaceable>query3</replaceable>
1705+
</synopsis>
1706+
means
1707+
<synopsis>
1708+
<replaceable>query1</replaceable> UNION (<replaceable>query2</replaceable> INTERSECT <replaceable>query3</replaceable>)
1709+
</synopsis>
1710+
You can also surround an individual <replaceable>query</replaceable>
1711+
with parentheses. This is important if
1712+
the <replaceable>query</replaceable> needs to use any of the clauses
1713+
discussed in following sections, such as <literal>LIMIT</literal>.
1714+
Without parentheses, you'll get a syntax error, or else the clause will
1715+
be understood as applying to the output of the set operation rather
1716+
than one of its inputs. For example,
1717+
<synopsis>
1718+
SELECT a FROM b UNION SELECT x FROM y LIMIT 10
1719+
</synopsis>
1720+
is accepted, but it means
1721+
<synopsis>
1722+
(SELECT a FROM b UNION SELECT x FROM y) LIMIT 10
1723+
</synopsis>
1724+
not
1725+
<synopsis>
1726+
SELECT a FROM b UNION (SELECT x FROM y LIMIT 10)
1727+
</synopsis>
1728+
</para>
16961729
</sect1>
16971730

16981731

0 commit comments

Comments
 (0)