<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!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</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" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">7.4. Combining Queries</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 11.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="queries-order.html" title="7.5. Sorting Rows">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="QUERIES-UNION"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7.4. Combining Queries</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> The results of two queries can be combined using the set operations union, intersection, and difference. The syntax is </p><pre class="synopsis"> <em class="replaceable"><code>query1</code></em> UNION [<span class="optional">ALL</span>] <em class="replaceable"><code>query2</code></em> <em class="replaceable"><code>query1</code></em> INTERSECT [<span class="optional">ALL</span>] <em class="replaceable"><code>query2</code></em> <em class="replaceable"><code>query1</code></em> EXCEPT [<span class="optional">ALL</span>] <em class="replaceable"><code>query2</code></em> </pre><p> <em class="replaceable"><code>query1</code></em> and <em class="replaceable"><code>query2</code></em> are queries that can use any of the features discussed up to this point. Set operations can also be nested and chained, for example </p><pre class="synopsis"> <em class="replaceable"><code>query1</code></em> UNION <em class="replaceable"><code>query2</code></em> UNION <em class="replaceable"><code>query3</code></em> </pre><p> which is executed as: </p><pre class="synopsis"> (<em class="replaceable"><code>query1</code></em> UNION <em class="replaceable"><code>query2</code></em>) UNION <em class="replaceable"><code>query3</code></em> </pre><p> </p><p> <code class="literal">UNION</code> effectively appends the result of <em class="replaceable"><code>query2</code></em> to the result of <em class="replaceable"><code>query1</code></em> (although there is no guarantee that this is the order in which the rows are actually returned). Furthermore, it eliminates duplicate rows from its result, in the same way as <code class="literal">DISTINCT</code>, unless <code class="literal">UNION ALL</code> is used. </p><p> <code class="literal">INTERSECT</code> returns all rows that are both in the result of <em class="replaceable"><code>query1</code></em> and in the result of <em class="replaceable"><code>query2</code></em>. Duplicate rows are eliminated unless <code class="literal">INTERSECT ALL</code> is used. </p><p> <code class="literal">EXCEPT</code> returns all rows that are in the result of <em class="replaceable"><code>query1</code></em> but not in the result of <em class="replaceable"><code>query2</code></em>. (This is sometimes called the <em class="firstterm">difference</em> between two queries.) Again, duplicates are eliminated unless <code class="literal">EXCEPT ALL</code> is used. </p><p> In order to calculate the union, intersection, or difference of two queries, the two queries must be <span class="quote">“<span class="quote">union compatible</span>”</span>, which means that they return the same number of columns and the corresponding columns have compatible data types, as described in <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a>. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="queries.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="queries-order.html">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">Home</a></td><td width="40%" align="right" valign="top"> 7.5. Sorting Rows</td></tr></table></div></body></html>