1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>7.4. Combining Queries (UNION, INTERSECT, EXCEPT)</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="queries-select-lists.html" title="7.3. Select Lists" /><link rel="next" href="queries-order.html" title="7.5. Sorting Rows (ORDER BY)" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">7.4. Combining Queries (<code class="literal">UNION</code>, <code class="literal">INTERSECT</code>, <code class="literal">EXCEPT</code>)</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="queries-select-lists.html" title="7.3. Select Lists">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><th width="60%" align="center">Chapter 7. Queries</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="queries-order.html" title="7.5. Sorting Rows (ORDER BY)">Next</a></td></tr></table><hr /></div><div class="sect1" id="QUERIES-UNION"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7.4. Combining Queries (<code class="literal">UNION</code>, <code class="literal">INTERSECT</code>, <code class="literal">EXCEPT</code>) <a href="#QUERIES-UNION" class="id_link">#</a></h2></div></div></div><a id="id-1.5.6.8.2" class="indexterm"></a><a id="id-1.5.6.8.3" class="indexterm"></a><a id="id-1.5.6.8.4" class="indexterm"></a><a id="id-1.5.6.8.5" class="indexterm"></a><a id="id-1.5.6.8.6" class="indexterm"></a><a id="id-1.5.6.8.7" class="indexterm"></a><a id="id-1.5.6.8.8" class="indexterm"></a><p>
3 The results of two queries can be combined using the set operations
4 union, intersection, and difference. The syntax is
5 </p><pre class="synopsis">
6 <em class="replaceable"><code>query1</code></em> UNION [<span class="optional">ALL</span>] <em class="replaceable"><code>query2</code></em>
7 <em class="replaceable"><code>query1</code></em> INTERSECT [<span class="optional">ALL</span>] <em class="replaceable"><code>query2</code></em>
8 <em class="replaceable"><code>query1</code></em> EXCEPT [<span class="optional">ALL</span>] <em class="replaceable"><code>query2</code></em>
10 where <em class="replaceable"><code>query1</code></em> and
11 <em class="replaceable"><code>query2</code></em> are queries that can use any of
12 the features discussed up to this point.
14 <code class="literal">UNION</code> effectively appends the result of
15 <em class="replaceable"><code>query2</code></em> to the result of
16 <em class="replaceable"><code>query1</code></em> (although there is no guarantee
17 that this is the order in which the rows are actually returned).
18 Furthermore, it eliminates duplicate rows from its result, in the same
19 way as <code class="literal">DISTINCT</code>, unless <code class="literal">UNION ALL</code> is used.
21 <code class="literal">INTERSECT</code> returns all rows that are both in the result
22 of <em class="replaceable"><code>query1</code></em> and in the result of
23 <em class="replaceable"><code>query2</code></em>. Duplicate rows are eliminated
24 unless <code class="literal">INTERSECT ALL</code> is used.
26 <code class="literal">EXCEPT</code> returns all rows that are in the result of
27 <em class="replaceable"><code>query1</code></em> but not in the result of
28 <em class="replaceable"><code>query2</code></em>. (This is sometimes called the
29 <em class="firstterm">difference</em> between two queries.) Again, duplicates
30 are eliminated unless <code class="literal">EXCEPT ALL</code> is used.
32 In order to calculate the union, intersection, or difference of two
33 queries, the two queries must be <span class="quote">“<span class="quote">union compatible</span>”</span>,
34 which means that they return the same number of columns and
35 the corresponding columns have compatible data types, as
36 described in <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a>.
38 Set operations can be combined, for example
39 </p><pre class="synopsis">
40 <em class="replaceable"><code>query1</code></em> UNION <em class="replaceable"><code>query2</code></em> EXCEPT <em class="replaceable"><code>query3</code></em>
42 which is equivalent to
43 </p><pre class="synopsis">
44 (<em class="replaceable"><code>query1</code></em> UNION <em class="replaceable"><code>query2</code></em>) EXCEPT <em class="replaceable"><code>query3</code></em>
46 As shown here, you can use parentheses to control the order of
47 evaluation. Without parentheses, <code class="literal">UNION</code>
48 and <code class="literal">EXCEPT</code> associate left-to-right,
49 but <code class="literal">INTERSECT</code> binds more tightly than those two
51 </p><pre class="synopsis">
52 <em class="replaceable"><code>query1</code></em> UNION <em class="replaceable"><code>query2</code></em> INTERSECT <em class="replaceable"><code>query3</code></em>
55 </p><pre class="synopsis">
56 <em class="replaceable"><code>query1</code></em> UNION (<em class="replaceable"><code>query2</code></em> INTERSECT <em class="replaceable"><code>query3</code></em>)
58 You can also surround an individual <em class="replaceable"><code>query</code></em>
59 with parentheses. This is important if
60 the <em class="replaceable"><code>query</code></em> needs to use any of the clauses
61 discussed in following sections, such as <code class="literal">LIMIT</code>.
62 Without parentheses, you'll get a syntax error, or else the clause will
63 be understood as applying to the output of the set operation rather
64 than one of its inputs. For example,
65 </p><pre class="synopsis">
66 SELECT a FROM b UNION SELECT x FROM y LIMIT 10
68 is accepted, but it means
69 </p><pre class="synopsis">
70 (SELECT a FROM b UNION SELECT x FROM y) LIMIT 10
73 </p><pre class="synopsis">
74 SELECT a FROM b UNION (SELECT x FROM y LIMIT 10)
76 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="queries-select-lists.html" title="7.3. Select Lists">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="queries-order.html" title="7.5. Sorting Rows (ORDER BY)">Next</a></td></tr><tr><td width="40%" align="left" valign="top">7.3. Select Lists </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 7.5. Sorting Rows (<code class="literal">ORDER BY</code>)</td></tr></table></div></body></html>