129 lines
6.8 KiB
HTML
129 lines
6.8 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Subqueries in SELECT statements" />
|
||
|
<meta name="abstract" content="Subqueries can help you to further refine your search conditions." />
|
||
|
<meta name="description" content="Subqueries can help you to further refine your search conditions." />
|
||
|
<meta name="DC.subject" content="SELECT statement, subquery, definition, SELECT statement, example, examples, in SELECT, subquery in SELECT" />
|
||
|
<meta name="keywords" content="SELECT statement, subquery, definition, SELECT statement, example, examples, in SELECT, subquery in SELECT" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafysubquery.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafysubnsearch.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafysubquerynotes.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafyhowsub.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="rbafysubq" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Subqueries in SELECT statements</title>
|
||
|
</head>
|
||
|
<body id="rbafysubq"><a name="rbafysubq"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Subqueries in SELECT statements</h1>
|
||
|
<div><p>Subqueries can help you to further refine your search conditions.</p>
|
||
|
<div class="section"><p>In simple WHERE and HAVING clauses, you can specify a search condition
|
||
|
by using a literal value, a column name, an expression, or a special register.
|
||
|
In those search conditions, you know that you are searching for a specific
|
||
|
value. However, sometimes you cannot supply that value until you have retrieved
|
||
|
other data from a table. For example, suppose you want a list of the employee
|
||
|
numbers, names, and job codes of all employees working on a particular project,
|
||
|
say project number MA2100. The first part of the statement is easy to write: </p>
|
||
|
<pre> <strong>SELECT</strong> EMPNO, LASTNAME, JOB
|
||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE
|
||
|
<strong>WHERE</strong> EMPNO …</pre>
|
||
|
</div>
|
||
|
<div class="section"><p>But you cannot go further because the CORPDATA.EMPLOYEE table
|
||
|
does not include project number data. You do not know which employees are
|
||
|
working on project MA2100 without issuing another SELECT statement against
|
||
|
the CORPDATA.EMP_ACT table.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>With SQL, you can nest one SELECT statement within another to
|
||
|
solve this problem. The inner SELECT statement is called a <strong>subquery</strong>.
|
||
|
The SELECT statement surrounding the subquery is called the <strong>outer-level
|
||
|
SELECT</strong>. Using a subquery, you can issue just one SQL statement to retrieve
|
||
|
the employee numbers, names, and job codes for employees who work on project
|
||
|
MA2100: </p>
|
||
|
<pre> <strong>SELECT</strong> EMPNO, LASTNAME, JOB
|
||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE
|
||
|
<strong>WHERE</strong> EMPNO <strong>IN</strong>
|
||
|
(<strong>SELECT</strong> EMPNO
|
||
|
<strong>FROM</strong> CORPDATA.EMPPROJACT
|
||
|
<strong>WHERE</strong> PROJNO = 'MA2100')</pre>
|
||
|
</div>
|
||
|
<div class="section"><p>To better understand what will result from this SQL statement,
|
||
|
imagine that SQL goes through the following process:</p>
|
||
|
</div>
|
||
|
<div class="section"><p>Step 1: SQL evaluates the subquery to obtain a list of EMPNO values: </p>
|
||
|
<pre>(<strong>SELECT</strong> EMPNO
|
||
|
<strong>FROM</strong> CORPDATA.EMPPROJACT
|
||
|
<strong>WHERE</strong> PROJNO= 'MA2100')</pre>
|
||
|
<p>Which results
|
||
|
in an interim results table:</p>
|
||
|
|
||
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr valign="top"><th valign="top" id="d0e112">EMPNO from CORPDATA.EMPPROJACT</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td valign="top" headers="d0e112 ">000010</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e112 ">000110</td>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</table>
|
||
|
</div>
|
||
|
<p>Step 2: The interim results table then serves as a list in the
|
||
|
search condition of the outer-level SELECT. Essentially, this is the statement
|
||
|
that is run.</p>
|
||
|
<pre><strong>SELECT</strong> EMPNO, LASTNAME, JOB
|
||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE
|
||
|
<strong>WHERE</strong> EMPNO <strong>IN</strong>
|
||
|
('000010', '000110')</pre>
|
||
|
<p>The final result table looks
|
||
|
like this:</p>
|
||
|
|
||
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><thead align="left"><tr><th valign="top" id="d0e145">EMPNO</th>
|
||
|
<th valign="top" id="d0e147">LASTNAME</th>
|
||
|
<th valign="top" id="d0e149">JOB</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td valign="top" headers="d0e145 ">000010</td>
|
||
|
<td valign="top" headers="d0e147 ">HAAS</td>
|
||
|
<td valign="top" headers="d0e149 ">PRES</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e145 ">000110</td>
|
||
|
<td valign="top" headers="d0e147 ">LUCCHESSI</td>
|
||
|
<td valign="top" headers="d0e149 ">SALESREP</td>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</table>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<ul class="ullinks">
|
||
|
<li class="ulchildlink"><strong><a href="rbafysubnsearch.htm">Subqueries and search conditions</a></strong><br />
|
||
|
A subquery can be part of a search condition.</li>
|
||
|
<li class="ulchildlink"><strong><a href="rbafysubquerynotes.htm">Usage notes on subqueries</a></strong><br />
|
||
|
When using subqueries, you should be aware of these usage notes.</li>
|
||
|
<li class="ulchildlink"><strong><a href="rbafyhowsub.htm">Include subqueries in WHERE or HAVING clauses</a></strong><br />
|
||
|
Here are several ways you can use to include a subquery in either a WHERE or HAVING clause.</li>
|
||
|
</ul>
|
||
|
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafysubquery.htm" title="You can use subqueries in a search condition as another way to select your data. Subqueries can be used anywhere an expression can be used.">Use subqueries</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|