@@ -1649,17 +1649,9 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
1649
1649
<replaceable>query1</replaceable> INTERSECT <optional>ALL</optional> <replaceable>query2</replaceable>
1650
1650
<replaceable>query1</replaceable> EXCEPT <optional>ALL</optional> <replaceable>query2</replaceable>
1651
1651
</synopsis>
1652
- <replaceable>query1</replaceable> and
1652
+ where <replaceable>query1</replaceable> and
1653
1653
<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.
1663
1655
</para>
1664
1656
1665
1657
<para>
@@ -1693,6 +1685,47 @@ SELECT DISTINCT ON (<replaceable>expression</replaceable> <optional>, <replaceab
1693
1685
the corresponding columns have compatible data types, as
1694
1686
described in <xref linkend="typeconv-union-case">.
1695
1687
</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>
1696
1729
</sect1>
1697
1730
1698
1731
0 commit comments