ibm-information-center/dist/eclipse/plugins/i5OS.ic.sqlp_5.4.0.1/rbafyintersect.htm

186 lines
8.0 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?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="Use INTERSECT keyword" />
<meta name="abstract" content="The INTERSECT keyword returns a combined result set that consists of all of the rows existing in both result sets." />
<meta name="description" content="The INTERSECT keyword returns a combined result set that consists of all of the rows existing in both result sets." />
<meta name="DC.subject" content="examples, INTERSECT" />
<meta name="keywords" content="examples, INTERSECT" />
<meta name="DC.Relation" scheme="URI" content="rbafytexas.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="rbafyintersect" />
<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>Use INTERSECT keyword</title>
</head>
<body id="rbafyintersect"><a name="rbafyintersect"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Use INTERSECT keyword</h1>
<div><p>The INTERSECT keyword returns a combined result set that consists
of all of the rows existing in both result sets.</p>
<div class="section"><p>Suppose you want to find a list of employee numbers that includes:</p>
<ul><li>People in department D11</li>
<li>People whose assignments include projects MA2112, MA2113, and AD3111</li>
</ul>
<p>INTERSECT returns the all of the employee numbers that exist in both
result sets. In other words, this query returns all of the people in department
D11 who are also working on projects MA2112, MA2113, and AD3111.</p>
</div>
<div class="section"><p>To do this, specify:</p>
<pre><strong>SELECT</strong> EMPNO
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>WHERE</strong> WORKDEPT = 'D11'
<strong>INTERSECT</strong>
<strong>SELECT</strong> EMPNO
<strong>FROM</strong> CORPDATA.EMPPROJACT
<strong>WHERE</strong> PROJNO = 'MA2112' <strong>OR</strong>
PROJNO = 'MA2113' <strong>OR</strong>
PROJNO = 'AD3111'
<strong>ORDER BY</strong> EMPNO</pre>
</div>
<div class="section"><p>To better understand the results from these SQL statements, imagine
that SQL goes through the following process:</p>
</div>
<div class="section"><p>Step 1. SQL processes the first SELECT statement:</p>
<pre><strong>SELECT</strong> EMPNO
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>WHERE</strong> WORKDEPT = 'D11'</pre>
<p>Which results in an interim
result 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="d0e84">EMPNO from CORPDATA.EMPLOYEE</th>
</tr>
</thead>
<tbody><tr><td valign="top" headers="d0e84 ">000060</td>
</tr>
<tr><td valign="top" headers="d0e84 ">000150</td>
</tr>
<tr><td valign="top" headers="d0e84 ">000160</td>
</tr>
<tr><td valign="top" headers="d0e84 ">000170</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e84 ">000180</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e84 ">000190</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e84 ">000200</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e84 ">000210</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e84 ">000220</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e84 ">200170</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e84 ">200220</td>
</tr>
</tbody>
</table>
</div>
<p>Step 2. SQL processes the second SELECT statement:</p>
<pre><strong>SELECT</strong> EMPNO
<strong>FROM</strong> CORPDATA.EMPPROJACT
<strong>WHERE</strong> PROJNO='MA2112' <strong>OR</strong>
PROJNO= 'MA2113' <strong>OR</strong>
PROJNO= 'AD3111'</pre>
<p>Which results in another interim
result 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="d0e145">EMPNO from CORPDATA.EMPPROJACT</th>
</tr>
</thead>
<tbody><tr><td valign="top" headers="d0e145 ">000230</td>
</tr>
<tr><td valign="top" headers="d0e145 ">000230</td>
</tr>
<tr><td valign="top" headers="d0e145 ">000240</td>
</tr>
<tr><td valign="top" headers="d0e145 ">000230</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000230</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000240</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000230</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000150</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000170</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000190</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000170</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000190</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000150</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000160</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000180</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000170</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000210</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e145 ">000210</td>
</tr>
</tbody>
</table>
</div>
<p>Step 3. SQL takes the first interim result table, compares it to
the second interim result table, and returns the rows that exist in both tables
minus any duplicate rows, and orders the results.</p>
<pre><strong>SELECT</strong> EMPNO
<strong>FROM</strong> CORPDATA.EMPLOYEE
<strong>WHERE</strong> WORKDEPT = 'D11'
<strong>INTERSECT</strong>
<strong>SELECT</strong> EMPNO
<strong>FROM</strong> CORPDATA.EMPPROJACT
<strong>WHERE</strong> PROJNO='MA2112' <strong>OR</strong>
PROJNO= 'MA2113' <strong>OR</strong>
PROJNO= 'AD3111'
<strong>ORDER BY</strong> EMPNO</pre>
<p>Which results in a combined result table
with values in ascending sequence:</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="d0e242">EMPNO</th>
</tr>
</thead>
<tbody><tr><td valign="top" headers="d0e242 ">000150</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e242 ">000160</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e242 ">000170</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e242 ">000180</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e242 ">000190</td>
</tr>
<tr valign="top"><td valign="top" headers="d0e242 ">000210</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafytexas.htm" title="Learn a variety of ways of tailoring your query to gather data using the SELECT statement. One way to do this is to use the SELECT statement in a program to retrieve a specific row (for example, the row for an employee). Furthermore, you can use clauses to gather data in a specific way.">Retrieve data using the SELECT statement</a></div>
</div>
</div>
</body>
</html>