184 lines
7.8 KiB
HTML
184 lines
7.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="Use EXCEPT keyword" />
|
||
|
<meta name="abstract" content="The EXCEPT keyword returns the result set of the first subselect minus any matching rows from the second subselect." />
|
||
|
<meta name="description" content="The EXCEPT keyword returns the result set of the first subselect minus any matching rows from the second subselect." />
|
||
|
<meta name="DC.subject" content="examples, EXCEPT" />
|
||
|
<meta name="keywords" content="examples, EXCEPT" />
|
||
|
<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="rbafyexcept" />
|
||
|
<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 EXCEPT keyword</title>
|
||
|
</head>
|
||
|
<body id="rbafyexcept"><a name="rbafyexcept"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Use EXCEPT keyword</h1>
|
||
|
<div><p>The EXCEPT keyword returns the result set of the first subselect
|
||
|
minus any matching rows from the second subselect.</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><em>Minus</em> those people whose assignments include projects MA2112, MA2113,
|
||
|
and AD3111</li>
|
||
|
</ul>
|
||
|
<p>This query returns all of the people in department D11 who are <em>not</em> 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>EXCEPT</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="d0e89">EMPNO from CORPDATA.EMPLOYEE</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td valign="top" headers="d0e89 ">000060</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e89 ">000150</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e89 ">000160</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e89 ">000170</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e89 ">000180</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e89 ">000190</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e89 ">000200</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e89 ">000210</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e89 ">000220</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e89 ">200170</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e89 ">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="d0e150">EMPNO from CORPDATA.EMPPROJACT</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td valign="top" headers="d0e150 ">000230</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e150 ">000230</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e150 ">000240</td>
|
||
|
</tr>
|
||
|
<tr><td valign="top" headers="d0e150 ">000230</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000230</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000240</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000230</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000150</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000170</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000190</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000170</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000190</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000150</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000160</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000180</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000170</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000210</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e150 ">000210</td>
|
||
|
</tr>
|
||
|
</tbody>
|
||
|
</table>
|
||
|
</div>
|
||
|
<p>Step 3. SQL takes the first interim result table, removes all of
|
||
|
the rows that also appear in the second interim result table, removes duplicate
|
||
|
rows, and orders the result:</p>
|
||
|
<pre><strong>SELECT</strong> EMPNO
|
||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE
|
||
|
<strong>WHERE</strong> WORKDEPT = 'D11'
|
||
|
<strong>EXCEPT</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="d0e247">EMPNO</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td valign="top" headers="d0e247 ">000060</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e247 ">000200</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e247 ">000220</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e247 ">200170</td>
|
||
|
</tr>
|
||
|
<tr valign="top"><td valign="top" headers="d0e247 ">200220</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>
|