10.12.2012 Views

Application Programming and SQL Guide - Kmlinux

Application Programming and SQL Guide - Kmlinux

Application Programming and SQL Guide - Kmlinux

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

With a single SELECT statement, you can select data from one column or as many<br />

as 750 columns.<br />

Selecting derived columns: SELECT expression<br />

You can select columns derived from a constant, an expression, or a function.<br />

Example: SELECT with an expression: This <strong>SQL</strong> statement generates a result<br />

table in which the second column is a derived column that is generated by adding<br />

the values of the SALARY, BONUS, <strong>and</strong> COMM columns.<br />

SELECT EMPNO, (SALARY + BONUS + COMM)<br />

FROM DSN8810.EMP;<br />

Derived columns in a result table, such as (SALARY + BONUS + COMM), do not<br />

have names. You can use the AS clause to give a name to an unnamed column of<br />

the result table. For information about using the AS clause, see “Naming result<br />

columns: AS.”<br />

To order the rows in a result table by the values in a derived column, specify a<br />

name for the column by using the AS clause, <strong>and</strong> specify that name in the ORDER<br />

BY clause. For information about using the ORDER BY clause, see “Putting the<br />

rows in order: ORDER BY” on page 9.<br />

Eliminating duplicate rows: DISTINCT<br />

The DISTINCT keyword removes duplicate rows from your result table, so that each<br />

row contains unique data.<br />

Example: SELECT DISTINCT: The following SELECT statement lists unique<br />

department numbers for administrating departments:<br />

SELECT DISTINCT ADMRDEPT<br />

FROM DSN8810.DEPT;<br />

The result table looks similar to the following output:<br />

ADMRDEPT<br />

========<br />

A00<br />

D01<br />

E01<br />

Naming result columns: AS<br />

With the AS clause, you can name result columns in a SELECT statement. This is<br />

particularly useful for a column that is derived from an expression or a function. For<br />

syntax <strong>and</strong> more information about the AS clause, see Chapter 4 of DB2 <strong>SQL</strong><br />

Reference.<br />

The following examples show different ways to use the AS clause.<br />

Example: SELECT with AS CLAUSE: The following example of the SELECT<br />

statement gives the expression SALARY+BONUS+COMM the name TOTAL_SAL.<br />

SELECT SALARY+BONUS+COMM AS TOTAL_SAL<br />

FROM DSN8810.EMP<br />

ORDER BY TOTAL_SAL;<br />

Example: CREATE VIEW with AS clause: You can specify result column names in<br />

the select-clause of a CREATE VIEW statement. You do not need to supply the<br />

Chapter 1. Retrieving data 7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!