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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1. Retrieving data<br />

Result tables<br />

You can retrieve data using the <strong>SQL</strong> statement SELECT to specify a result table.<br />

This chapter describes how to interactively use SELECT statements to retrieve data<br />

from DB2 tables. It includes the following sections:<br />

v “Result tables”<br />

v “Data types” on page 4<br />

v “Selecting columns: SELECT” on page 5<br />

v “Selecting rows using search conditions: WHERE” on page 8<br />

v “Putting the rows in order: ORDER BY” on page 9<br />

v “Summarizing group values: GROUP BY” on page 11<br />

v “Merging lists of values: UNION” on page 13<br />

v “Creating common table expressions: WITH” on page 14<br />

v “Accessing DB2 data that is not in a table” on page 16<br />

v “Using 15-digit <strong>and</strong> 31-digit precision for decimal numbers” on page 16<br />

v “Finding information in the DB2 catalog” on page 18<br />

For more advanced topics on using SELECT statements, see Chapter 4, “Using<br />

subqueries,” on page 49, <strong>and</strong> Chapter 20, “Planning to access distributed data,” on<br />

page 431.<br />

Examples of <strong>SQL</strong> statements illustrate the concepts that this chapter discusses.<br />

Consider developing <strong>SQL</strong> statements similar to these examples <strong>and</strong> then running<br />

them dynamically using SPUFI or DB2 Query Management Facility (DB2 QMF).<br />

The data retrieved through <strong>SQL</strong> is always in the form of a table, which is called a<br />

result table. Like the tables from which you retrieve the data, a result table has rows<br />

<strong>and</strong> columns. A program fetches this data one row at a time.<br />

Example: SELECT statement: The following SELECT statement retrieves the last<br />

name, first name, <strong>and</strong> phone number of employees in department D11 from the<br />

sample employee table:<br />

SELECT LASTNAME, FIRSTNME, PHONENO<br />

FROM DSN8810.EMP<br />

WHERE WORKDEPT = ’D11’<br />

ORDER BY LASTNAME;<br />

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

LASTNAME FIRSTNME PHONENO<br />

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

ADAMSON BRUCE 4510<br />

BROWN DAVID 4501<br />

JOHN REBA 0672<br />

JONES WILLIAM 0942<br />

LUTZ JENNIFER 0672<br />

PIANKA ELIZABETH 3782<br />

SCOUTTEN MARILYN 1682<br />

STERN IRVING 6432<br />

WALKER JAMES 2986<br />

YAMAMOTO KIYOSHI 2890<br />

YOSHIMURA MASATOSHI 2890<br />

© Copyright IBM Corp. 1983, 2004 3

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

Saved successfully!

Ooh no, something went wrong!