Skip to content

Commit f3fec23

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 f6b5d05 commit f3fec23

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
@@ -1707,17 +1707,9 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
17071707
<replaceable>query1</replaceable> INTERSECT <optional>ALL</optional> <replaceable>query2</replaceable>
17081708
<replaceable>query1</replaceable> EXCEPT <optional>ALL</optional> <replaceable>query2</replaceable>
17091709
</synopsis>
1710-
<replaceable>query1</replaceable> and
1710+
where <replaceable>query1</replaceable> and
17111711
<replaceable>query2</replaceable> are queries that can use any of
1712-
the features discussed up to this point. Set operations can also
1713-
be nested and chained, for example
1714-
<synopsis>
1715-
<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable> UNION <replaceable>query3</replaceable>
1716-
</synopsis>
1717-
which is executed as:
1718-
<synopsis>
1719-
(<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable>) UNION <replaceable>query3</replaceable>
1720-
</synopsis>
1712+
the features discussed up to this point.
17211713
</para>
17221714

17231715
<para>
@@ -1751,6 +1743,47 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
17511743
the corresponding columns have compatible data types, as
17521744
described in <xref linkend="typeconv-union-case"/>.
17531745
</para>
1746+
1747+
<para>
1748+
Set operations can be combined, for example
1749+
<synopsis>
1750+
<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable> EXCEPT <replaceable>query3</replaceable>
1751+
</synopsis>
1752+
which is equivalent to
1753+
<synopsis>
1754+
(<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable>) EXCEPT <replaceable>query3</replaceable>
1755+
</synopsis>
1756+
As shown here, you can use parentheses to control the order of
1757+
evaluation. Without parentheses, <literal>UNION</literal>
1758+
and <literal>EXCEPT</literal> associate left-to-right,
1759+
but <literal>INTERSECT</literal> binds more tightly than those two
1760+
operators. Thus
1761+
<synopsis>
1762+
<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable> INTERSECT <replaceable>query3</replaceable>
1763+
</synopsis>
1764+
means
1765+
<synopsis>
1766+
<replaceable>query1</replaceable> UNION (<replaceable>query2</replaceable> INTERSECT <replaceable>query3</replaceable>)
1767+
</synopsis>
1768+
You can also surround an individual <replaceable>query</replaceable>
1769+
with parentheses. This is important if
1770+
the <replaceable>query</replaceable> needs to use any of the clauses
1771+
discussed in following sections, such as <literal>LIMIT</literal>.
1772+
Without parentheses, you'll get a syntax error, or else the clause will
1773+
be understood as applying to the output of the set operation rather
1774+
than one of its inputs. For example,
1775+
<synopsis>
1776+
SELECT a FROM b UNION SELECT x FROM y LIMIT 10
1777+
</synopsis>
1778+
is accepted, but it means
1779+
<synopsis>
1780+
(SELECT a FROM b UNION SELECT x FROM y) LIMIT 10
1781+
</synopsis>
1782+
not
1783+
<synopsis>
1784+
SELECT a FROM b UNION (SELECT x FROM y LIMIT 10)
1785+
</synopsis>
1786+
</para>
17541787
</sect1>
17551788

17561789

0 commit comments

Comments
 (0)