@@ -1707,17 +1707,9 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
1707
1707
<replaceable>query1</replaceable> INTERSECT <optional>ALL</optional> <replaceable>query2</replaceable>
1708
1708
<replaceable>query1</replaceable> EXCEPT <optional>ALL</optional> <replaceable>query2</replaceable>
1709
1709
</synopsis>
1710
- <replaceable>query1</replaceable> and
1710
+ where <replaceable>query1</replaceable> and
1711
1711
<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.
1721
1713
</para>
1722
1714
1723
1715
<para>
@@ -1751,6 +1743,47 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
1751
1743
the corresponding columns have compatible data types, as
1752
1744
described in <xref linkend="typeconv-union-case"/>.
1753
1745
</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>
1754
1787
</sect1>
1755
1788
1756
1789
0 commit comments