1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>7.4.Combining Queries</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="queries.html" title="Chapter7.Queries">
<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">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="queries-union"></a>7.4.Combining Queries</h2></div></div></div>
<a name="id582820"></a><a name="id582831"></a><a name="id582841"></a><a name="id582852"></a><a name="id582862"></a><a name="id582872"></a><a name="id582882"></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 really says
</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">union compatible</span>”,
which means that they return the same number of columns and
the corresponding columns have compatible data types, as
described in <a href="typeconv-union-case.html" title="10.5.UNION, CASE, and Related Constructs">Section10.5, “<code class="literal">UNION</code>, <code class="literal">CASE</code>, and Related Constructs”</a>.
</p>
</div></body>
</html>
|