170 lines
9.7 KiB
HTML
170 lines
9.7 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="copyright" content="(C) Copyright IBM Corporation 2005" />
|
||
|
<meta name="DC.rights.owner" content="(C) Copyright IBM Corporation 2005" />
|
||
|
<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="task" />
|
||
|
<meta name="DC.Title" content="Get information from a single table" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzatcgetstartsql.htm" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="rzatcgetinfosql" />
|
||
|
<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>Get information from a single table</title>
|
||
|
</head>
|
||
|
<body id="rzatcgetinfosql"><a name="rzatcgetinfosql"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Get information from a single table</h1>
|
||
|
<div><div class="section">Now that you have inserted all the information into our tables, you
|
||
|
must look at it again. In SQL, this is done with the SELECT statement. The
|
||
|
SELECT statement is the most complex of all SQL statements. This statement
|
||
|
is composed of three main clauses:<ol><li>The SELECT clause, which specifies those columns containing the data.</li>
|
||
|
<li>The FROM clause, which specifies the table or tables containing the columns
|
||
|
with the data.</li>
|
||
|
<li>The WHERE clause, which supplies conditions that determine which rows
|
||
|
of data are retrieved.</li>
|
||
|
</ol>
|
||
|
<p>In addition to the three main clauses, there are several other clauses
|
||
|
described in <a href="../sqlp/rbafykickoff.htm">SQL Programming</a> topic
|
||
|
and in the <a href="../db2/rbafzmst02.htm">SQL Reference</a> topic
|
||
|
that can affect the final form of returned data. </p>
|
||
|
</div>
|
||
|
<ol><li class="stepexpand"><span>To see the values you inserted into the INVENTORY_LIST table, type
|
||
|
SELECT and press F4 (prompt). The following display is shown:</span> <pre class="screen"> Specify SELECT Statement
|
||
|
|
||
|
Type SELECT statement information. Press F4 for a list.
|
||
|
|
||
|
FROM tables . . . . . . . . <strong>SAMPLECOLL.INVENTORY_LIST</strong>____________________
|
||
|
SELECT columns . . . . . . . <strong>*</strong>____________________________________________
|
||
|
WHERE conditions . . . . . . _____________________________________________
|
||
|
GROUP BY columns . . . . . . _____________________________________________
|
||
|
HAVING conditions . . . . . _____________________________________________
|
||
|
ORDER BY columns . . . . . . _____________________________________________
|
||
|
FOR UPDATE OF columns . . . _____________________________________________
|
||
|
|
||
|
Bottom
|
||
|
Type choices, press Enter.
|
||
|
|
||
|
DISTINCT rows in result table . . . . . . . . . N Y=Yes, N=No
|
||
|
UNION with another SELECT . . . . . . . . . . . N Y=Yes, N=No
|
||
|
Specify additional options . . . . . . . . . . . N Y=Yes, N=No
|
||
|
|
||
|
|
||
|
|
||
|
F3=Exit F4=Prompt F5=Refresh F6=Insert line F9=Specify subquery
|
||
|
F10=Copy line F12=Cancel F14=Delete line F15=Split line F24=More keys</pre>
|
||
|
</li>
|
||
|
<li class="stepexpand"><span>Type the table name in the <span class="uicontrol">FROM tables</span> field
|
||
|
on the display. To select all columns from the table, type * for the <span class="uicontrol">SELECT
|
||
|
columns</span> field on the display. </span> <div class="p"><pre class="screen"> Display Data
|
||
|
Data width . . . . . . : 71
|
||
|
Position to line . . . . . Shift to column . . . . . .
|
||
|
....+....1....+....2....+....3....+....4....+....5....+....6....+....7.
|
||
|
ITEM ITEM UNIT QUANTITY LAST NUMBER
|
||
|
NUMBER NAME COST ON ORDER ORDERED
|
||
|
HAND DATE
|
||
|
153047 Pencils, red 10.00 25 - 20
|
||
|
229740 Lined tablets 1.50 120 - 20
|
||
|
544931 ***UNKNOWN*** 5.00 - - 20
|
||
|
303476 Paper clips 2.00 100 - 20
|
||
|
559343 Envelopes, legal 3.00 500 - 20
|
||
|
291124 Envelopes, standard .00 - - 20
|
||
|
775298 Chairs, secretary 225.00 6 - 20
|
||
|
073956 Pens, black 20.00 25 - 20
|
||
|
******** End of data ********
|
||
|
|
||
|
F3=Exit F12=Cancel F19=Left F20=Right F21=Split</pre>
|
||
|
</div>
|
||
|
</li>
|
||
|
<li class="stepexpand"><span>Press Enter and the statement runs to select all of the data for
|
||
|
all of the columns in the table. The following output is shown:</span> <p>The
|
||
|
column headings that were defined using the LABEL ON statement are shown.
|
||
|
The ITEM_NAME for the third entry has the default value that was specified
|
||
|
in the CREATE TABLE statement. The QUANTITY_ON_HAND column has a null value
|
||
|
for the rows where no value was inserted. The LAST_ORDER_DATE column contains
|
||
|
all null values because that column is not in any of the INSERT statements
|
||
|
and the column was not defined to have a default value. Similarly, the ORDER_QUANTITY
|
||
|
column contains the default value for all rows.</p>
|
||
|
<p>This statement can be
|
||
|
entered on the Enter SQL Statements display as:</p>
|
||
|
<pre><strong>SELECT</strong> *
|
||
|
<strong>FROM</strong> SAMPLECOLL.INVENTORY_LIST</pre>
|
||
|
</li>
|
||
|
<li class="stepexpand"><span>To limit the number of columns returned by the SELECT statement,
|
||
|
the columns you want to see must be specified. To restrict the number of output
|
||
|
rows returned, the WHERE clause is used. To see only the items that cost more
|
||
|
than 10 dollars, and only have the values for the columns ITEM_NUMBER, UNIT_COST,
|
||
|
and ITEM_NAME returned, type <tt>SELECT</tt> and press F4 (Prompt). The Specify
|
||
|
SELECT Statement display is shown.</span> <div class="p"><pre class="screen"> Specify SELECT Statement
|
||
|
|
||
|
Type SELECT statement information. Press F4 for a list.
|
||
|
|
||
|
FROM tables . . . . . . . . <strong>SAMPLECOLL.INVENTORY_LIST</strong>____________________
|
||
|
SELECT columns . . . . . . . <strong>ITEM_NUMBER, UNIT_COST, ITEM_NAME</strong>____________
|
||
|
WHERE conditions . . . . . . <strong>UNIT_COST > 10.00</strong>____________________________
|
||
|
GROUP BY columns . . . . . . _____________________________________________
|
||
|
HAVING conditions . . . . . _____________________________________________
|
||
|
ORDER BY columns . . . . . . _____________________________________________
|
||
|
FOR UPDATE OF columns . . . _____________________________________________
|
||
|
|
||
|
Bottom
|
||
|
Type choices, press Enter.
|
||
|
|
||
|
DISTINCT rows in result table . . . . . . . . . N Y=Yes, N=No
|
||
|
UNION with another SELECT . . . . . . . . . . . N Y=Yes, N=No
|
||
|
Specify additional options . . . . . . . . . . . N Y=Yes, N=No
|
||
|
|
||
|
|
||
|
|
||
|
F3=Exit F4=Prompt F5=Refresh F6=Insert line F9=Specify subquery
|
||
|
F10=Copy line F12=Cancel F14=Delete line F15=Split line F24=More keys</pre>
|
||
|
</div>
|
||
|
<p>Although only one line is initially shown for each prompt
|
||
|
on the Specify SELECT Statement display, F6 (Insert line) can be used to add
|
||
|
more lines to any of the input areas in the top part of the display. This
|
||
|
can be used if more columns were to be entered in the SELECT columns list,
|
||
|
or a longer, more complex WHERE condition were needed.</p>
|
||
|
</li>
|
||
|
<li class="stepexpand"><span>Fill in the display as shown above.</span></li>
|
||
|
<li class="stepexpand"><span>Press Enter to run the SELECT statement. The following output appears:</span> <div class="p"><pre class="screen"> Display Data
|
||
|
Data width . . . . . . : 41
|
||
|
Position to line . . . . . Shift to column . . . . . .
|
||
|
....+....1....+....2....+....3....+....4.
|
||
|
ITEM UNIT ITEM
|
||
|
NUMBER COST NAME
|
||
|
775298 225.00 Chairs, secretary
|
||
|
073956 20.00 Pens, black
|
||
|
******** End of data ********
|
||
|
|
||
|
F3=Exit F12=Cancel F19=Left F20=Right F21=Split</pre>
|
||
|
</div>
|
||
|
</li>
|
||
|
</ol>
|
||
|
<div class="section">The only rows returned are those whose data values satisfy the condition
|
||
|
specified in the WHERE clause. Furthermore, the only data values returned
|
||
|
are from the columns you explicitly specified in the SELECT clause. Data values
|
||
|
of columns other than those explicitly identified are not returned.</div>
|
||
|
<div class="section">This statement can be entered on the Enter SQL Statements display
|
||
|
as:<pre><strong>SELECT</strong> ITEM_NUMBER,UNIT_COST,ITEM_NAME
|
||
|
<strong>FROM</strong> SAMPLECOLL.INVENTORY_LIST
|
||
|
<strong>WHERE</strong> UNIT_COST > 10.00</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzatcgetstartsql.htm" title="This topic describes how to create and work with schemas, tables, and views using SQL statements in interactive SQL.">Get started with SQL</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|