codekingpro/portable-devtools
114k
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 16.3 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 operations4 union, intersection, and difference. The syntax is5</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>9</pre><p>10 where <em class="replaceable"><code>query1</code></em> and11 <em class="replaceable"><code>query2</code></em> are queries that can use any of12 the features discussed up to this point.13 </p><p>14 <code class="literal">UNION</code> effectively appends the result of15 <em class="replaceable"><code>query2</code></em> to the result of16 <em class="replaceable"><code>query1</code></em> (although there is no guarantee17 that this is the order in which the rows are actually returned).18 Furthermore, it eliminates duplicate rows from its result, in the same19 way as <code class="literal">DISTINCT</code>, unless <code class="literal">UNION ALL</code> is used.20 </p><p>21 <code class="literal">INTERSECT</code> returns all rows that are both in the result22 of <em class="replaceable"><code>query1</code></em> and in the result of23 <em class="replaceable"><code>query2</code></em>. Duplicate rows are eliminated24 unless <code class="literal">INTERSECT ALL</code> is used.25 </p><p>26 <code class="literal">EXCEPT</code> returns all rows that are in the result of27 <em class="replaceable"><code>query1</code></em> but not in the result of28 <em class="replaceable"><code>query2</code></em>. (This is sometimes called the29 <em class="firstterm">difference</em> between two queries.) Again, duplicates30 are eliminated unless <code class="literal">EXCEPT ALL</code> is used.31 </p><p>32 In order to calculate the union, intersection, or difference of two33 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 and35 the corresponding columns have compatible data types, as36 described in <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a>.37 </p><p>38 Set operations can be combined, for example39</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>41</pre><p>42 which is equivalent to43</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>45</pre><p>46 As shown here, you can use parentheses to control the order of47 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 two50 operators. Thus51</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>53</pre><p>54 means55</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>)57</pre><p>58 You can also surround an individual <em class="replaceable"><code>query</code></em>59 with parentheses. This is important if60 the <em class="replaceable"><code>query</code></em> needs to use any of the clauses61 discussed in following sections, such as <code class="literal">LIMIT</code>.62 Without parentheses, you'll get a syntax error, or else the clause will63 be understood as applying to the output of the set operation rather64 than one of its inputs. For example,65</p><pre class="synopsis">66SELECT a FROM b UNION SELECT x FROM y LIMIT 1067</pre><p>68 is accepted, but it means69</p><pre class="synopsis">70(SELECT a FROM b UNION SELECT x FROM y) LIMIT 1071</pre><p>72 not73</p><pre class="synopsis">74SELECT a FROM b UNION (SELECT x FROM y LIMIT 10)75</pre><p>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 16.3 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>