@@ -1651,17 +1651,9 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
1651
1651
<replaceable>query1</replaceable> INTERSECT <optional>ALL</optional> <replaceable>query2</replaceable>
1652
1652
<replaceable>query1</replaceable> EXCEPT <optional>ALL</optional> <replaceable>query2</replaceable>
1653
1653
</synopsis>
1654
- <replaceable>query1</replaceable> and
1654
+ where <replaceable>query1</replaceable> and
1655
1655
<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.
1665
1657
</para>
1666
1658
1667
1659
<para>
@@ -1695,6 +1687,47 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
1695
1687
the corresponding columns have compatible data types, as
1696
1688
described in <xref linkend="typeconv-union-case">.
1697
1689
</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>
1698
1731
</sect1>
1699
1732
1700
1733
0 commit comments