129 lines
7.0 KiB
HTML
129 lines
7.0 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="Basic SELECT statement" />
|
||
|
<meta name="abstract" content="The format and syntax shown here are very basic. SELECT statements can be more varied than the examples presented in this topic." />
|
||
|
<meta name="description" content="The format and syntax shown here are very basic. SELECT statements can be more varied than the examples presented in this topic." />
|
||
|
<meta name="DC.subject" content="SELECT statement, statements, SELECT, example, examples, specifying column, asterisk (select all columns), FROM clause, FROM clause, description, clause, FROM" />
|
||
|
<meta name="keywords" content="SELECT statement, statements, SELECT, example, examples, specifying column, asterisk (select all columns), FROM clause, FROM clause, description, clause, FROM" />
|
||
|
<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="rbafyselectbasic" />
|
||
|
<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>Basic SELECT statement</title>
|
||
|
</head>
|
||
|
<body id="rbafyselectbasic"><a name="rbafyselectbasic"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Basic SELECT statement</h1>
|
||
|
<div><p>The format and syntax shown here are very basic. SELECT statements
|
||
|
can be more varied than the examples presented in this topic.</p>
|
||
|
<div class="section"><p>You can write SQL statements on one line or on many lines. For
|
||
|
SQL statements in precompiled programs, the rules for the continuation of
|
||
|
lines are the same as those of the host language (the language the program
|
||
|
is written in). A SELECT statement can also be used by a cursor in a program.
|
||
|
Finally, a SELECT statement can be prepared in a dynamic application. </p>
|
||
|
<div class="note"><span class="notetitle">Notes:</span> <ol><li>The SQL statements described in this section can be run on SQL tables
|
||
|
and views, and database physical and logical files.</li>
|
||
|
<li>Character strings specified in an SQL statement (such as those used with
|
||
|
WHERE or VALUES clauses) are case sensitive; that is, uppercase characters
|
||
|
must be entered in uppercase and lowercase characters must be entered in lowercase.
|
||
|
<pre><strong>WHERE</strong> ADMRDEPT='a00' (does not return a result)
|
||
|
|
||
|
<strong>WHERE</strong> ADMRDEPT='A00' (returns a valid department number)</pre>
|
||
|
<p>Comparisons
|
||
|
may not be case sensitive if a shared-weight sort sequence is being used where
|
||
|
uppercase and lowercase characters are treated as the same character.</p>
|
||
|
</li>
|
||
|
</ol>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><p>A SELECT statement can include the following:</p>
|
||
|
</div>
|
||
|
<div class="section"> <ol><li>The name of each column you want to include in the result.</li>
|
||
|
<li>The name of the table or view that contains the data.</li>
|
||
|
<li>A search condition to identify the rows that contain the information you
|
||
|
want.</li>
|
||
|
<li>The name of each column used to group your data.</li>
|
||
|
<li>A search condition that uniquely identifies a group that contains the
|
||
|
information you want.</li>
|
||
|
<li>The order of the results so a specific row among duplicates can be returned.</li>
|
||
|
</ol>
|
||
|
</div>
|
||
|
<div class="section"><p>A SELECT statement looks like this:</p>
|
||
|
<pre> <strong>SELECT</strong> column names
|
||
|
<strong>FROM</strong> table or view name
|
||
|
<strong>WHERE</strong> search condition
|
||
|
<strong>GROUP BY</strong> column names
|
||
|
<strong>HAVING</strong> search condition
|
||
|
<strong>ORDER BY</strong> column-name</pre>
|
||
|
</div>
|
||
|
<div class="section"><p>The SELECT and FROM clauses must be specified. The other clauses
|
||
|
are optional.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>With the SELECT clause, you specify the name of each column you
|
||
|
want to retrieve. For example: </p>
|
||
|
<pre> <strong>SELECT</strong> EMPNO, LASTNAME, WORKDEPT
|
||
|
</pre>
|
||
|
</div>
|
||
|
<div class="section"><p>You can specify that only one column be retrieved, or as many
|
||
|
as 8000 columns. The value of each column you name is retrieved in the order
|
||
|
specified in the SELECT clause.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>If you want to retrieve all columns (in the same order as they
|
||
|
appear in the table's definition), use an asterisk (*) instead of naming the
|
||
|
columns: </p>
|
||
|
<pre> <strong>SELECT</strong> *
|
||
|
</pre>
|
||
|
</div>
|
||
|
<div class="section"><p>The FROM clause specifies the table that you want to select data <em>from</em>.
|
||
|
You can select columns from more than one table. When issuing a SELECT, you
|
||
|
must specify a FROM clause. Issue the following statement: </p>
|
||
|
<pre><strong>SELECT</strong> *
|
||
|
<strong>FROM</strong> EMPLOYEE</pre>
|
||
|
<p>The result is all of the columns
|
||
|
and rows from table EMPLOYEE.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>The SELECT list can also contain expressions, including constants,
|
||
|
special registers, and scalar fullselects. An AS clause can be
|
||
|
used to give the resulting column a name. For example, issue the following
|
||
|
statement: </p>
|
||
|
<pre><strong>SELECT</strong> LASTNAME, SALARY * .05 <strong>AS</strong> RAISE
|
||
|
<strong>FROM</strong> EMPLOYEE
|
||
|
<strong>WHERE</strong> EMPNO = '200140'</pre>
|
||
|
<p>The result of this statement
|
||
|
is:</p>
|
||
|
|
||
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" frame="hsides" border="1" rules="all"><caption>Table 1. Results for query</caption><thead align="left"><tr><th valign="top" id="d0e176">LASTNAME</th>
|
||
|
<th valign="top" id="d0e178">RAISE</th>
|
||
|
</tr>
|
||
|
</thead>
|
||
|
<tbody><tr><td valign="top" headers="d0e176 ">NATZ</td>
|
||
|
<td valign="top" headers="d0e178 ">1421</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>
|