Skip to content

Commit 086cda1

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 9e3be5c commit 086cda1

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
@@ -1651,17 +1651,9 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
16511651
<replaceable>query1</replaceable> INTERSECT <optional>ALL</optional> <replaceable>query2</replaceable>
16521652
<replaceable>query1</replaceable> EXCEPT <optional>ALL</optional> <replaceable>query2</replaceable>
16531653
</synopsis>
1654-
<replaceable>query1</replaceable> and
1654+
where <replaceable>query1</replaceable> and
16551655
<replaceable>query2</replaceable> are queries that can use any of
1656-
the features discussed up to this point. Set operations can also
1657-
be nested and chained, for example
1658-
<synopsis>
1659-
<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable> UNION <replaceable>query3</replaceable>
1660-
</synopsis>
1661-
which is executed as:
1662-
<synopsis>
1663-
(<replaceable>query1</replaceable> UNION <replaceable>query2</replaceable>) UNION <replaceable>query3</replaceable>
1664-
</synopsis>
1656+
the features discussed up to this point.
16651657
</para>
16661658

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

17001733

0 commit comments

Comments
 (0)