102 lines
6.3 KiB
HTML
102 lines
6.3 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="Example: Correlated subquery in select-list" />
|
|
<meta name="abstract" content="Suppose that you want a list of all of the departments, including the department name, number, and manager's name." />
|
|
<meta name="description" content="Suppose that you want a list of all of the departments, including the department name, number, and manager's name." />
|
|
<meta name="DC.subject" content="correlation, subqueries, example select list, subquery, correlated, example select list, examples, correlated subquery, select list" />
|
|
<meta name="keywords" content="correlation, subqueries, example select list, subquery, correlated, example select list, examples, correlated subquery, select list" />
|
|
<meta name="DC.Relation" scheme="URI" content="rbafycorrs.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="rbafyexsub3" />
|
|
<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>Example: Correlated subquery in select-list</title>
|
|
</head>
|
|
<body id="rbafyexsub3"><a name="rbafyexsub3"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Correlated subquery in select-list</h1>
|
|
<div><p>Suppose that you want a list of all of the departments, including
|
|
the department name, number, and manager's name.</p>
|
|
<div class="section"><p>Department names and numbers are found in the CORPDATA.DEPARTMENT
|
|
table. However, DEPARTMENT only has the manager's number, not the manager's
|
|
name. To find the name of the manager for each department, you need to find
|
|
the employee number from the EMPLOYEE table that matches the manager number
|
|
in the DEPARTMENT table and return the name for the row that matches. Only
|
|
departments that currently have a manager assigned are to be returned. Execute
|
|
the following:</p>
|
|
<pre><strong>SELECT</strong> DEPTNO, DEPTNAME,
|
|
(<strong>SELECT</strong> FIRSTNME <strong>CONCAT</strong> ' ' <strong>CONCAT</strong>
|
|
MIDINIT <strong>CONCAT</strong> ' ' <strong>CONCAT</strong> LASTNAME
|
|
<strong>FROM</strong> EMPLOYEE X
|
|
<strong>WHERE</strong> X.EMPNO = Y.MGRNO) AS MANAGER_NAME
|
|
<strong>FROM</strong> DEPARTMENT Y
|
|
<strong>WHERE</strong> MGRNO <strong>IS NOT NULL</strong></pre>
|
|
<p>For each row returned
|
|
for DEPTNO and DEPTNAME, the system finds where EMPNO = MGRNO and returns
|
|
the manager's name. The result table produced by the query has the following
|
|
values:</p>
|
|
|
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="border" border="1" rules="all"><caption>Table 1. Result set for previous query</caption><thead align="left"><tr><th valign="top" width="33.793103448275865%" id="d0e78">DEPTNO</th>
|
|
<th valign="top" width="33.793103448275865%" id="d0e80">DEPTNAME</th>
|
|
<th valign="top" width="32.41379310344827%" id="d0e82">MANAGER_NAME</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr><td valign="top" width="33.793103448275865%" headers="d0e78 ">A00</td>
|
|
<td valign="top" width="33.793103448275865%" headers="d0e80 ">SPIFFY COMPUTER SERVICE DIV.</td>
|
|
<td valign="top" width="32.41379310344827%" headers="d0e82 ">CHRISTINE I HAAS</td>
|
|
</tr>
|
|
<tr><td valign="top" width="33.793103448275865%" headers="d0e78 ">B01</td>
|
|
<td valign="top" width="33.793103448275865%" headers="d0e80 ">PLANNING</td>
|
|
<td valign="top" width="32.41379310344827%" headers="d0e82 ">MICHAEL L THOMPSON</td>
|
|
</tr>
|
|
<tr><td valign="top" width="33.793103448275865%" headers="d0e78 ">C01</td>
|
|
<td valign="top" width="33.793103448275865%" headers="d0e80 ">INFORMATION CENTER</td>
|
|
<td valign="top" width="32.41379310344827%" headers="d0e82 ">SALLY A KWAN</td>
|
|
</tr>
|
|
<tr><td valign="top" width="33.793103448275865%" headers="d0e78 ">D11</td>
|
|
<td valign="top" width="33.793103448275865%" headers="d0e80 ">MANUFACTURING SYSTEMS</td>
|
|
<td valign="top" width="32.41379310344827%" headers="d0e82 ">IRVING F STERN</td>
|
|
</tr>
|
|
<tr><td valign="top" width="33.793103448275865%" headers="d0e78 ">D21</td>
|
|
<td valign="top" width="33.793103448275865%" headers="d0e80 ">ADMINISTRATION SYSTEMS</td>
|
|
<td valign="top" width="32.41379310344827%" headers="d0e82 ">EVA D PULASKI</td>
|
|
</tr>
|
|
<tr><td valign="top" width="33.793103448275865%" headers="d0e78 ">E01</td>
|
|
<td valign="top" width="33.793103448275865%" headers="d0e80 ">SUPPORT SERVICES </td>
|
|
<td valign="top" width="32.41379310344827%" headers="d0e82 ">JOHN B GEYER</td>
|
|
</tr>
|
|
<tr><td valign="top" width="33.793103448275865%" headers="d0e78 ">E11</td>
|
|
<td valign="top" width="33.793103448275865%" headers="d0e80 ">OPERATIONS </td>
|
|
<td valign="top" width="32.41379310344827%" headers="d0e82 ">EILEEN W HENDERSON</td>
|
|
</tr>
|
|
<tr><td valign="top" width="33.793103448275865%" headers="d0e78 ">E21</td>
|
|
<td valign="top" width="33.793103448275865%" headers="d0e80 ">SOFTWARE SUPPORT</td>
|
|
<td valign="top" width="32.41379310344827%" headers="d0e82 ">THEODORE Q SPENSER</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafycorrs.htm" title="You can write a subquery that SQL might need to re-evaluate as it examines each new row (WHERE clause) or group of rows (HAVING clause) in the outer-level SELECT. This is called a correlated subquery.">Correlated subqueries</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |