V3.1.0.

1

cover

Front cover

DB2 SQL Workshop
(Course Code CF12)

Student Notebook
ERC 4.1

IBM Certified Course Material

Student Notebook

Trademarks IBM® is a registered trademark of International Business Machines Corporation. The following are trademarks of International Business Machines Corporation in the United States, or other countries, or both: AIX OS/390 WebSphere DB2 POWER z/OS iSeries QMF 400

Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Windows is a trademark of Microsoft Corporation in the United States, other countries, or both. UNIX® is a registered trademark of The Open Group in the United States and other countries. Linux® is a registered trademark of Linus Torvalds in the United States, other countries, or both. Other company, product, or service names may be trademarks or service marks of others.

February 2007 Edition
The information contained in this document has not been submitted to any formal IBM test and is distributed on an “as is” basis without any warranty either express or implied. The use of this information or the implementation of any of these techniques is a customer responsibility and depends on the customer’s ability to evaluate and integrate them into the customer’s operational environment. While each item may have been reviewed by IBM for accuracy in a specific situation, there is no guarantee that the same or similar results will result elsewhere. Customers attempting to adapt these techniques to their own environments do so at their own risk. © Copyright International Business Machines Corporation 1999, 2007. All rights reserved. This document may not be reproduced in whole or in part without the prior written permission of IBM. Note to U.S. Government Users — Documentation related to restricted rights — Use, duplication or disclosure is subject to restrictions set forth in GSA ADP Schedule Contract with IBM Corp.

V3.1.0.1
Student Notebook

TOC

Contents
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii Course Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Agenda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Unit 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1 Unit Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 1.1 Introduction to Relational Data Management Systems . . . . . . . . . . . . . . . . . . . 1-3 Computerless Data Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4 Relational Database Management System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5 Computerless Data Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6 Asking for Information - RDBMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7 SQL Structured Query Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8 Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Table Name Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10 Sample Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 Sample Alphanumeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12 Date Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13 Time Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-14 CREATE TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15 NULL Characteristic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-16 Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-17 VIEW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18 Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-19 Unit Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20 Unit 2. Simple SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1 Unit Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 2.1 Simple SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 EMPLOYEE Table - Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 EMPLOYEE Table - Part 1 (Cont) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 DEPARTMENT Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 PROJECT Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8 Structure of an SQL Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9 Retrieving All Columns, All Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10 Retrieving All Columns, Limited Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11 Selecting Specific Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12 Select with Ordered Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13 Select with Ordered Output (Cont) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Alternate ORDER BY Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15 Suppressing Duplicate Output Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16 Suppressing Duplicate Output Rows (Cont) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17 Retrieving Rows by Character Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18
© Copyright IBM Corp. 1999, 2007
Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Contents

iii

. . . . . . . . . .3-12 Joining a Table with Itself (1 of 3) . . . . . . . . . . . . . . . .2-25 SELECT with IN . . . . . . . . . . .2-30 Partial String Search . . . . . . . . . . . . . . . . . . . . . .2-33 Unit Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-24 Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. .3-16 Joining a Table with Itself – Other Example (2 of 2) . . . . . . . . 3-1 Unit Objectives . . . . . . . . . . . . . . .4-10 iv DB2 SQL Workshop © Copyright IBM Corp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-4 Retrieving Data from Multiple Tables (JOIN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-8 Another JOIN Example (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . .3-19 Unit 4. . . . . . . . . . . . . . . . . . . . . . . . . . Retrieving Data from Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1999. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .LIKE . . . . . .AND/OR . 4-3 Selecting Calculated Values . . . . . . . . . . . . . . . .2-21 Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-27 Retrieving Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Example with '_' . . . . . . . . . . . . . . . . .Student Notebook Retrieving Rows by Numerical Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .AND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-5 Substitution of NULL Values .3-5 Correlation Names . . . . . . .4-2 4. . . . .4-8 Decimal Representation of a Value . . . . . . . . . . . . . . . . . . . . . . . . . .2-26 SELECT with BETWEEN .3-14 Joining a Table with Itself (3 of 3) . . . . . . . . . . . . . . . . . . . .3-6 JOIN Syntax 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 Retrieving Data from Multiple Tables (Principle) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Scalar Functions and Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Parentheses . . . . . . . . . . . . . . . . . . . . . . .2-32 Checkpoint . . . . . . . . . . . . . . . . . . . . . . . .3-2 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-7 JOIN Syntax 2 (JOIN Keyword) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-28 Partial String Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-22 Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-19 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-6 Arithmetic with NULL Values . . . . . . . . . . . . .3-17 Checkpoint . . . . . . . . . . . .2-23 Multiple Conditions . . . . . . . . . . . . . . . . . . . . .2-29 Partial String Search . . . . . . . . .3-10 JOIN with Three Tables (1 of 2) . . . . . . . . . . . . . . . . . . . .4-4 Naming Result Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-20 AND and OR .3-18 Unit Summary . . . . . . . . . . . . . . . . . . . . . .3-9 Another JOIN Example (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Truncation and Rounding . . . . . . . . . . . . . . . . . . . . .OR . . . . . . . . . . . . . . . . . . . . . . . .3-15 Joining a Table with Itself – Other Example (1 of 2) . . . .4-7 Calculated Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Accessing Data Stored in Multiple Tables . . . . . .Examples with '%' . . . Scalar Functions and Arithmetic . . . . . . . . . . . . . . . . . . . . .2-34 Unit 3. . . . . . . . . . . . . . . . . . . . . . . . 4-1 Unit Objectives . . . . . . . . . . . . . . .4-9 Decimal Values . . . . . . . . . . . . . . . . . . . . . . . . .3-13 Joining a Table with Itself (2 of 3) . . . . .2-31 Negation .3-11 JOIN with Three Tables (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Null Comparison . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .1 Student Notebook TOC Condition on Calculated Values .1 Column Functions and Grouping . Column Functions and Grouping . . . . . . . . . . . . . . . . . . . . . . . . . 5-13 GROUP BY. . . . . . . . . . . . . . . . . . . . .Single Report . . . . . . . . . . . . . . . . . .Six Clauses . . . . . Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-20 Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-22 Unit 6. . . . . . . . . . CHAR Function . . . . . . . . . . . . . . . . . . . . Unit Summary . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9 © Copyright IBM Corp. . Substring of Strings . . . . . . . . . . . . . . . . . . . . Subtraction of Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6 Union Rule Number 2 . . . . . . . . . . . . . . . . . . . 5-1 Unit Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DATE Arithmetic (1 of 2) . . . . . . . Date / Time Scalar Functions . . . 5-2 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21 Unit Summary . . . . . . . . . . . . . . . 5-4 Column Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5 Union Rule Number 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Substring of Strings (Cont) . . . . . . . . DATE / TIME Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 UNION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7 Union Rule Number 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17 SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-1 Unit Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HAVING (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1. . . . . . . . . . . . . . . 5-3 SQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7 Beware of Nulls . . . . . . . . . . . . 5-10 GROUP BY More than One Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .V3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . UNION . 6-3 Multiple Queries . . . . . . . . . . . . . . . . . . Contents v . . . . . . . . . . . . . . . . . . . . . . Date Scalar Functions . . . . . . . . . . . . 5-18 Conceptual Execution of a SELECT (1 of 2) . 5-19 Conceptual Execution of a SELECT (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15 Examples with HAVING . . . . . . . Comparison with Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12 GROUP BY. . . . . . . . . . . . . . . . . 6-4 UNIONing Queries Together . . . . . . . . . . . . . . . . . . . 6-2 6. . . . . . . . . . 1999. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11 The Hardest to Remember Rule in All of SQL! . . . . . . HAVING (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5 Sample Column Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14 GROUP BY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . . . . . . ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9 GROUP BY . . . Concatenation of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Multiple Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11 4-12 4-13 4-14 4-15 4-16 4-17 4-18 4-19 4-20 4-21 4-22 4-23 4-24 4-25 Unit 5. . . . . . . . . . . . . . . . . . . . . . . . . . 5-8 Column Functions Based on Subset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DATE Arithmetic (2 of 2) . . . . . . . . . 6-8 UNION ALL . . . . . . . . . . . . . . . . . . . . .0. . . . . . . . . . . . . . . . . . . . . . . . . 5-16 Restrictions . . Date and Time . . . . . . . .Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Subqueries . . . . . . . . .1 Using Subqueries .7-9 Subquery Using NOT IN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-5 Create View PERSINFO .Student Notebook UNION ALL . . . . . . . .6-12 UNION . . . . . . . . . . 7-3 Result Using Separate Selects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-6 Subquery Using IN . . . . . . . . . . . . . .7-13 Unit Summary . . . . . . . . . . . . Checkpoint Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-13 Delete Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-10 Subquery within HAVING Clause . . . . . . . . . . . . . . . . . .7-4 Same Result Using a Subquery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-14 Unit 8. . . . . . . . . . . . . .Generation of Fitting Result Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-1 Unit Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-11 UNION . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-2 8. . . . . . B-1 vi DB2 SQL Workshop © Copyright IBM Corp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1999. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-15 Checkpoint . . . . . . . . . . . . . . . . . .6-16 Unit Summary . . . . . . . . . . . . .7-12 Checkpoint . . . . . . . . . . . . . . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . .7-8 Subquery Using IN . . . . . . . .8-11 Update Columns (1 of 2) . . . . . . . . .8-15 Reminders . . . . .Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-9 Insert Multiple Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-18 Appendix A. . . . . . . . . . .8-16 Checkpoint .7-7 Variations on Predicates . . 7-1 Unit Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-10 UNION ALL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-13 EXCEPT and INTERSECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-7 Inserting Rows into the Table . . . . . . . . . . . . 8-3 TESTEMP Table . . . . . . . . . . . . . . . . . . . . . . . . . . .8-12 Update Columns (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-17 Unit 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-14 Drop Table TESTEMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-2 7. . . . . . . . . . . . . . . . . . Maintaining Data . . . . . . .7-5 Subquery with Basic Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-1 Appendix B. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Maintaining Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-4 Create Table TESTEMP . . .8-17 Unit Summary . . . . . . . .7-11 Restrictions/Reminders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

or both. other countries. © Copyright IBM Corp.0.V3. 1999. or both. or other countries. Other company. or both. product. in the United States. Windows is a trademark of Microsoft Corporation in the United States.1 Student Notebook TMK Trademarks The reader should recognize that the following terms. Trademarks vii . are official trademarks of IBM or other companies: IBM® is a registered trademark of International Business Machines Corporation. other countries. other countries. which appear in the content of this training document.1. or service names may be trademarks or service marks of others. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. or both: AIX® OS/390® WebSphere® DB2® POWER™ z/OS® iSeries™ QMF™ 400® Java and all Java-based trademarks are trademarks of Sun Microsystems. Inc. Linux® is a registered trademark of Linus Torvalds in the United States. UNIX® is a registered trademark of The Open Group in the United States and other countries. The following are trademarks of International Business Machines Corporation in the United States.

Student Notebook viii DB2 SQL Workshop © Copyright IBM Corp. 1999. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. .

1999. This course is appropriate for customers working in all DB2 environments. FROM. application designers.V3. and Windows. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. programmers.1 Student Notebook Uempty Course Description DB2 SQL Workshop Duration: 2 days Purpose This course provides an introduction to the SQL language and applies to the entire DB2 Family. This includes but is not limited to end-users. and system administrators who do not yet have knowledge of the SQL Data Manipulation Language (DML). Course Description ix . including joining a table to itself • Use SQL arithmetic operations • Use scalar and column functions • Use UNION and UNION ALL • INSERT. z/OS. WHERE. database administrators. Linux. GROUP BY. HAVING and ORDER BY clauses • Code inner Joins. Prerequisites • Basic computer literacy • Basic editing skills • Database skills are not required Objectives On completion of this course.1. Audience Everyone needing to write.0. iSeries. or understand SQL queries. VM/VSE. support. you should be able to: • Code SQL statements to retrieve data from a DB2 table. that is. UNIX. including the SELECT. UPDATE and DELETE rows • Code simple CREATE TABLE and CREATE VIEW statements © Copyright IBM Corp.

Student Notebook Contents • • • • • • • • Introduction Simple SQL Queries Retrieving data from multiple tables Scalar functions and arithmetic Column functions and grouping UNION Using Subqueries Maintaining data x DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999. .

1. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.0. 1999.1 Student Notebook Uempty Agenda Day 1 Welcome Introduction Simple SQL Queries Exercise 1 Exercise Review Retrieving Data from Multiple Tables Exercise 2 Exercise Review Scalar Functions and Arithmetic Exercise 3 Day 2 Exercise review Column Functions and Grouping Exercise 4 Exercise Review Union Exercise 5 Exercise Review Using Subqueries Exercise 6 Exercise Review Maintaining Data Exercise 7 Exercise Review © Copyright IBM Corp.V3. Agenda vii .

. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.Student Notebook xii DB2 SQL Workshop © Copyright IBM Corp.

1 Student Notebook Uempty Unit 1.Data Manipulation Language (DML) . table. Introduction 1-1 . view • State the more common DB2 UDB data types • List the most commonly used SQL statements in the: .1.Data Definition Language (DDL) . you should be able to: • Define the terms: . tables. 1999.0. data types. and views. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. What You Should Be Able to Do After completing this unit. row. column. and their meaning How You Will Check Your Progress • Checkpoint questions © Copyright IBM Corp. Introduction What This Unit Is About Database concepts.V3. Unit 1.Data Control Language (DCL) • State three null characteristics.Database.

you should be able to: Define the terms – Database.1 Notes: 1-2 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999. column. view State the more common DB2 data types List the most commonly used SQL statements in the – Data definition language (DDL) – Data manipulation language (DML) – Data control language (DCL) State three null characteristics and their meaning © Copyright IBM Corporation 2006 Figure 1-1. Unit Objectives CF124. . table. row.Student Notebook Unit Objectives After completing this unit.

1 Student Notebook Uempty 1. Unit 1.1 Introduction to Relational Data Management Systems © Copyright IBM Corp.1. 1999.0.V3. Introduction 1-3 . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

• Project data was filed in a 'PROJECTS' folder. People manually managed the data. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook Computerless Data Storage Departments Projects Employees © Copyright IBM Corporation 2006 Figure 1-2. • Employee data was filed in an 'EMPLOYEES' folder. 1999. updated the data. Computerless Data Storage CF124. • Department data was filed in a 'DEPARTMENTS' folder. 1-4 DB2 SQL Workshop © Copyright IBM Corp. and retrieved it on demand.1 Notes: In a traditional office. search through it. .

A table is a set of columns and rows. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1.1 Student Notebook Uempty Relational Database Management System EMPLOYEE EMPNO FIRSTNME 000010 000020 000030 000050 000060 000070 CHRISTINE MICHAEL SALLY JOHN IRVING EVA MID LASTNAME INIT I L A B F D HAAS WORK DEPT A00 DEPARTMENT .. In a well-designed database. Every table has a name.. for instance. he/she must talk to the data management system in a language which the system understands: SQL .1 Notes: A relational database is a collection of tables. Unit 1.V3. 1999. If someone requires information about an employee.. . and a third one data about projects.0. DEPT DEPTNAME NO A00 B01 C01 D01 D11 D21 SPIFFY COMPUTER SERVICE DIV. The data base management system manages the data. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS . . THOMPSON B01 KWAN GEYER STERN PULASKI C01 E01 D11 D21 PROJECT PROJNO PROJNAME ADMIN SERVICES GENERAL ADMIN SYSTEMS PAYROLL PROGRAMMING DEPT NO D01 D21 D21 . Database A collection of tables AD3100 AD3110 AD3111 AD3112 AD3113 IF1000 PERSONNEL PROGRAMMING D21 ACCOUNT PROGRAMMING QUERY SERVICES D21 C01 © Copyright IBM Corporation 2006 Figure 1-3. one table contains employee data. Introduction 1-5 . © Copyright IBM Corp.. each table contains data about a specific subject (entity). another table department data. Relational Database Management System CF124.Structured Query Language. All data is stored in tables.

A medium for communication 5. Someone who returns the data 3.1 Notes: A query requires: 1. 1999. 000010 HAAS I CHRISTINA A00 000340GOUNOT R JASON E21 000050GEYER B JOHN E01 000200 BROWN DAVID D11 000150 ADAMSON BRUCE D11 © Copyright IBM Corporation 2006 Figure 1-4. A language understood by both sides 4.Student Notebook Computerless Data Retrieval Please give me a list of all employees in department D11 sorted by name. A pool with the desired information 1-6 DB2 SQL Workshop © Copyright IBM Corp. Computerless Data Retrieval CF124. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . Someone who requests data (data requestor) 2.

phone.0. Asking for Information . programmer.1. The medium depends on the environment. designer Human language Structured Query Language (SQL) A medium for communication . Pool (folders) with the desired information Tables Someone who fetches the information Integrated search and retrieval functions © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Unit 1. etc. 1999.1 Notes Human communication RDBMS Data requestor End-user.RDBMS CF124. notes. a query tool. Introduction 1-7 . etc. The medium could be a program.1 Student Notebook Uempty Asking for Information .V3.RDBMS Return a list of employees in department D11 sorted by last name RDBMS EMPLOYEE EMPNO LASTNAME MID FIRSTNME INIT WORKDEPT PHONENO 000150 ADAMSON 000200 BROWN 000050 GEYER 000340 GOUNOT 000010 HAAS B R T BRUCE DAVID JOHN JASON CHRISTINE D11 D11 E01 E21 A00 4510 4501 6789 5698 3978 SELECT * FROM EMPLOYEE WHERE WORKDEPT = 'D11' ORDER BY LASTNAME SE L E CT S tatem ent © Copyright IBM Corporation 2006 Figure 1-5.

views and indices • DML is used by all data base users to retrieve information or to maintain data • DCL is also used by data base administrators to allow or disallow activities in DB2 UDB by granting or revoking privileges Most Relational Database Managers understand SQL. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. By means of SQL. SQL Structured Query Language CF124. 1999. for example. COBOL. not how DB2 UDB should access the data. The result of the DML query is a set of qualifying rows from these tables. • DDL is mostly used by data base administrators to define DB2 UDB objects such as tables. C or Java. . we communicate with the RDBMS. The object of a DML query is a set of tables. SQL can be used either interactively or embedded in an application program. PL/1.1 Notes: SQL has similarities with the human language. We tell DB2 UDB what we want. 1-8 DB2 SQL Workshop © Copyright IBM Corp.Student Notebook SQL Structured Query Language © Copyright IBM Corporation 2006 Figure 1-6.

000330 WING LEE .. ... .1 Student Notebook Uempty Table USERA. . • For each row. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. ... .. a column has one entry (field). .1.. . ... 1999. .... © Copyright IBM Corp. Unit 1.. . . 0 row 000030 SALLY A KWAN -.... . • Rows are normally stored unordered. • Column names must be unique within a table... Table Notes: • Every table has a name. . 1941-05-11 ......- 000050 JOHN B GEYER 3214 .0. 1925-09-15 . • All values of a column have the same data type... Introduction 1-9 . . • A table consists of a set of rows and columns.. • Every column has a name. ..EMPLOYEE EMPNO FIRSTNME MIDINIT LASTNAME ..V3. .1 Figure 1-7. 1933-08-24 9220 000020 MICHAEL L THOMSON . . BIRTHDATE COMM 000010 CHRISTINE I HAAS . 1948-02-02 . 1941-07-18 2030 column text column © Copyright IBM Corporation 2006 date column numeric column CF124.

• The qualifier precedes the simple table name and is separated from it by a period.1 Notes: • Every table must have a unique name.EMPLOYEE Table owner / Schema Simple Name USERA Figure 1-8. . 1-10 DB2 SQL Workshop © Copyright IBM Corp.Student Notebook Table Name Construction Qualified Table Name USERA. 1999. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Table Name Construction © Copyright IBM Corporation 2006 EMPLOYEE CF124. • Every table name consists of at least two parts referred to as simple table name and its qualifier (tables used in a distributed processing environment may have three part names). • The simple table name must be unique within a qualifier.

Sample Numeric Data Types CF124.1 Notes: The visual shows a few of the DB2 UDB supported numeric data types. 31 digits m = total number of digits n = number of decimal digits © Copyright IBM Corporation 2006 Figure 1-9. see the appropriate reference manual.483. For complete list of supported data types in your environment.1.0.647 DECIMAL(m.V3.648 to +2. © Copyright IBM Corp.147.1 Student Notebook Uempty Sample Numeric Data Types Data Type SMALLINT INTEGER Comment Whole numbers from -32768 to +32767 Whole numbers from -2. 1999.147. Unit 1. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.483. Introduction 1-11 .n) Decimal numbers with max.

1 Notes: The visual shows a few of the alphanumeric data types available for the various platforms. Sample Alphanumeric Data Types CF124. In queries. Alphanumeric data may contain any character or printable special character.Student Notebook Sample Alphanumeric Data Types Data Type CHAR(n) or CHARACTER(n) VARCHAR(n) Comment STRING (fixed length) STRING (variable length) © Copyright IBM Corporation 2006 Figure 1-10. 1999. see the reference manual for your environment. . For completeness. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. alphanumeric values must be enclosed in single quotes. 1-12 DB2 SQL Workshop © Copyright IBM Corp.

1999. Date Data Type CF124.input verification allows arithmetic sorting component extraction © Copyright IBM Corporation 2006 Figure 1-11.0. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. © Copyright IBM Corp.2005' Within an SQL statement.mm.1 Notes: USA = US Standard EUR = European Standard ISO = International Standards Organization JIS = Japanese Industrial Standard Within an SQL statement. ISO) are accepted. Unit 1. Introduction 1-13 .01.1.1 Student Notebook Uempty Date Data Type Data Type DATE YYYYMMDD Comment Display / Input Format USA mm/dd/yyyy EUR dd. enclose dates in apostrophes. all date formats (USA. EUR.V3. Examples: '2005-01-15' '01/15/2005' '15. JIS.yyyy ISO yyyy-mm-dd JIS yyyy-mm-dd DATE / TIME / TIMESTAMP data type .

1999. Examples: '21. enclose times in apostrophes.Student Notebook Time Data Type Data Type TIME HHMMSS Comment Display / Input Formats USA EUR ISO JIS hh:mm AM or PM hh.mm. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes USA = US Standard EUR = European Standard ISO = International Standards Organization JIS = Japanese Industrial Standard Within an SQL statement.ss hh.mm.ss hh:mm:ss © Copyright IBM Corporation 2006 Figure 1-12. . Time Data Type CF124.45.00' '9:45 PM' '21:45:00' 1-14 DB2 SQL Workshop © Copyright IBM Corp.

For each column.tablename). . BIRTHDATE SALARY BONUS COMM Figure 1-13. ) CF124. .V3. . . NOT NULL. DATE DECIMAL(9.1 Notes: If a table name qualifier is not explicitly supplied in the CREATE TABLE statement. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Introduction 1-15 . the DB2 UDB environment determines the qualifier to be assigned (e.1. NOT NULL. NOT NULL. . CREATE TABLE CHAR(6) VARCHAR(12) CHAR(1) VARCHAR(15) CHAR(3) . .1 Student Notebook Uempty Create Table CREATE TABLE EMPLOYEE ( EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT . .2) DECIMAL(9.g.2) DECIMAL(9. at a minimum. . 1999.0. the following must be defined: Column name Data type (and length) NULL characteristic © Copyright IBM Corp. userid.2) © Copyright IBM Corporation 2006 NOT NULL. Unit 1.

1999. NOT NULL WITH DEFAULT columns are automatically supplied a value when the inserting user or application does not supply its value.1 Notes: When a table is created. A column that is specified as NOT NULL must always contain a value for every row in the table. NULL Characteristic CF124. a system [(value)] or user defined default value will be supplied © Copyright IBM Corporation 2006 Figure 1-14. 1-16 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook NULL Characteristic "nullable" Column can be marked as having an 'unknown value' NOT NULL Column must always have a value NOT NULL WITH Column must always have a value DEFAULT if we do not supply one. . if a null characteristic is not provided the column is marked as "nullable".

Authorization CF124.1 Notes: A table owner or DB2 UDB administrator can grant privileges to other users.V3. Unit 1. Example 1: Table owner USERX issues the following SQL GRANT and REVOKE statement: GRANT SELECT ON PROJECT TO SALLY This statement gives SALLY permission to retrieve data (SELECT) from the table named USERX. or change values of (UPDATE) table USERX. add rows to (INSERT).PROJECT.1. © Copyright IBM Corp. 1999. UPDATE ON EMPLOYEE TO SECR1 This statement authorizes user SECR1 to retrieve data from (SELECT).1 Student Notebook Uempty Authorization GRANT SELECT ON PROJECT TO SALLY DEPARTMENT EMPLOYEE PROJECT REVOKE SELECT ON PROJECT FROM SALLY © Copyright IBM Corporation 2006 Figure 1-15.PROJECT. Example 2: USERX issues the following SQL GRANT: GRANT SELECT. Introduction 1-17 . REVOKE SELECT ON PROJECT FROM SALLY This statement revokes SALLY's permission to retrieve data (SELECT) from the table named USERX. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.EMPLOYEE.0. INSERT.

Data manipulation via some views is limited.Student Notebook VIEW You only see what you should EMPNO 000150 FIRSTNME MIDINIT LASTNAME WORKDEPT PHONENO 4510 BRUCE DAVID JOHN JASON CHRISTINE ADAMSON BROWN B R T D11 D11 E01 E21 A00 000200 4501 000050 GEYER GOUNOT HAAS 6789 000340 5698 000010 3978 Data security © Copyright IBM Corporation 2006 Simplification CF124. A view may encompass derived data. VIEW Notes: Views provide alternative ways of looking at the data of one or more tables.1 Figure 1-16. Purpose: • Improved data security. all views can be used as base tables. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999. averages. • Simplification for the end user (less data. For retrieval. more user-friendly column names) Properties: • • • • • No additional data is stored. A view encompasses a subset of columns and rows. 1-18 DB2 SQL Workshop . for example. • Improved data independence. A view may encompass data from several tables and/or other views. © Copyright IBM Corp. A view can be thought of as containing the data of selected columns and rows of the base table.

Unit 1. 1999. True or False? A database is a collection of tables. What are some of the advantages of using date data types? 3. Name the alphanumeric and numeric data types covered in this unit. Introduction 1-19 .V3. Name the types of SQL statements in the Data Manipulation Language (DML). 4.0. 2. Checkpoint CF124.1 Student Notebook Uempty Checkpoint 1. 2. 3.1 Notes: Please write down your answers below: 1. © Copyright IBM Corp. 4. © Copyright IBM Corporation 2006 Figure 1-17. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1.

Student Notebook

Unit Summary
Having completed this unit, you should be able to: Define the terms
– Database, table, row, column, view

State the more common DB2 data types List the most commonly used SQL statements in the
– Data definition language (DDL) – Data manipulation language (DML) – Data control language (DCL)

State three null characteristics and their meaning

© Copyright IBM Corporation 2006

Figure 1-18. Unit Summary

CF124.1

Notes:

1-20 DB2 SQL Workshop

© Copyright IBM Corp. 1999, 2007
Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

V3.1.0.1
Student Notebook

Uempty

Unit 2. Simple SQL Queries
What This Unit Is About
The SELECT statement is used to retrieve data from a table. In this unit, we will discuss some of the SELECT statement's clauses and their syntax. In subsequent units, we will be looking at more complex SELECT statements.

What You Should Be Able to Do
After completing this unit, you should be able to • Describe four SELECT statement clauses (SELECT, FROM, WHERE, ORDER BY) and their purpose. • Retrieve all rows of a table • Retrieve specific columns • Retrieve rows based on comparisons to numeric or alphanumeric column values • Use the keywords BETWEEN, IN, LIKE, DISTINCT • Order the resulting rows in a desired sequence

How You Will Check Your Progress
• Checkpoint questions • Machine labs

© Copyright IBM Corp. 1999, 2007

Unit 2. Simple SQL Queries

2-1

Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Student Notebook

Unit Objectives
After completing this unit, you should be able to: Describe four clauses of an SQL SELECT statement Use a SELECT statement to:
– Retrieve all rows of a table – Retrieve specific columns – Retrieve rows based on comparisons to numeric or alphanumeric data – Retrieve rows based on specific columns containing NULL values

Use the keywords BETWEEN, IN, LIKE, DISTINCT Order the resulting rows in a desired sequence

© Copyright IBM Corporation 2007

Figure 2-1. Unit Objectives

CF124.1

Notes:

2-2

DB2 SQL Workshop

© Copyright IBM Corp. 1999, 2007
Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

.1 Student Notebook Uempty 2.V3.1 Simple SQL Queries © Copyright IBM Corp. 1999. 2007 Unit 2. Simple SQL Queries 2-3 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.0.1.

EMPLOYEE Table .Part 1 (1 of 2) MID EMPNO -----000010 000020 000030 000050 000060 000070 000090 000100 000110 000120 000130 000140 000150 000160 000170 FIRSTNME -----------CHRISTINE MICHAEL SALLY JOHN IRVING EVA EILEEN THEODORE VINCENZO SEAN DOLORES HEATHER BRUCE ELIZABETH MASATOSHI R J M A INIT ------I L A B F D W Q G LASTNAME --------------HAAS THOMPSON KWAN GEYER STERN PULASKI HENDERSON SPENSER LUCCHESSI O'CONNELL QUINTANA NICHOLLS ADAMSON PIANKA YOSHIMURA WORK DEPT -------A00 B01 C01 E01 D11 D21 E11 E21 A00 A00 C01 C01 D11 D11 D11 PHONE NO ------3978 3476 4738 6789 6423 7831 5498 0972 3490 2167 4578 1793 4510 3782 2890 HIREDATE ---------1965-01-01 1973-10-10 1975-04-05 1949-08-17 1973-09-14 1980-09-30 1970-08-15 1980-06-19 1958-05-16 1963-12-05 1971-07-28 1976-12-15 1972-02-12 1977-10-11 1978-09-15 © Copyright IBM Corporation 2007 Figure 2-2.Student Notebook EMPLOYEE Table .1 Notes: EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE Employee number First name Middle initial Last name Department number Phone number Hire date 2-4 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.Part 1 CF124. .

00 800.00 2092. 1999.00 600.Part 1 (Cont) CF124.00 500.00 40175.00 24680. It is found only once within the Employee table.1.00 400.00 22250.00 38250. Simple SQL Queries 2-5 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.00 23800.00 800.00 © Copyright IBM Corporation 2007 Figure 2-3.00 32250.00 2893.Part 1 (2 of 2) ED EMPNO -----000010 000020 000030 000050 000060 000070 000090 000100 000110 000120 000130 000140 000150 000160 000170 JOB -------PRES MANAGER MANAGER MANAGER MANAGER MANAGER MANAGER MANAGER SALESREP CLERK ANALYST ANALYST DESIGNER DESIGNER DESIGNER LEVEL ----18 18 20 16 16 16 16 14 19 14 16 18 16 17 16 SEX --F M F M M F F M M M F F M F M BIRTHDATE ---------1933-08-24 1948-02-02 1941-05-11 1925-09-15 1945-07-07 1953-05-26 1941-05-15 1956-12-18 1929-11-05 1942-10-18 1925-09-15 1946-01-19 1947-05-17 1955-04-12 1951-01-05 SALARY --------52750.00 800.00 29750.00 2580.00 1904. © Copyright IBM Corp.00 3060.00 2340.00 29250.00 BONUS --------1000.V3.00 3214.1 Student Notebook Uempty EMPLOYEE Table .00 3300.00 700.00 3720.00 600.00 500.00 46500.00 2022. EMPLOYEE Table . 2007 Unit 2.00 26150.00 25280.00 500.00 500.00 900.00 2380.00 1780. .00 28420.00 600.00 1974.00 500.0.00 36170.00 COMM --------4220.00 41250.00 2274.1 Notes: EMPNO JOB EDLEVEL SEX BIRTHDATE SALARY BONUS COMM Employee number (repeated for readability purpose) Job Education level (Ed-level) Sex Date of birth Salary (yearly) Bonus (yearly) Commission (yearly) EMPNO is repeated on this page to facilitate row location.

00 1462.00 500.00 19180.00 1000. R J S H EMPNO 000010 000020 000030 000050 000060 000070 000090 000100 000110 000120 000130 000140 000150 000160 000170 000180 000190 000200 000210 000220 000230 000240 000250 000260 000270 000280 000290 000300 000310 000320 000330 000340 FIRSTNME CHRISTINE MICHAEL SALLY JOHN IRVING EVA EILEEN THEODORE VINCENZO SEAN DOLORES HEATHER BRUCE ELIZABETH MASATOSHI MARILYN JAMES DAVID WILLIAM JENNIFER JAMES SALVATORE DANIEL SYBIL MARIA ETHEL JOHN PHILIP MAUDE RAMLAL WING JASON T K J M S P L R R X F V © Copyright IBM Corp.00 300.00 1380.00 41250.00 500.00 20450.00 23800.00 600.00 500.00 26250.00 400.00 29250.00 800.2-6 LASTNAME HAAS THOMPSON KWAN GEYER STERN PULASKI HENDERSON SPENSER LUCCHESSI O'CONNELL QUINTANA NICHOLLS ADAMSON PIANKA YOSHIMURA SCOUTTEN WALKER BROWN JONES LUTZ JEFFERSON MARINO SMITH JOHNSON PEREZ SCHNEIDER PARKER SMITH SETRIGHT MEHTA LEE GOUNOT WORKDEPT A00 B01 C01 E01 D11 D21 E11 E21 A00 A00 C01 C01 D11 D11 D11 D11 D11 D11 D11 D11 D21 D21 D21 D21 D21 E11 E11 E11 E11 E21 E21 E21 PHONENO 3978 3476 4738 6789 6423 7831 5498 0972 3490 2167 4578 1793 4510 3782 2890 1682 2986 4501 0942 0672 4265 3780 0961 8953 9001 8997 4502 2095 3332 9990 2103 5698 HIREDATE 1965-01-01 1973-10-10 1975-04-05 1949-08-17 1973-09-14 1980-09-30 1970-08-15 1980-06-19 1958-05-16 1963-12-05 1971-07-28 1976-12-15 1972-02-12 1977-10-11 1978-09-15 1973-07-07 1974-07-26 1966-03-03 1979-04-11 1968-08-29 1966-11-21 1979-12-05 1969-10-30 1975-09-11 1980-09-30 1967-03-24 1980-05-30 1972-06-19 1964-09-12 1965-07-07 1976-02-23 1947-05-05 JOB EDLEVEL PRES 18 MANAGER 18 MANAGER 20 MANAGER 16 MANAGER 16 MANAGER 16 MANAGER 16 MANAGER 14 SALESREP 19 CLERK 14 ANALYST 16 ANALYST 18 DESIGNER 16 DESIGNER 17 DESIGNER 16 DESIGNER 17 DESIGNER 16 DESIGNER 16 DESIGNER 17 DESIGNER 18 CLERK 14 CLERK 17 CLERK 15 CLERK 16 CLERK 15 OPERATOR 17 OPERATOR 12 OPERATOR 14 OPERATOR 12 FIELDREP 16 FIELDREP 14 FIELDREP 16 S E X F M F M M F F M M M F F M F M F M M M F M M M F F F M M F M M M BIRTHDATE 1933-08-14 1948-02-02 1941-05-11 1925-09-15 1945-07-07 1953-05-26 1941-05-15 1956-12-18 1929-11-05 1942-10-18 1925-09-15 1946-01-19 1947-05-17 1955-04-12 1951-01-05 1949-02-21 1952-06-25 1941-05-29 1953-02-23 1948-03-19 1935-05-30 1954-03-31 1939-11-12 1936-10-05 1953-05-26 1936-03-28 1946-07-09 1936-10-27 1931-04-21 1932-08-11 1941-07-18 1926-05-17 SALARY BONUS 52750.00 2340.00 3060.00 2092.00 400.00 23840.00 2030.00 1227.00 25280.00 28760.00 24680.00 1534.00 COMM 4220.00 700.00 40175.00 1636. 2007 R .00 1780.00 2274.00 2100.00 3300.00 1707.00 3214.00 400.00 21340.00 1596.00 18270.00 27380.00 15340.00 500.00 800.00 500.00 17750.00 600.00 2893.00 300.00 500.00 2380.00 1904.00 400.00 1907.00 1974.00 32250.00 22250.00 2387.00 400.00 1272.00 2580.00 2301.00 900.00 15900.00 600.00 2022. 1999.00 28420.00 25370.00 500.00 500.00 2217.00 600.00 29840.00 800.00 19950.00 500.00 29750.00 26150.00 Student Notebook Employee Table Data DB2 SQL Workshop M I D I N I T I L A B F D W Q G M A Course materials may not be reproduced in whole or in part without the prior written permission of IBM.00 400.00 300.00 500.00 3720.00 38250.00 2190.00 1420.00 400.00 600.00 22180.00 46500.00 600.00 27740.00 1774.00 36170.00 17250.

2007 Unit 2. © Copyright IBM Corp.V3. they are displayed by the report writer to represent unknown values.1 Notes: DEPTNO DEPTNAME MGRNO ADMRDEPT LOCATION Department number Department name Employee serial number of the responsible manager Department number of the department to which this department reports Location The dashes in the MGRNO and LOCATION columns indicate unknown values. . Simple SQL Queries 2-7 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.1.1 Student Notebook Uempty DEPARTMENT Table DEPTNO -----A00 B01 C01 D01 D11 D21 E01 E11 E21 DEPTNAME ----------------------------SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT MGRNO -----000010 000020 000030 000060 000070 000050 000090 000100 ADMRDEPT -------A00 A00 A00 A00 D01 D01 A00 E01 E01 LOCATION -------- © Copyright IBM Corporation 2007 Figure 2-4. DEPARTMENT Table CF124.0. The dashes are not physically stored in the table.

00 1. .00 2.Student Notebook PROJECT Table DEPT PROJNO -----AD3100 AD3110 AD3111 AD3112 AD3113 IF1000 IF2000 MA2100 MA2110 MA2111 MA2112 MA2113 OP1000 OP1010 OP2000 OP2010 OP2011 OP2012 OP2013 PL2100 PROJNAME --------------------ADMIN SERVICES GENERAL ADMIN SYSTEMS PAYROLL PROGRAMMING PERSONNEL PROGRAMMING ACCOUNT PROGRAMMING QUERY SERVICES USER EDUCATION WELD LINE AUTOMATION W L PROGRAMMING W L PROGRAM DESIGN W L ROBOT DESIGN W L PROD CONT PROGS OPERATION SUPPORT OPERATION GEN SYSTEMS SERVICES SYSTEMS SUPPORT SCP SYSTEMS SUPPORT APPLICATIONS SUPPORT DB/DC SUPPORT WELD LINE PLANNING NO ---D01 D21 D21 D21 D21 C01 C01 D01 D11 D11 D11 D11 E01 E11 E01 E21 E21 E21 E21 B01 RESP EMP -----000010 000070 000230 000250 000270 000030 000030 000010 000060 000220 000150 000160 000050 000090 000050 000100 000320 000330 000340 000020 PR STAFF ----6.50 6.00 1. 1999.00 4.00 2.00 1. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.00 3.1 Notes: PROJNO PROJNAME DEPTNO RESPEMP PRSTAFF PRSTDATE PRENDATE MAJPROJ Project number Project name Responsible department Employee number of the responsible employee Estimated mean staffing Estimated starting date Estimated ending date Major project for a subproject 2-8 DB2 SQL Workshop © Copyright IBM Corp.00 1.00 2.00 5.00 3.00 6.00 5. PROJECT Table CF124.00 12.00 9.00 1.00 1.00 2.00 PRSTDATE ---------1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-02-15 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 PRENDATE ---------1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1982-12-01 1982-12-01 1982-12-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1982-09-15 MAJ PROJ -----AD3100 AD3110 AD3110 AD3110 MA2100 MA2110 MA2110 MA2110 OP1000 OP2000 OP2010 OP2010 OP2010 MA2100 © Copyright IBM Corporation 2007 Figure 2-5.

Each SELECT statement must have a SELECT and a FROM clause. 2007 Unit 2. Structure of an SQL Query CF124.1.Sorts result rows © Copyright IBM Corporation 2007 Figure 2-6. 1999.Conditions (qualifies rows) .Table or view names .V3.0. Simple SQL Queries 2-9 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. The individual clauses will be discussed in detail later on in this course. © Copyright IBM Corp.1 Student Notebook Uempty Structure of an SQL Query SELECT . The other clauses are optional. .1 Notes: Clauses must be coded in the sequence indicated in the visual.Defines result columns Column names Arithmetic expressions Literals (text or numeric) Scalar functions Column functions Concatenation FROM WHERE ORDER BY .

The output of every SELECT statement is called the result table. In practice. we rarely need to see all of the data in a production table. . In a query. 1999. Retrieving All Columns. retrieve only the needed columns and rows.Student Notebook Retrieving All Columns.1 © Copyright IBM Corporation 2007 Figure 2-7. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT MGRNO ADMRDEPT LOCATION 000010 000020 000030 000060 000070 000050 000090 000100 A00 A00 A00 A00 D01 D01 A00 E01 E01 CF124. This is a simple statement useful for displaying all of the data in small tables. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 2-10 DB2 SQL Workshop © Copyright IBM Corp. All Rows I need a listing of all department data SELECT * FROM DEPARTMENT DEPTNO DEPTNAME A00 B01 C01 D01 D11 D21 E01 E11 E21 SPIFFY COMPUTER SERVICE DIV. All Rows Notes: The above statement displays all columns and all rows of the DEPARTMENT table.

V3.1.0.1
Student Notebook

Uempty

Retrieving All Columns, Limited Rows
What does the data look like in the Department table?

SELECT * FROM DEPARTMENT FETCH FIRST 5 ROWS ONLY

DEPTNO A00 B01 C01 D01 D11

DEPTNAME SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS

MGRNO 000010 000020 000030 000060

ADMRDEPT A00 A00 A00 A00 D01

LOCATION -

© Copyright IBM Corporation 2007

Figure 2-8. Retrieving All Columns, Limited Rows

CF124.1

Notes:
The above statement displays all columns of the DEPARTMENT table but limits the result table to the first x rows specified in the FETCH FIRST clause. The FETCH FIRST clause, when used with an ORDER BY clause is useful in producing 'top 10' type reports. More on ORDER BY coming up.

© Copyright IBM Corp. 1999, 2007

Unit 2. Simple SQL Queries

2-11

Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Student Notebook

Selecting Specific Columns
For each department, I need its number, its name and the department to which it reports.

SELECT DEPTNO, DEPTNAME, ADMRDEPT FROM DEPARTMENT

DEPTNO A00 B01 C01 D01 D11 D21 E01 E11 E21

DEPTNAME SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT
© Copyright IBM Corporation 2007

ADMRDEPT A00 A00 A00 A00 D01 D01 A00 E01 E01

Figure 2-9. Selecting Specific Columns

CF124.1

Notes:
Items in the select list must be separated by commas. Spaces before and after commas are optional.

2-12 DB2 SQL Workshop

© Copyright IBM Corp. 1999, 2007
Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

V3.1.0.1
Student Notebook

Uempty

Select with Ordered Output (1 of 2)
By the way, the listing should be sorted by the department reported to SELECT DEPTNO, DEPTNAME, ADMRDEPT FROM DEPARTMENT ORDER BY ADMRDEPT ASC

DEPTNO A00 C01 B01 E01 D01 D11 D21 E21 E11

DEPTNAME ADMRDEPT SPIFFY COMPUTER SERVICE DIV.A00 A00 INFORMATION CENTER A00 PLANNING A00 SUPPORT SERVICES A00 DEVELOPMENT CENTER D01 MANUFACTURING SYSTEMS D01 ADMINISTRATION SYSTEMS E01 SOFTWARE SUPPORT E01 OPERATIONS
© Copyright IBM Corporation 2007

Figure 2-10. Select with Ordered Output

CF124.1

Notes:
• Resulting rows are sequenced according to columns listed in the ORDER BY clause. • The keyword ASC means sort in ascending sequence. This is the default sort sequence and the keyword ASC may be omitted. • DESC means sort in descending sequence. • Depending on the environment, columns used in the ORDER BY clause must be a part of the result table.

© Copyright IBM Corp. 1999, 2007

Unit 2. Simple SQL Queries

2-13

Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Subsequent columns of the ORDER BY clause refine the order of rows. BIRTHDATE FROM EMPLOYEE ORDER BY BIRTHDATE DESC Then. ADMRDEPT FROM DEPARTMENT ORDER BY ADMRDEPT ASC.1 Notes: • If multiple columns are specified in the ORDER BY clause. • ASC or DESC may be specified for each column. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. the youngest employee (the one with the latest birth date) will be listed first. Assume you execute following statement: SELECT EMPNO. 1999. Select with Ordered Output (Cont) CF124. 2-14 DB2 SQL Workshop © Copyright IBM Corp. their left to right sequence within the ORDER BY determines their sorting priority.Student Notebook Select with Ordered Output (2 of 2) I need the previous listing ordered first according to the number to which the department reports and within that sequence sorted by descending department numbers. SELECT DEPTNO. DEPTNO DESC DEPTNO E01 D01 C01 B01 A00 D21 D11 E21 E11 DEPTNAME SUPPORT SERVICES DEVELOPMENT CENTER INFORMATION CENTER PLANNING SPIFFY COMPUTER SERVICE DIV. • A selected column can be identified by the column name or by an integer. referencing the columns position within the selection list. • Date AND time columns may also be sorted in ascending or descending order. . DEPTNAME.and second-column values are further ordered by the values of the third column. The rows with the same first-column values are further ordered by the values of the second column specified. The first ORDER BY column defines the primary order of the rows. The rows with identical first. ADMINISTRATION SYSTEMS MANUFACTURING SYSTEMS SOFTWARE SUPPORT OPERATIONS © Copyright IBM Corporation 2007 ADMRDEPT A00 A00 A00 A00 A00 D01 D01 E01 E01 Figure 2-11. and so on.

1 ASC.1. FIRSTNME. LASTNAME. In queries with a SELECT.1 Student Notebook Uempty Alternate ORDER BY Specifications SELECT LASTNAME.1 Notes. 1 ASC. SEX FROM EMPLOYEE ORDER BY WORKDEPT DESC. Only one ORDER BY clause is allowed per query. 5 DESC ORDER BY 3 DESC. .0. © Copyright IBM Corp. FROM. Simple SQL Queries 2-15 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. SEX DESC Equivalent ORDER BY clauses: ORDER BY WORKDEPT DESC. LASTNAME ASC. WORKDEPT. JOB ASC. 4 ASC. 4. 1999. LASTNAME. 2007 Unit 2. JOB. SEX DESC ORDER BY 3 DESC. JOB. 1. JOB.V3. 5 DESC ORDER BY WORKDEPT DESC. SEX DESC © Copyright IBM Corporation 2007 Figure 2-12. Alternate ORDER BY Specifications CF124. WHERE and ORDER BY clause the ORDER BY must be coded as the last clause in the SELECT statement. 4 ASC. 5 DESC ORDER BY 3 DESC.

Suppressing Duplicate Output Rows CF124. . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. DISTINCT removes duplicate rows. SELECT JOB FROM EMPLOYEE ORDER BY JOB JOB ANALYST ANALYST CLERK CLERK CLERK CLERK SELECT DISTINCT JOB FROM EMPLOYEE JOB ANALYST CLERK DESIGNER FIELDREP MANAGER OPERATOR PRES SALESREP © Copyright IBM Corporation 2007 . 2-16 DB2 SQL Workshop © Copyright IBM Corp.1 Notes: • DISTINCT ensures that the result contains unique rows. 1999.Student Notebook Suppressing Duplicate Output Rows (1 of 2) Now. I want to know the different jobs performed by the employees. . It is effective at the row level. • The keyword DISTINCT must immediately follow the word SELECT. Figure 2-13.

JOB FROM EMPLOYEE ORDER BY WORKDEPT.V3. JOB CLERK PRES SALESREP MANAGER ANALYST MANAGER DESIGNER MANAGER CLERK MANAGER MANAGER MANAGER OPERATOR FIELDREP MANAGER CF124. Simple SQL Queries 2-17 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . . 1999. JOB WORKDEPT A00 A00 A00 B01 C01 C01 C01 D11 D11 D11 D11 D11 D11 JOB CLERK PRES SALESREP MANAGER ANALYST ANALYST MANAGER DESIGNER DESIGNER DESIGNER DESIGNER DESIGNER DESIGNER SELECT DISTINCT WORKDEPT. Suppressing Duplicate Output Rows (Cont) Notes: • DISTINCT eliminates all rows that contain duplicate data in the set of columns specified in the SELECT clause. • DISTINCT must not be used more than once within the scope of the SELECT statement. .1. I need a listing of the job distribution by department SELECT WORKDEPT.1 Student Notebook Uempty Suppressing Duplicate Output Rows (2 of 2) Also. 2007 Unit 2. JOB FROM EMPLOYEE ORDER BY WORKDEPT. . © Copyright IBM Corp.0. • DISTINCT refers to the result row. JOB WORKDEPT A00 A00 A00 B01 C01 C01 D11 D11 D21 D21 E01 E11 E11 E21 E21 © Copyright IBM Corporation 2007 .1 Figure 2-14.

The comparison of alphanumeric columns is case sensitive. The values specified in a predicate must be compatible with the data type of the column or expression to which it is compared. tests. The SELECT list need not include the columns used in the WHERE clause. false. .1 Notes: To qualify specific table rows for further processing use the WHERE clause to specify the conditions rows must meet to be part of the result table. A WHERE clause can have one or more predicates. or conditions. A predicate in the WHERE clause specifies a condition that is true. or unknown for a given row or group of rows. Retrieving Rows by Character Comparison CF124. the value to which it is to be compared must be enclosed in single quotes. 2-18 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. if you compare a column which is defined as alphanumeric. ADMRDEPT DEPARTMENT ADMRDEPT = 'A00' DEPTNO A00 B01 C01 D01 E01 ADMRDEPT A00 A00 A00 A00 A00 © Copyright IBM Corporation 2007 Figure 2-15. For example.Student Notebook Retrieving Rows by Character Comparison I need a list of the departments reporting to department A00 SELECT FROM WHERE DEPTNO. 1999.

© Copyright IBM Corp. the values to be compared must not be enclosed in single quotes.1 Notes: For numeric columns. Simple SQL Queries 2-19 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999. 2007 Unit 2.0.1 Student Notebook Uempty Retrieving Rows by Numerical Comparison I want the last name and education level of all employees with an education level greater than or equal to 19 SELECT FROM WHERE LASTNAME. for example. INTEGER. EDLEVEL EMPLOYEE EDLEVEL >= 19 LASTNAME KWAN LUCCHESSI EDLEVEL 20 19 © Copyright IBM Corporation 2007 Figure 2-16. .1.V3. SMALLINT or DECIMAL. Retrieving Rows by Numerical Comparison CF124.

greater than 20000 -.not equal to SALARY > SALARY < 20000 -. 1999.Student Notebook Comparison Operators SELECT FROM WHERE * EMPLOYEE SALARY = 20000 -. 2-20 DB2 SQL Workshop © Copyright IBM Corp.equal to OR OR OR OR OR SALARY <> 20000 -. Within many query editors.1 Notes: The symbols between the column named SALARY and the value 20000 are interpreted to mean the words to the right of the predicate. . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. two hyphens side by side (no space between them) represents a comment marker.less than or equal to © Copyright IBM Corporation 2007 Figure 2-17. Text to the right of the comment marker is ignored.less than SALARY >= 20000 -.greater than or equal to SALARY <= 20000 -. Comparison Operators CF124.

0. 2007 Unit 2. For predicates combined via OR. AND and OR . Simple SQL Queries 2-21 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. © Copyright IBM Corp. for a row to qualify.1.1 Student Notebook Uempty AND and OR .Principle Y Y R R Yellow AND Oval Y Y Y R Y Y Yellow OR Oval Y Y Y Y R Y © Copyright IBM Corporation 2007 Figure 2-18.Principle CF124.V3. . 1999. rows qualify if at least one predicate is true.1 Notes: When predicates are combined via AND. both predicates must be true.

EDLEVEL EMPLOYEE JOB = 'ANALYST' EDLEVEL = 16 EMPNO 000130 JOB ANALYST EDLEVEL 16 © Copyright IBM Corporation 2007 Figure 2-19. for a row to qualify.1 Notes: When AND is used between predicates. 2-22 DB2 SQL Workshop © Copyright IBM Corp. job. .Student Notebook Multiple Conditions . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. both conditions must be met. Multiple Conditions . In the above example. only rows for analysts with an education level of 16 are displayed. JOB.AND List employee number. 1999. and education level for analysts with an education level of 16 SELECT FROM WHERE AND EMPNO.AND CF124.

EDLEVEL EMPLOYEE JOB = 'ANALYST' EDLEVEL = 20 JOB. © Copyright IBM Corp. Multiple Conditions . In the above example. JOB. 1999. SELECT FROM WHERE OR ORDER BY EMPNO. and employees with an education level of 20 (independent of their job) are displayed.1 Notes: Rows qualify for further processing if at least one condition is met. Simple SQL Queries 2-23 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.0. 2007 Unit 2.1.V3. EMPNO EMPNO 000130 000140 000030 JOB ANALYST ANALYST MANAGER © Copyright IBM Corporation 2007 EDLEVEL 16 18 20 Figure 2-20. employees who are analysts.OR CF124. .1 Student Notebook Uempty Multiple Conditions .OR List the columns below for all analysts and all employees with an education level of 20. Sort the list by job and employee number.

1 © Copyright IBM Corporation 2007 Notes: If you combine multiple conditions by AND and OR.AND/OR JOB ANALYST ANALYST DESIGNER MANAGER PRES EDLEVEL 16 18 18 18 18 CF124.AND/OR List the columns below for all analysts with an ed-level of 16. 2-24 DB2 SQL Workshop © Copyright IBM Corp. SELECT FROM WHERE AND OR ORDER BY EMPNO. EDLEVEL EMPLOYEE JOB = 'ANALYST' EDLEVEL = 16 EDLEVEL = 18 JOB. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. JOB. EMPNO EMPNO 000130 000140 000220 000020 000010 Figure 2-21.Student Notebook Multiple Conditions . Include employees with an ed-level of 18. 1999. Sort the list by job and employee number. Multiple Conditions . AND takes precedence over OR. .

1. . If the order of evaluation is not determined by parentheses. Simple SQL Queries 2-25 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. © Copyright IBM Corp.0.Parentheses CF124. Multiple Conditions .V3. JOB. AND is applied before OR. 1999.Parentheses List the columns below for all analysts with an ed-level of 16 or 18.1 Notes: Search conditions within parentheses are evaluated first. The order in which operators at the same precedence level are evaluated is undefined to allow for the optimization of search conditions. 2007 Unit 2.1 Student Notebook Uempty Multiple Conditions . SELECT FROM WHERE AND OR EMPNO. EDLEVEL EMPLOYEE JOB = 'ANALYST' (EDLEVEL = 16 EDLEVEL = 18) EMPNO 000130 000140 JOB ANALYST ANALYST EDLEVEL 16 18 © Copyright IBM Corporation 2007 Figure 2-22.

or a time. or alphanumeric. An alphanumeric value. The values may be numeric. 'C01'. a date. Sort the listing by ed-level and last name. Example: SELECT * FROM DEPARTMENT WHERE DEPTNO IN ('B01'. 'D01') 2-26 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. a date.1 © Copyright IBM Corporation 2007 Notes: The IN predicate compares a value with a set of values. EDLEVEL EMPLOYEE EDLEVEL IN (14. 19 or 20.20) EDLEVEL. 1999. .19. LASTNAME EDLEVEL 14 14 14 14 14 19 20 CF124. or a time must be enclosed within single quotes.Student Notebook SELECT with IN List last name and ed-level for all employees with an ed-level of 14. SELECT with IN LASTNAME. SELECT FROM WHERE ORDER BY LASTNAME JEFFERSON LEE O'CONNELL SMITH SPENSER LUCCHESSI KWAN Figure 2-23.

1 © Copyright IBM Corporation 2007 Notes: The BETWEEN predicate determines if a given value lies between and including two values specified in ascending order (low value listed first. 2007 Unit 2. The clause WHERE EDLEVEL BETWEEN 12 AND 15 is equivalent to WHERE EDLEVEL >= 12 AND EDLEVEL <= 15 Alphanumeric values could also be used within the between predicate. . 1999. Sort by ed-level.0. Simple SQL Queries 2-27 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Example: WHERE WORKDEPT BETWEEN 'A00' AND 'D00' © Copyright IBM Corp.1 Student Notebook Uempty SELECT with BETWEEN List employee number and ed-level for all employees with an ed-level from 12 through 15. SELECT with BETWEEN EMPNO. EDLEVEL EMPLOYEE EDLEVEL BETWEEN 12 AND 15 EDLEVEL EDLEVEL 12 12 14 14 14 14 14 15 15 CF124.V3.1. the higher or equal value must be listed after the AND). SELECT FROM WHERE ORDER BY EMPNO 000290 000310 000100 000120 000230 000300 000330 000250 000270 Figure 2-24.

If the value is not null. the result is false. DEPTNAME. the result is reversed. The result of "IS NULL" cannot be unknown. If "IS NOT NULL" is specified.Null Comparison List number and name of all departments whose manager is unknown SELECT FROM WHERE DEPTNO. 1999. the result is true. Retrieving Rows . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 2-28 DB2 SQL Workshop © Copyright IBM Corp. MGRNO DEPARTMENT MGRNO IS NULL DEPTNO D01 DEPTNAME DEVELOPMENT CENTER MGRNO - © Copyright IBM Corporation 2007 Figure 2-25. If the value of the expression is null.1 Notes: The NULL predicate tests for null values.Student Notebook Retrieving Rows .Null Comparison CF124. .

© Copyright IBM Corp. 2007 Unit 2.1 Notes: • The LIKE predicate searches for character strings that have a certain pattern.1 Student Notebook Uempty Partial String Search .1.0. • In the pattern the percent sign ('%') serves as place holder for a string of zero or more arbitrary characters. . Partial String Search .LIKE List the last name for all employees whose last name starts with the letter G SELECT FROM WHERE LASTNAME EMPLOYEE LASTNAME LIKE 'G%' LASTNAME GEYER GOUNOT © Copyright IBM Corporation 2007 Figure 2-26.LIKE CF124. Simple SQL Queries 2-29 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.V3. The above SELECT searches for names beginning with the letter 'G'.

Examples with '%' CF124. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Partial String Search . The second example displays all names containing 'M' and 'N' in that sequence. 1999. Names beginning with 'M' and ending with 'N' would also be displayed.Examples with '%' SELECT FROM WHERE LASTNAME EMPLOYEE LASTNAME LIKE '%SON' LASTNAME THOMPSON HENDERSON ADAMSON JEFFERSON JOHNSON SELECT FROM WHERE LASTNAME EMPLOYEE LASTNAME LIKE '%M%N%' LASTNAME THOMPSON ADAMSON MARINO © Copyright IBM Corporation 2007 Figure 2-27.1 Notes: The first example displays all names ending with 'SON'. . 2-30 DB2 SQL Workshop © Copyright IBM Corp. and will only work as illustrated if the data type of the LASTNAME column is VARCHAR.Student Notebook Partial String Search .

Simple SQL Queries 2-31 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Example with '_' CF124. 1999.1 Notes: The underscore symbol ('_') in the pattern causes the character in the corresponding position of the appropriate column to not be compared.V3.1 Student Notebook Uempty Partial String Search . To search for last names whose third character is a 'C'.Example with '_' I need a listing of all employee last names whose second character is the letter 'C' SELECT FROM WHERE LASTNAME EMPLOYEE LASTNAME LIKE '_C%' LASTNAME SCOUTTEN SCHNEIDER © Copyright IBM Corporation 2007 Figure 2-28. the pattern would have to start with two underscores.0.1. The above SELECT returns names which start with any arbitrary character if the second character is the letter 'C'. 2007 Unit 2. © Copyright IBM Corp. . Partial String Search . Each underscore in the pattern represents a single character whose value is not compared.

WHERE NOT SALARY > 2000 2-32 DB2 SQL Workshop © Copyright IBM Corp. Negation DEPTNAME SPIFFY COMPUTER SERVICE DIV. . NOT is applied before AND and AND is applied before OR. DEPTNAME DEPARTMENT DEPTNO NOT LIKE 'D%' DEPTNO A00 B01 C01 E01 E11 E21 Figure 2-29. 1999. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook Negation I need a listing of all departments except those with a department number starting with 'D' SELECT FROM WHERE DEPTNO. PLANNING INFORMATION CENTER SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT © Copyright IBM Corporation 2007 CF124. All predicates can be negated by preceding the predicate with the word NOT.1 Notes: If there are composite conditions.

Not sorted at all 3. 6. Which clauses in a SELECT statement are required? 4.1 Student Notebook Uempty Checkpoint 1. 4. 2007 Unit 2. Simple SQL Queries 2-33 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1. 1999. 2. True or False? The sequence in which clauses are coded in a SELECT statement is arbitrary. times and timestamps must always be enclosed within what? 5. what gives the underscore and the percent symbol their masking abilities? 6.0. character data. True or False? WHERE clause predicates must be separated from each other by commas.V3. Sorted in ascending order b. BETWEEN causes the rows of the result table to be a.1 Notes: Please write down your answers below: 1. Within a WHERE clause. © Copyright IBM Corp. dates. Checkpoint CF124. . © Copyright IBM Corporation 2007 Figure 2-30. 2. 5. Sorted in descending order c. 3. When doing patterned character string searches.

Unit Summary CF124.Student Notebook Unit Summary Having completed this unit. IN. . you should be able to: Describe four clauses of an SQL SELECT statement Use a SELECT statement to: – Retrieve all rows of a table – Retrieve specific columns – Retrieve rows based on comparisons to numeric or alphanumeric data – Retrieve rows based on specific columns containing NULL values Use the keywords BETWEEN.1 Notes: 2-34 DB2 SQL Workshop © Copyright IBM Corp. LIKE. 1999. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. DISTINCT Order the resulting rows in a desired sequence © Copyright IBM Corporation 2007 Figure 2-31.

you should be able to: • Retrieve data from more than one table or view. .1 Student Notebook Uempty Unit 3.1.V3. Retrieving Data from Multiple Tables What This Unit Is About How to retrieve columns from more than one table or view. • Use correlation names in queries How You Will Check Your Progress Accountability: • Checkpoint • Machine labs © Copyright IBM Corp. 2007 Unit 3. Retrieving Data from Multiple Tables 3-1 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.0. • Specify JOIN predicates. 1999. What You Should Be Able to Do After completing this unit.

.1 Notes: 3-2 DB2 SQL Workshop © Copyright IBM Corp. you should be able to: Retrieve data from more than one table or view Specify JOIN predicates Use correlation names in queries © Copyright IBM Corporation 2007 Figure 3-1. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook Unit Objectives After completing this unit. 1999. Unit Objectives CF124.

2007 Unit 3.0.1 Student Notebook Uempty 3. .V3. 1999. Retrieving Data from Multiple Tables 3-3 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Accessing Data Stored in Multiple Tables © Copyright IBM Corp.1.

.. one row exists in table DEPARTMENT. Retrieving Data from Multiple Tables (Principle) CF124. INFORMATION CENTER DEVELOPMENT CENTER ADMINISTRATION SYSTEMS .. 1999. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook Retrieving Data from Multiple Tables (Principle) PROJECT PROJNO AD3100 AD3110 AD3111 AD3112 PROJNAME ADMIN SERVICES GENERAL ADMIN SYSTEMS PAYROLL PROGRAMMING PERSONNEL PROGRAMMING DEPTNO D01 D21 D21 D21 . © Copyright IBM Corporation 2007 Figure 3-2. DEPARTMENT DEPTNO A00 C01 D01 D21 DEPTNAME SPIFFY COMPUTER SERVICE DIV... The rows are related by the values in the DEPTNO columns. By looking at the data within the two tables we can see how a row in one table is related to a row in the other table.1 Notes: For each department number in table PROJECT. 3-4 DB2 SQL Workshop © Copyright IBM Corp.

the table names in the FROM clause. list the project number.. DEPARTMENT PROJECT. PROJECT.JOIN PREDICATE PROJNO PROJNO AD3100 AD3110 AD3111 AD3112 AD3113 . 1999. PROJNAME. DEPTNO D01 D21 D21 D21 D21 . and the join predicate in the WHERE clause. Retrieving Data from Multiple Tables (JOIN) CF124. . DEPTNAME PROJECT.1 Notes: You can use the SELECT statement to produce reports that contain information from two or more tables. Furthermore. This is commonly referred as a JOIN.. specify the columns you want to be displayed in the SELECT clause. DEPTNAME DEVELOPMENT CENTER ADMINISTRATION SYSTEMS ADMINISTRATION SYSTEMS ADMINISTRATION SYSTEMS ADMINISTRATION SYSTEMS . and the number and name of the department responsible for the project. it is a good practice that all column names in a SELECT referencing more than one table be fully qualified to avoid potential error.0..DEPTNO. 2007 Unit 3.V3.. When the join predicate is omitted each qualified row from the first table is combined with all qualifying rows in the second table..DEPTNO -..DEPTNO=DEPARTMENT. PROJNAME ADMIN SERVICES GENERAL ADMIN SYSTEMS PAYROLL PROGRAMMING PERSONNEL PROGRAMMING ACCOUNT PROGRAMMING . project name. © Copyright IBM Corp. Because DEPTNO is a column of tables DEPARTMENT and PROJECT.1. Avoid a Cartesian Product! © Copyright IBM Corporation 2007 Figure 3-3.. the column name must be qualified. Retrieving Data from Multiple Tables 3-5 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Student Notebook Uempty Retrieving Data from Multiple Tables (JOIN) For every project. SELECT FROM WHERE ORDER BY PROJNO. To join two tables. This is called a Cartesian Product and is usually an unwanted result..

. D. . PROJNAME ADMIN SERVICES GENERAL ADMIN SYSTEMS PAYROLL PROGRAMMING PERSONNEL PROGRAMMING ACCOUNT PROGRAMMING . 3-6 DB2 SQL Workshop © Copyright IBM Corp.1 Notes: Correlation names are defined in the FROM clause of any query.. Here we take advantage of the fact that we specify short correlation names and we follow the recommendation to qualify all column names in our query. Correlation Names CF124. They can be used as short-hand synonyms for the table names in that query. DEPTNAME DEVELOPMENT CENTER ADMINISTRATION SYSTEMS ADMINISTRATION SYSTEMS ADMINISTRATION SYSTEMS ADMINISTRATION SYSTEMS .Student Notebook Correlation Names SELECT FROM WHERE ORDER BY P.PROJNO.PROJNAME.. 1999.DEPTNO P..DEPTNO. P. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. P.PROJNO PROJNO AD3100 AD3110 AD3111 AD3112 AD3113 . © Copyright IBM Corporation 2007 Figure 3-4. DEPARTMENT D P.....DEPTNO = D. DEPTNO D01 D21 D21 D21 D21 .DEPTNAME PROJECT P.

1 Notes: The tables EMPLOYEE and DEPARTMENT have common values for columns WORKDEPT and DEPTNO. we provide at least two JOIN conditions. LASTNAME. © Copyright IBM Corporation 2007 Figure 3-5.V3. In most cases. Further conditions are allowed and may be added by means of AND or OR. 1999. ensuring that there are no un-linked tables. Retrieving Data from Multiple Tables 3-7 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. DEPTNAME EMPLOYEE. display the employee number. The general rule of thumb is: The number of tables minus one (n-1) is USUALLY the LEAST number of join predicates needed for the query. when joining two tables we provide at least one JOIN condition. and the number and name of the department they are working in.1. JOIN Syntax 1 CF124. WORKDEPT. . © Copyright IBM Corp. DEPARTMENT WORKDEPT = DEPTNO LASTNAME = 'HAAS' EMPNO LASTNAME WORKDEPT 000010 HAAS A00 DEPTNAME SPIFFY COMPUTER SERVICE DIV.1 Student Notebook Uempty JOIN Syntax 1 For employees with a last name of HAAS. WORKDEPT contains the department number to which the employee belongs.0. 2007 Unit 3. SELECT FROM WHERE AND EMPNO. The join predicate tells DB2 UDB to combine the row for an employee with the row for the department to which the employee belongs. For three tables. last name.

WORKDEPT. LASTNAME. the join predicates must be specified in an ON clause. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. © Copyright IBM Corporation 2007 Figure 3-6. DEPTNAME EMPLOYEE JOIN DEPARTMENT WORKDEPT = DEPTNO LASTNAME = 'HAAS' EMPNO LASTNAME 000010 HAAS WORKDEPT DEPTNAME A00 SPIFFY COMPUTER SERVICE DIV. .Student Notebook JOIN Syntax 2 (JOIN Keyword) SELECT FROM ON WHERE EMPNO. 3-8 DB2 SQL Workshop © Copyright IBM Corp.1 Notes: When the JOIN keyword is used in the FROM clause. 1999. Row conditions (local predicates) must be written in a WHERE clause which must follow the ON clause. JOIN Syntax 2 (JOIN Keyword) CF124.

. 2007 Unit 3.. and the employee number and last name of the manager. for department D21. SPIFFY COMPUTER SERVICE DIV..1 Student Notebook Uempty Another JOIN Example (1 of 2) . 000010 000020 PLANNING 000030 INFORMATION CENTER -----DEVELOPMENT CENTER 000060 MANUFACTURING SYSTEMS 000070 ADMINISTRATION SYSTEMS 000050 SUPPORT SERVICES EMPLOYEE EMPNO FIRSTNME 000010 000020 000030 000050 000060 000070 000090 000100 CHRISTINE MICHAEL SALLY JOHN IRVING EVA EILEEN THEODORE MIDINIT LASTNAME I L A B F D W Q HAAS THOMPSON KWAN GEYER STERN PULASKI HENDERSON SPENSER . © Copyright IBM Corp. . Display the department name.V3. Retrieving Data from Multiple Tables 3-9 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999. Another JOIN Example (1 of 2) CF124.1 Notes: The DEPARTMENT and EMPLOYEE tables can be joined another way: the MGRNO column in the DEPARTMENT table corresponds to the employee number (EMPNO in the EMPLOYEE table) of the manager of the department. DEPARTMENT DEPTNO DEPTNAME A00 B01 C01 D01 D11 D21 E01 MGRNO . © Copyright IBM Corporation 2007 Figure 3-7..0.1. ..

Student Notebook

Another JOIN Example (2 of 2)

SELECT FROM WHERE AND

DEPTNAME, MGRNO, LASTNAME DEPARTMENT, EMPLOYEE MGRNO = EMPNO DEPTNO = ‘D21’

DEPTNAME ADMINISTRATION SYSTEMS

MGRNO 000070

LASTNAME PULASKI

© Copyright IBM Corporation 2007

Figure 3-8. Another JOIN Example (2 of 2)

CF124.1

Notes:
This shows the JOIN answering the question on the previous page. Note that we could also have used the second type of syntax: SELECT DEPTNAME, MGRNO, LASTNAME FROM DEPARTMENT JOIN EMPLOYEE ON MGRNO = EMPNO WHERE DEPTNO = ‘D21'

3-10 DB2 SQL Workshop

© Copyright IBM Corp. 1999, 2007
Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

V3.1.0.1
Student Notebook

Uempty

JOIN with Three Tables (1 of 2)
PROJECT
PROJNO AD3100 AD3110 AD3111 AD3112 AD3113 IF1000 PROJNAME ADMIN SERVICES GENERAL AD SYSTEMS PAYROLL PROGRAMMING PERSONNEL PROGRAMMING ACCOUNT. PROGRAMMING QUERY SERVICES DEPTNO . . . D01 D21 D21 D21 D21 C01

For department D21 list PROJNO, DEPTNO, DEPTNAME, MGRNO, and LASTNAME.
DEPARTMENT

DEPTNO DEPTNAME A00 B01 C01 D01 D11 D21 E01

MGRNO . . .

SPIFFY COMPUTER SERVICE DIV. 000010 000020 PLANNING 000030 INFORMATION CENTER -----DEVELOPMENT CENTER 000060 MANUFACTURING SYSTEMS 000070 ADMINISTRATION SYSTEMS 000050 SUPPORT SERVICES

EMPLOYEE EMPNO FIRSTNME
000010 000020 000030 000050 000060 000070 000090 000100 CHRISTINE MICHAEL SALLY JOHN IRVING EVA EILEEN THEODORE

MIDINIT LASTNAME
I L A B F D W Q HAAS THOMPSON KWAN GEYER STERN PULASKI HENDERSON SPENSER

...

© Copyright IBM Corporation 2007

Figure 3-9. JOIN with Three Tables (1 of 2)

CF124.1

Notes:
Here we want to find out the project number for projects belonging to department D21, as well as the department name, the employee number and last name of the department's manager. This requires joining together the PROJECT, DEPARTMENT and EMPLOYEE tables. To find the name of the department that a project is assigned to, DB2 UDB can first read the PROJECT table to obtain the department number of the department. Next, read the corresponding row of the DEPARTMENT table to obtain the department name and the employee number of the responsible manager. Next, read the corresponding row of the EMPLOYEE table to obtain the manager's last name. The DB2 UDB optimizer determines the sequence of the above steps.

© Copyright IBM Corp. 1999, 2007

Unit 3. Retrieving Data from Multiple Tables

3-11

Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Student Notebook

JOIN with Three Tables (2 of 2)
SELECT FROM PROJNO, P.DEPTNO, DEPTNAME, MGRNO, LASTNAME PROJECT P, DEPARTMENT D, EMPLOYEE E P.DEPTNO = D.DEPTNO -- join predicate D.MGRNO = E.EMPNO -- join predicate D.DEPTNO = 'D21‘ -- local predicate PROJNO

WHERE AND AND ORDER BY

PROJNO AD3110 AD3111 AD3112 AD3113

DEPTNO DEPTNAME D21 D21 D21 D21 ADMINISTRATION ADMINISTRATION ADMINISTRATION ADMINISTRATION SYSTEMS SYSTEMS SYSTEMS SYSTEMS

MGRNO LASTNAME 000070 000070 000070 000070 PULASKI PULASKI PULASKI PULASKI

© Copyright IBM Corporation 2007

Figure 3-10. JOIN with Three Tables (2 of 2)

CF124.1

Notes:
With three tables being joined together, and the join conditions being on one column, we need two join predicates (conditions) If multiple tables are specified, but no JOIN predicate and no search condition is specified, the intermediate result table consists of all possible combinations of the rows of the specified tables (the Cartesian product); the number of rows in the result is the product of the number of rows of all specified tables.

3-12 DB2 SQL Workshop

© Copyright IBM Corp. 1999, 2007
Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

the column ADMRDEPT contains the number of the department (DEPTNO) that this department reports to. PLANNING … MGRNO 000010 000020 … ADMRDEPT A00 A00 … © Copyright IBM Corporation 2007 Figure 3-11. © Copyright IBM Corp. . A00. therefore. the DEPARTMENT table can be joined to itself.0. Retrieving Data from Multiple Tables 3-13 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Student Notebook Uempty Joining a Table with Itself (1 of 3) DEPARTMENT DEPTNO DEPTNAME A00 B01 … SPIFFY COMPUTER SERVICE DIV.V3. ADMRDEPT and DEPTNO are columns in the same table. 2007 Unit 3. reports to itself). 1999. department B01 reports to department A00 (the highest department.1.1 Notes: In the DEPARTMENT table. for example. Joining a Table with Itself (1 of 3) CF124.

and one for the higher department (named SUP in the visual).the . ..Student Notebook Joining a Table with Itself (2 of 3) Display the name of department B01 and the name of. in reality. department it reports to DEPARTMENT (DEP) DEPTNO DEPTNAME MGRNO ADMRDEPT A00 B01 SPIFFY COMPUTING SERVICE DIV. DEPARTMENT (SUP) © Copyright IBM Corporation 2007 Figure 3-12.1 Notes: To answer the question on the visual. PLANNING MGRNO 000010 000020 ADMRDEPT A00 A00 . . We will code this as if we had two copies of the table: one copy for the department we are interested in – we name this copy DEP in the visual. we have to access the DEPARTMENT table twice: once for the department we are interested in (B01) and once for the department it reports to. it will join each candidate row of the table with other rows of the same table.. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. DB2 will NOT create a copy of the table. PLANNING 000010 000020 A00 A00 DEPTNO A00 B01 DEPTNAME SPIFFY COMPUTING SERVICE DIV. Of course. 3-14 DB2 SQL Workshop © Copyright IBM Corp. Joining a Table with Itself (2 of 3) CF124. 1999.

2007 Unit 3. 1999. DEPTNAME DEPARTMENT DEP. © Copyright IBM Corporation 2007 Figure 3-13.V3. Joining a Table with Itself (3 of 3) CF124.1.ADMRDEPT = SUP. Retrieving Data from Multiple Tables 3-15 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes: Table qualifiers (correlation names) are required because a table is referenced twice within the FROM clause of the query.DEPTNO DEP. .DEPTNAME.DEPTNO = ‘B01’ DEPTNAME PLANNING DEPTNAME SPIFFY COMPUTER SERVICE DIV. DEPARTMENT SUP DEP.0. © Copyright IBM Corp. SUP.1 Student Notebook Uempty Joining a Table with Itself (3 of 3) SELECT FROM WHERE AND DEP.

. . the dates of birth are compared and for each employee that is older than his/her manager information is put into the result table. . . E21 E21 1956-12-18 1941-07-18 2. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1956-12-18 1941-07-18 000100 000330 SPENSER LEE E21 E21 © Copyright IBM Corporation 2007 Figure 3-14. from the department table we find his/her manager's employee number. Obtain department number from DEPARTMENT (D) DEPTNO DEPTNAME . . . Using the manager's employee number. . BIRTHDATE . we access the EMPLOYEE table again to retrieve the manager's date of birth. MGRNO ADMRDEPT . . . . .Student Notebook Joining a Table with Itself – Another Example (1 of 2) 1. E21 . E21 SOFTWARE SUPPORT 000100 3. 000100 000330 LASTNAME SPENSER LEE WORKDEPT . LASTNAME WORKDEPT . . . Retrieve row for manager from EMPLOYEE (M) EMPNO . BIRTHDATE . . 1999. Then. 3-16 DB2 SQL Workshop © Copyright IBM Corp. . Joining a Table with Itself – Other Example (1 of 2) CF124. . Then. .1 Notes: To get the required data. Using the EMPLOYEE table. Retrieve employee's row from EMPLOYEE (E) Which employees are older than their manager? EMPNO . the EMPLOYEE table must be accessed twice. for each employee we read his/her department number. .

table qualifiers (correlation names) are required because a table is referenced twice within the FROM clause of the query.LASTNAME.V3.DEPTNO D. M. E.EMPNO EMPLOYEE E. 1999.BIRTHDATE. Retrieving Data from Multiple Tables 3-17 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes: Once again.1 Student Notebook Uempty Joining a Table with Itself – Another Example (2 of 2) Which employees are older than their manager? SELECT FROM E.WORKDEPT = D.BIRTHDATE.BIRTHDATE < M. M. 2007 Unit 3.EMPNO. © Copyright IBM Corp. E.EMPNO E. DEPARTMENT D. .MGRNO = M. EMPLOYEE M E.BIRTHDATE WHERE AND AND EMPNO 000110 000130 000200 000230 000250 000260 000280 000300 000310 000320 000330 000340 LASTNAME LUCCHESI QUINTANA BROWN JEFFERSON SMITH JOHNSON SCHNEIDER SMITH SETRIGHT MEHTA LEE GOUNOT BIRTHDATE BIRTHDATE EMPNO 1929-11-05 1925-09-15 1941-05-29 1935-05-30 1939-11-12 1936-10-05 1936-03-28 1936-10-27 1931-04-21 1932-08-11 1941-07-18 1926-05-17 1933-08-14 1941-05-11 1945-07-07 1953-05-26 1953-05-26 1953-05-26 1941-05-15 1941-05-15 1941-05-15 1956-12-18 1956-12-18 1956-12-18 000010 000030 000060 000070 000070 000070 000090 000090 000090 000100 000100 000100 © Copyright IBM Corporation 2007 Figure 3-15.0. Joining a Table with Itself – Other Example (2 of 2) CF124.1.

The statement is executed and the result is the Cartesian product of the tables. b. 1999. 2. You receive an error and the statement is not executed. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Why do we use correlation names in a SELECT? © Copyright IBM Corporation 2007 Figure 3-16.Student Notebook Checkpoint 1. Checkpoint CF124. you should use JOIN conditions to obtain the desired result.1 Notes: Please write down your answers below: 1. 3. . Which of the following situations applies if you forget the JOIN conditions in a SELECT statement using multiple tables: a. True or False? If you reference multiple tables in the FROM clause. 2. 3-18 DB2 SQL Workshop © Copyright IBM Corp. 3.

1 Notes: © Copyright IBM Corp.1 Student Notebook Uempty Unit Summary Having completed this unit. Unit Summary CF124. you should be able to: Retrieve data from more than one table or view Specify JOIN predicates Use correlation names in queries © Copyright IBM Corporation 2007 Figure 3-17.V3. 2007 Unit 3. Retrieving Data from Multiple Tables 3-19 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.1. .0.

1999.Student Notebook 3-20 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. .

What You Should Be Able to Do After completing this unit. such as salary + comm. Arithmetic allows us to work with or present calculated values. 1999. 2007 Unit 4. Scalar Functions and Arithmetic What This Unit Is About Scalar functions can be used to manipulate column or expression values.1 Student Notebook Uempty Unit 4. you should be able to: • Use arithmetic in the SELECT and WHERE clauses • Specify basic scalar functions such as COALESCE/VALUE. Date columns can also be used in arithmetic calculations. . This unit will discuss the format and syntax of basic scalar functions.1. SUBSTR • Use date and time scalar functions • Use the CONCAT operator How You Will Check Your Progress Accountability: • Checkpoint • Machine labs © Copyright IBM Corp.V3. DECIMAL. Scalar Functions and Arithmetic 4-1 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.0.

you should be able to: Use arithmetic in the SELECT and WHERE clauses Use basic scalar functions such as COALESCE/VALUE. Unit Objectives CF124. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. DECIMAL.Student Notebook Unit Objectives After completing this unit.1 Notes: 4-2 DB2 SQL Workshop © Copyright IBM Corp. SUBSTR Use date and time scalar functions Use the CONCAT operator © Copyright IBM Corporation 2007 Figure 4-1. . 1999.

1.V3. .0.1 Scalar Functions and Arithmetic © Copyright IBM Corp. 2007 Unit 4. Scalar Functions and Arithmetic 4-3 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.1 Student Notebook Uempty 4.

and SALARY + COMM for employees whose salary is less than $20000. such as SALARY+COMM. • In the result table.00 17250. multiplication and division are applied before addition and subtraction. COMM.00 1534. SALARY + COMM FROM EMPLOYEE WHERE SALARY < 20000 ORDER BY EMPNO EMPNO 000210 000250 000260 000290 000300 000310 000320 Figure 4-2.00 1272.00 19180. Selecting Calculated Values SALARY 18270.00 19950. . Operators at the same precedence level are applied from left to right.00 16567.00 17750. 1999.00 18630.00 15900.00 1596. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.00 17172.00 19170. SALARY. ordered by employee number SELECT EMPNO. SALARY. 4-4 DB2 SQL Workshop © Copyright IBM Corp.00 COMM 1462.Student Notebook Selecting Calculated Values I need a list containing EMPNO. When the order of elevation is not specified by parentheses.00 1380.1 © Copyright IBM Corporation 2007 Notes: • You can use the following arithmetic operators and parentheses() to calculate values: addition subtraction multiplication division + * / • Precedence of operations: Expressions within parentheses are evaluated first. derived columns.00 20714. do not have meaningful column names associated with them. COMM.00 21546.00 15340.00 CF124.00 1420.00 19732.00 1227.

00 20714. You can use the AS clause to rename the column headings for these derived column results.1 Student Notebook Uempty Naming Result Columns SELECT FROM WHERE ORDER BY EMPNO.00 19170. Command Editor. Scalar Functions and Arithmetic 4-5 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. COMM.00 17750.00 1420.00 INCOME 19732.1 © Copyright IBM Corporation 2007 Notes: Derived columns ordinarily have no column names or have dummy column names specified by the SQL tool used (SPUFI. and so on). SALARY + COMM AS INCOME EMPLOYEE SALARY < 20000 EMPNO EMPNO 000210 000250 000260 000290 000300 000310 000320 Figure 4-3. 2007 Unit 4. © Copyright IBM Corp.00 19180. .00 15900. SALARY.00 1596.0. 1999.00 17172.00 18630.00 1227. QMF.00 17250.00 1380.1.00 19950.00 1534.00 15340. Naming Result Columns SALARY 18270. Because the SELECT clause is executed after the WHERE clause the new column name may only be used in the ORDER BY clause.00 COMM 1462.00 CF124.00 1272.00 21546.00 16567.V3.

and so on). . every expression must be numeric.1 © Copyright IBM Corporation 2007 Notes: The COALESCE scalar function is equivalent to the VALUE scalar function. that is. expression_n.Student Notebook Substitution of NULL Values I need a listing containing department names and the employee number of its manager. MGRNO is NULL. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 'UNKNOWN') AS MANAGER FROM DEPARTMENT ORDER BY DEPTNAME DEPTNAME ADMINISTRATION SYSTEMS DEVELOPMENT CENTER INFORMATION CENTER MANUFACTURING SYSTEMS OPERATIONS PLANNING SOFTWARE SUPPORT SPIFFY COMPUTER SERVICE DIV. Substitution of NULL Values MANAGER 000070 UNKNOWN 000030 000060 000090 000020 000100 000010 000050 CF124. COALESCE (MGRNO. • In the example. • Every expression must be of a compatible data type (if the first expression is numeric. The result is null if the values of all parameters are null. SELECT DEPTNAME . 4-6 DB2 SQL Workshop © Copyright IBM Corp. the value "UNKNOWN" is returned if a department does not have a manager. 1999. expression_2. sorted by department name. SUPPORT SERVICES Figure 4-4. 'constant') • The COALESCE function takes a variable number of parameters and returns the value of the first parameter that is not null. • COALESCE allows multiple parameters: COALESCE(expression_1.

COMM. SELECT EMPNO. when commission is NULL..00 . assume unknown commissions to be zero.00 17250. 2007 Unit 4. Scalar Functions and Arithmetic 4-7 Course materials may not be reproduced in whole or in part without the prior written permission of IBM..00 1227. SALARY. COMM. the result of the expression is unknown. SALARY + COALESCE (COMM. 1999.. COMM TOTAL INCOME 1462.1 Student Notebook Uempty Arithmetic with NULL Values I need a list of the total income (salary and commission).00 . TOTAL INCOME 19732.1 Notes: If one of the columns used in the expression contains a NULL value.00 . In the total.. Arithmetic with NULL Values CF124....V3. . EMPNO 000210 000260 000290 000300 . © Copyright IBM Corp. SALARY + COMM is null. In the right example.. © Copyright IBM Corporation 2007 Figure 4-5.00 17750.00 16567. SALARY. SALARY 18270.00 15340.00 . SALARY + COMM AS "TOTAL INCOME" FROM EMPLOYEE EMPNO 000210 000260 000290 000300 . In the left example. 0) AS "TOTAL INCOME" FROM EMPLOYEE SALARY 18270..0. COMM 1462.00 .00 1227..00 17250.1..00 17750..00 15340.00 16567. for SALARY + COMM...00 17750.00 17250. nulls are replaced with zeros..00 19732. .. SELECT EMPNO.

00 17750.125000 19899.250000 18415.125000 CF124. 4-8 DB2 SQL Workshop © Copyright IBM Corp.00 18955.00 19180.625000 16496.00 19950. 1999.875000 15915. You can also use calculations with multiple columns (for example. Calculated Values SALARY 18270.0375 EMPLOYEE SALARY < 20000 EMPNO EMPNO 000210 000250 000260 000290 000300 000310 000320 Figure 4-6. COL1 * COL2). . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. SALARY.Student Notebook Calculated Values SELECT FROM WHERE ORDER BY EMPNO.00 17250.1 © Copyright IBM Corporation 2007 Notes: The above example shows multiplication using a constant.250000 17896.00 15340.250000 20698. SALARY * 1.00 15900.

1 Student Notebook Uempty Decimal Representation of a Value SELECT EMPNO. Decimal Representation of a Value SALARY 18270.0375.00 19950. Scalar Functions and Arithmetic 4-9 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. the DECIMAL scalar function is used to ensure that the result column has 2 decimal positions.00 18955.25 18415.62 16496.V3.12 CF124.00 15340.00 17750.25 17896.87 15915. 2) FROM EMPLOYEE WHERE SALARY < 20000 ORDER BY EMPNO EMPNO 000210 000250 000260 000290 000300 000310 000320 Figure 4-7.1. © Copyright IBM Corp.1 © Copyright IBM Corporation 2007 Notes: The DECIMAL scalar function returns a decimal representation of a numeric value or a decimal representation of a numeric looking character value. DECIMAL (SALARY * 1.25 20698.00 19180.00 15900. 2007 Unit 4.12 19899. 1999.00 17250. . The result is truncated after the second decimal position.0. 8. In the example above. No rounding occurs with the DECIMAL scalar function. SALARY.

Student Notebook Decimal Values .00 19180.00 15340.00 17750.88 15915.Truncation and Rounding SALARY 18270.63 16496.13 19899. Most environments support a ROUND scalar function as another method of rounding.0375 + 0.00 18955.Truncation and Rounding SELECT FROM WHERE ORDER BY EMPNO.25 20698. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.00 17250.13 CF124.00 19950. add 5 to the first decimal position to be truncated and apply the DECIMAL function to this expression.00 15900.005. 4-10 DB2 SQL Workshop © Copyright IBM Corp.1 © Copyright IBM Corporation 2007 Notes: If you need a rounded result. 8. See the SQL Reference manual for your environment for additional information.25 17896. 2) EMPLOYEE SALARY < 20000 EMPNO EMPNO 000210 000250 000260 000290 000300 000310 000320 Figure 4-8. 1999. SALARY. DECIMAL (SALARY * 1. .25 18415. Decimal Values .

(COMM/SALARY) * 100 EMPLOYEE (COMM/SALARY) * 100 > 8 EMPNO EMPNO 000140 000210 000240 000330 COMM 2274.00 1462. © Copyright IBM Corp.00 2301.002100 8.00 SALARY 28420.0.001400 8.00 18270.1.1 Student Notebook Uempty Condition on Calculated Values SELECT FROM WHERE ORDER BY EMPNO.000600 8.V3.001500 © Copyright IBM Corporation 2007 Figure 4-9. . Condition on Calculated Values CF124.00 25370. SALARY. 1999.00 8.00 28760. COMM.1 Notes: Arithmetic expressions can be used in the SELECT list and in the WHERE clause. Scalar Functions and Arithmetic 4-11 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.00 2030. The above SELECT returns rows for employees with a commission higher than 8 percent of their salary. 2007 Unit 4.

nnnnnn © Copyright IBM Corporation 2007 Figure 4-10. .ss.ss hh:mm AM hh:mm PM hh. that is. Date and Time Notes: The visual shows the internal and external formats for DATE/TIME data.mm. day.Student Notebook Date and Time DATE. TIME. A timestamp is a seven-part value (year. minute.1 8 bytes yyyy-mm-dd 10 bytes 8 bytes mm/dd/yyyy 8 bytes dd. Format ISO USA EUR JIS LOCAL Time Format hh. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.yyyy ??? ??? 8 bytes yyyy-mm-dd 10 bytes TIMESTAMP DATA: yyyy-mm-dd-hh.mm. 4-12 DB2 SQL Workshop © Copyright IBM Corp. month. without sign DataType DATE TIME TIMESTAMP Internal Format yyyymmdd hhmmss yyyymmddhhmmssnnnnnn Internal Length 4 bytes 3 bytes 10 bytes Program uses an external format. and microsecond) that represents a date and a time including microseconds. hour.ss hh:mm:ss ??? Length Date Format Length 10 bytes 10 bytes ??? 26 bytes CF124. 1999. TIMESTAMP data internally stored as packed decimal.mm. second.mm.

1.1 Student Notebook Uempty CHAR Function CHAR controls the external format of date / time data SELECT CHAR (TIMECOL. Scalar Functions and Arithmetic 4-13 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. CHAR Function CF124.00 © Copyright IBM Corporation 2007 Figure 4-11.V3. 1999.. USA). 03:30 PM 15.0. © Copyright IBM Corp.. 2007 Unit 4. ISO).30. CHAR (TIMECOL.1 Notes: The visual shows the CHAR function to convert a DATE or TIME to a chosen external character format. .

Date or time constants (character string representations of a valid date or time) must be placed in single quotes.Student Notebook Comparison with Dates SELECT FROM WHERE ORDER BY EMPNO. all employees are selected that were born '1955-01-01' or later. All legal formats can be used. 1999. In the shown example. BIRTHDATE EMPLOYEE BIRTHDATE >= '1955-01-01' BIRTHDATE EMPNO 000160 000100 LASTNAME PIANKA SPENCER BIRTHDATE 1955-04-12 1956-12-18 © Copyright IBM Corporation 2007 Figure 4-12.1 Notes: Columns defined with a DATE or TIME data type can be compared with other DATE or TIME columns or with a valid date or time. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Comparison with Dates CF124. 4-14 DB2 SQL Workshop © Copyright IBM Corp. . LASTNAME.

0. HOURS.0)) timestamp – timestamp timestamp duration (decimal (20.nnnnnn (the nnnnnn portion represents microseconds). MONTHS.6)) Labeled durations: YEARS. The result is presented in format yyyymmddhhmmss.0)) date – date date duration (decimal (8. The result is presented in format yyyymmdd. 2007 Unit 4. SECONDS. DATE / TIME Arithmetic CF124. © Copyright IBM Corp. MICROSECONDS time + labeled duration time date date + labeled duration timestamp timestamp + duration © Copyright IBM Corporation 2007 Figure 4-13.time time duration (decimal (6.V3. The result is presented in format hhmmss. A time duration represents the period between two times. A timestamp duration represents the period between two timestamps.1 Student Notebook Uempty DATE / TIME Arithmetic Subtraction only time .1. MINUTES. 1999.1 Notes: A date duration represents the period between two dates. Scalar Functions and Arithmetic 4-15 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . DAYS.

or system timestamp respectively. 720901. and CURRENT_TIMESTAMP (or CURRENT DATE. 4-16 DB2 SQL Workshop © Copyright IBM Corp. CURRENT_DATE BIRTHDATE AS DIFFER FROM EMPLOYEE WHERE CURRENT_DATE . Subtraction of Dates CF124. Leading zeros are not displayed. SELECT EMPNO. the values of two date columns are subtracted. but older versions of DB2 on some platforms do not support this format. that is. and 00 days (72 years old or more). © Copyright IBM Corporation 2007 Figure 4-14. In the WHERE clause. 781126. 00 months. 1999. CURRENT TIME and CURRENT TIMESTAMP) are special registers provided by DB2 UDB. 740021. the calculated durations are compared with 720000. For example. (The results are for CURRENT_DATE of 2005-05-12).Student Notebook Subtraction of Dates I need a listing containing the ages of all employees 72 years old or more. 72 years. The format with the underscore is more standard. . containing the system date. 750607. the value 790727 means 79 years. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. LASTNAME. CURRENT_DATE. sorted by age in descending sequence.BIRTHDATE >= 720000 ORDER BY DIFFER DESC EMPNO 000130 000050 000340 000110 000310 000320 LASTNAME QUINTANA GEYER GOUNOT LUCCHESSI SETRIGHT MEHTA DIFFER 790727. and 27 days.1 Notes: In above example. system time. in the first result line. CURRENT_TIME. 790727. 7 months. The result of the subtraction is presented in the format yyyymmdd.

YEAR. time. Scalar Functions and Arithmetic 4-17 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes: The visual shows some scalar functions for date and time values. HOUR.Converts a date to the number of days since 12/31/0000 DAYS(date_1) – DAYS(date_2) gives the number of days between date_1 and date_2 © Copyright IBM Corporation 2007 Figure 4-15.1.1 Student Notebook Uempty Date / Time Scalar Functions DAY. © Copyright IBM Corp.V3. TIME Extract portions of a date. . or duration DAYS . DATE. MICROSECOND. 1999. MINUTE. MONTH. Date / Time Scalar Functions CF124.0. timestamp. 2007 Unit 4. SECOND.

790727.1 © Copyright IBM Corporation 2007 Notes: In the above example. DAY(CURRENT_DATE . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 781126.BIRTHDATE) AS DAYS EMPLOYEE YEAR(CURRENT_DATE . MONTH(CURRENT_DATE . 4-18 DB2 SQL Workshop © Copyright IBM Corp. FIRSTNME. 740021. The AS clause is used to name the columns.BIRTHDATE) AS MONTHS. . CURRENT_DATE . year. and day are displayed separately using appropriate scalar functions. YEAR(CURRENT_DATE . YEARS 79 79 78 75 74 72 MONTHS 7 7 11 6 0 9 DAYS 27 27 26 7 21 1 CF124. 750607. The resulting columns normally do not have column headings.BIRTHDATE) AS YEARS.BIRTHDATE) >= 72 AGE DESC. 1999. Date Scalar Functions FIRSTNME JOHN DOLORES JASON VINCENZO MAUDE RAMLAL AGE 790727.Student Notebook Date Scalar Functions SELECT LASTNAME. LASTNAME FROM WHERE ORDER BY LASTNAME GEYER QUINTANA GOUNOT LUCCHESSI SETRIGHT MEHTA Figure 4-16. 720901. month.BIRTHDATE AS AGE.

DAYS(PRSTDATE) AS DAYS FROM PROJECT WHERE DAYS(PRENDATE) . Scalar Functions and Arithmetic 4-19 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.0.DAYS(PRSTDATE) <= 300 ORDER BY DAYS PROJNO PL2100 MA2113 DAYS 257 289 © Copyright IBM Corporation 2007 Figure 4-17. the project start date (PRSTDATE) must be subtracted from the project end date (PRENDATE) because the end date is higher. DAYS(PRENDATE) .1 Student Notebook Uempty DATE Arithmetic (1 of 2) SELECT PROJNO. DATE Arithmetic (1 of 2) CF124. © Copyright IBM Corp. 1999. .V3.1 Notes: The example shows the duration of projects in days. 2007 Unit 4. To obtain a positive result.1.

4-20 DB2 SQL Workshop © Copyright IBM Corp. DATE Arithmetic (2 of 2) CF124.1 Notes: The example shows the use of labeled durations and adds 2 months and 15 days to a date. . 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook DATE Arithmetic (2 of 2) SELECT FROM WHERE ORDER BY PROJNO. 1999. PRENDATE. PRENDATE + 2 MONTHS + 15 DAYS PROJECT PROJNO = 'AD3100' PROJNO PROJNO AD3100 PRENDATE 1983-02-01 1983-04-16 © Copyright IBM Corporation 2007 Figure 4-18.

Assume that COURSINF is defined CHAR(20) and that both course code and course title are stored in that column. © Copyright IBM Corp. separated by a blank. Scalar Functions and Arithmetic 4-21 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. SUBSTR(COURSINF... .1 Student Notebook Uempty Substring of Strings (1 of 2) COURSINF C F1 2 C F1 3 SQL SQL B AS I CS A DV A NCED SELECT SUBSTR(COURSINF. 15) . that is. 6. 1999.0. you can use the SUBSTR scalar function.V3.1. To only display the course title. SQL SQL B AS I CS A DV A NCED © Copyright IBM Corporation 2007 Figure 4-19. Substring of Strings CF124.1 Notes: If only a part of a character string is to be selected.15) returns a substring of length 15 beginning at position 6. the course title in the above example. use the SUBSTR scalar function to extract the part.6. 2007 Unit 4.

start with the first character and return two characters.2) extracts the first two characters.1. . 1999. Substring of Strings (Cont) CF124. The function can be interpreted to read: Within the PROJNO column.1 Notes: The first two characters of the project number represents the project class. 2) AS PROJ_CLASS. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. PROJNAME PROJECT PROJNO LIKE 'IF%' PROJ_CLASS IF IF PROJNAME QUERY SERVICES USER EDUCATION © Copyright IBM Corporation 2007 Figure 4-20. To display only the project class.Student Notebook Substring of Strings (2 of 2) SELECT FROM WHERE SUBSTR(PROJNO. 4-22 DB2 SQL Workshop © Copyright IBM Corp. the SUBSTR function is used. SUBSTR(PROJNO.1.

' || FIRSTNME FROM EMPLOYEE Check your environment's SQL documentation to see what additional concatenation symbols are supported.V3. VINCENZO O'CONNELL.1. Scalar Functions and Arithmetic 4-23 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes: The above example displays last name and first name as a single column. Example: SELECT LASTNAME || '. . Concatenation of Values CF124. 1999. ' CONCAT FIRSTNME AS NAME FROM EMPLOYEE WHERE WORKDEPT = 'A00' ORDER BY NAME NAME HAAS. Double bars (||) can be used to represent concatenation. separated by a comma and a space. 2007 Unit 4.1 Student Notebook Uempty Concatenation of Values SELECT LASTNAME CONCAT '. SEAN © Copyright IBM Corporation 2007 Figure 4-21. CHRISTINE LUCCHESSI. CONCAT combines character strings into a single column. © Copyright IBM Corp.0. but you must make sure that the vertical bar symbol on your keyboard will actually result in a vertical bar on the host. The word CONCAT works in all environments.

If you subtract two dates. 3. YYYY-MM-DD c.1 Notes: Please write down your answers below: 1.YYYY b. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. True or False? If you use the DECIMAL scalar function to have two decimal positions instead of six. 2. 1999. YYYYMMDD 3. .MM. Checkpoint CF124. DD. the result will be rounded. the format of the result will be: a. © Copyright IBM Corporation 2007 Figure 4-22. Name several scalar functions. 2. 4-24 DB2 SQL Workshop © Copyright IBM Corp.Student Notebook Checkpoint 1.

. DECIMAL. Scalar Functions and Arithmetic 4-25 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes: © Copyright IBM Corp.1 Student Notebook Uempty Unit Summary Having completed this unit. 2007 Unit 4. 1999. Unit Summary CF124.1.0.V3. SUBSTR Use date and time scalar functions Use the CONCAT operator © Copyright IBM Corporation 2007 Figure 4-23. you should be able to: Use arithmetic in the SELECT and WHERE clauses Use basic scalar functions such as COALESCE/VALUE.

. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.Student Notebook 4-26 DB2 SQL Workshop © Copyright IBM Corp.

In addition. . 2007 Unit 5. Column Functions and Grouping What This Unit Is About This unit introduces column functions which can be used to obtain results derived from a set of one or more rows. 1999.1. you should be able to: • Describe the difference between scalar and column functions • List the more common DB2 UDB column functions. What You Should Be Able to Do After completing this unit. Column Functions and Grouping 5-1 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. the syntax for selecting group results will be taught.0.1 Student Notebook Uempty Unit 5. How You Will Check Your Progress • Checkpoint questions • Machine labs © Copyright IBM Corp. • Group rows into sets based on one or more columns.V3.

. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. you should be able to: Describe the difference between scalar and column functions List the more common DB2 column functions Group rows into sets based on one or more columns © Copyright IBM Corporation 2007 Figure 5-1. Unit Objectives CF124.Student Notebook Unit Objectives After completing this unit. 1999.1 Notes: 5-2 DB2 SQL Workshop © Copyright IBM Corp.

2007 Unit 5. Column Functions and Grouping 5-3 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.V3.1 Column Functions and Grouping © Copyright IBM Corp.1 Student Notebook Uempty 5.1. .0. 1999.

1999. Scalar functions produce a value for each row within the set. Column functions produce a summary row for a set of rows. Inside the parentheses is the argument to which the function is applied. SQL Functions CF124. the name of the function followed by parentheses. 5-4 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes: There are two types of SQL functions: • Column functions (also known as summary functions) • Scalar functions The format for writing both types of functions is the same.Student Notebook SQL Functions Function_name(f_argument) Column Functions AVG(SALARY) ________ ________ ________ ______ ________ ________ Scalar Functions YEAR(HIREDATE) __________ _____ __________ _____ __________ _____ __________ _____ __________ _____ } © Copyright IBM Corporation 2007 Figure 5-2. .

147. 2007 Unit 5. COUNT_BIG(*). Column Functions SUM(expression) AVG(expression) MIN(expression) MAX(expression) COUNT(*) COUNT_BIG(*) COUNT(DISTINCT col-name) COUNT_BIG(DISTINCT colname) © Copyright IBM Corporation 2007 CF124. COUNT returns an INTEGER value (up to 2.1 Notes: For SUM. COUNT(*). .0) format. Column Functions and Grouping 5-5 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.. AVG.1. 1999.). COUNT(DISTINCT.0. COUNT_BIG(DISTINCT…). the following applies: The basis for these column functions are values derived from one or more rows. MIN. the argument for COUNT(*) or COUNT_BIG(*) is a set of rows.V3. The result is the number of rows in the set. Example for COUNT(*): SELECT COUNT(*) FROM EMPLOYEE WHERE WORKDEPT = 'A00' returns the number of rows having the value 'A00' in the WORKDEPT column. then we can use COUNT_BIG which returns a value in DECIMAL(31.1 Student Notebook Uempty Column Functions Compute Total Calculate Average Find Minimum Value Find Maximum Value Determine the number of rows meeting the search condition Determine the number of unique non-null values in a column Figure 5-3.. In general. © Copyright IBM Corp. The data type of the input values for SUM and AVG must be numeric.647). MAX. if a larger result is possible (there are tables with billions of rows).483.

1999. . 5-6 DB2 SQL Workshop © Copyright IBM Corp.Student Notebook Example for COUNT(DISTINCT column-name): SELECT COUNT(DISTINCT WORKDEPT) FROM EMPLOYEE returns the number of unique non-null values in the WORKDEPT column. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. The argument of this function can also be an expression.

59375000 MIN 15340. SALARY + COMM) may be used as an argument for column functions.V3. Column Functions and Grouping 5-7 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.00 COUNT DEPT 32 8 © Copyright IBM Corporation 2007 Figure 5-4.1. COUNT(*) AS COUNT. expressions (for example. Sample Column Functions CF124. COUNT (DISTINCT WORKDEPT) AS DEPT EMPLOYEE FROM SUM 873715. Except for COUNT(*) and COUNT_BIG(*). Precision and scale of the resulting values are derived from the definition for the column to which the column function applies. AVG(SALARY) AS AVG. The result does not display detailed information for individual rows. MIN(SALARY ) AS MIN. © Copyright IBM Corp.0.1 Student Notebook Uempty Sample Column Functions SELECT SUM(SALARY) AS SUM.00 AVG 27303. .1 Notes: The set of rows to which these column functions are applied contains all rows of the employee table.00 MAX 52750. MAX(SALARY) AS MAX. 2007 Unit 5.

Consider using the COALESCE/VALUE scalar function to replace nulls with known values. Beware of Nulls CF124. 1999. Nulls are ignored in column level arithmetic. 5-8 DB2 SQL Workshop © Copyright IBM Corp. .Student Notebook Beware of Nulls SUM(SALARY)+SUM(BONUS)+SUM(COMM) VERSUS SUM(SALARY+BONUS+COMM) EMPNO 000010 000020 000030 000040 -----Sum: SALARY 1000 2000 2500 1500 -----7000 ===== BONUS 500 NULL 400 100 ----1000 ===== COMM 100 300 NULL 400 ---800 ==== SALARY+BONUS+COMM 1600 NULL NULL 2000 ------------------> 8800 3600 ==== © Copyright IBM Corporation 2007 Figure 5-5. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes: If a null is part of row level arithmetic the result is null.

1. .1 Notes: The result is derived from the set of rows for which the WHERE clause predicate is true. 1999.00 © Copyright IBM Corporation 2007 Figure 5-6.V3. 2007 Unit 5.1 Student Notebook Uempty Column Functions Based on Subset I need the sum of all salaries for workdepts beginning with the letter D SELECT FROM WHERE SUM(SALARY) AS SUM EMPLOYEE WORKDEPT LIKE 'D%' SUM 373020. Column Functions Based on Subset CF124. © Copyright IBM Corp.0. Column Functions and Grouping 5-9 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

I want the totals spent for salaries.00 23800. having the same value for WORKDEPT). In addition.00 29250. 1999. 'C01') WORKDEPT SELECT WORKDEPT.00 © Copyright IBM Corporation 2007 WORKDEPT A00 B01 C01 SUM 128500.00 46500. A name defined by an AS clause cannot be used in GROUP BY. 5-10 DB2 SQL Workshop © Copyright IBM Corp. SUM(SALARY) AS SUM FROM EMPLOYEE WHERE WORKDEPT IN ('A00'. for these departments. 'C01') GROUP BY WORKDEPT ORDER BY WORKDEPT WORKDEPT A00 A00 A00 B01 C01 C01 C01 Figure 5-7. SELECT FROM WHERE ORDER BY WORKDEPT. SALARY EMPLOYEE WORKDEPT IN ('A00'.Student Notebook GROUP BY I need a listing of the salaries for all employees in the departments A00. a column function results in a single value for each group. 'B01'.1 Notes: With GROUP BY. If an ordered result table is required an appropriate ORDER BY clause must be specified. B01. 'B01'.00 90470.00 CF124.00 28420. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. GROUP BY does not guarantee a sorted result table. . A group is made up of all the candidate rows (rows satisfying the WHERE clause) having the same value for all the columns in the GROUP BY (in this example. GROUP BY SALARY 52750. and C01.00 41250.00 41250.00 38250.

V3.1. Column Functions and Grouping 5-11 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.00 46500.2) AS AVGSAL FROM EMPLOYEE WHERE EDLEVEL >= 18 GROUP BY SUBSTR(WORKDEPT.00 29840. GROUP BY More than One Column CF124.1. 1999.0. © Copyright IBM Corp.00 28420.1 Student Notebook Uempty GROUP BY more than one Column Find out the average salary per education level for each department group (given by the first character of the department number).1). 2007 Unit 5. for education levels 18 and higher SELECT SUBSTR(WORKDEPT. EDLEVEL DEPT_GROUP A A B C C D EDLEVEL 18 19 18 18 20 18 AVGSAL 52750. .00 © Copyright IBM Corporation 2007 Figure 5-8.00 38250. EDLEVEL. DECIMAL(AVG(SALARY).00 41250.1) AS DEPT_GROUP.9.1.1 Notes: Here we are grouping by the first character of the department number and education level.

2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. columns listed in the GROUP BY clause don't have to have been selected.Student Notebook The Hardest to Remember Rule in All of SQL! Only applies to queries with COLUMN FUNCTIONS in the SELECT Clause If a SELECT clause has COLUMN functions AND columns not in COLUMN functions ALL columns not in COLUMN functions must be included in the GROUP BY clause © Copyright IBM Corporation 2007 Figure 5-9. . The Hardest to Remember Rule in All of SQL! CF124. 1999.1 Notes: Column functions are: • MIN • MAX • SUM • AVG • COUNT(*) • COUNT_BIG(*) • COUNT(DISTINCT column_name) • COUNT_BIG(DISTINCT column_name) The reverse of the rule is not true. that is. 5-12 DB2 SQL Workshop © Copyright IBM Corp.

5000000000000009000000000000 1.0000000000000000000000000000 5.0. DEPTNO. 1999.1.1 Student Notebook Uempty GROUP BY. DEPTNO ORDER BY MAJPROJ DESC MAJPROJ OP2010 OP2000 OP1000 MA2110 MA2100 MA2100 AD3110 AD3100 DEPTNO C01 D01 E01 E21 E21 E11 D11 B01 D11 D21 D21 AVG(PRSTAFF) 1.1 Notes: The requirement is. Note that nulls within the scope of the grouping are combined: the first report line can be interpreted to read: Department C01 has two projects that have an unknown major project. Column Functions and Grouping 5-13 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.0000000000000000000000000000 © Copyright IBM Corporation 2007 COUNT(*) 2 2 2 3 1 1 3 1 1 3 1 Figure 5-10.0000000000000000000000000000 8. Note also that nulls are sorted as high values (ORDER BY MAJPROJ DESC).2500000000000000000000000000 5. GROUP BY. the average number of staff assigned and the number of projects that are part of the major project. DEPTNO.6666666666666661000000000000 1. ORDER BY CF124.5000000000000000000000000000 9. Looking at the fourth line in the report. 2007 Unit 5.V3.0000000000000000000000000000 4. from the PROJECT table. © Copyright IBM Corp.6666666666666663000000000000 6. Our hard to remember rule tells us that when we have a column function in the select list we must GROUP BY all columns in the select list that are not arguments of a column function. . COUNT(*) AS "COUNT(*)" FROM PROJECT GROUP BY MAJPROJ. produce a list that includes MAJPROJ.0000000000000000000000000000 2. it can be interpreted to read: Department E21 has 3 subprojects to major project OP2010 and their average staffing is 1. ORDER BY SELECT MAJPROJ. AVG(PRSTAFF) AS "AVG(PRSTAFF)".9999999999999982000000000000 1.

00 Figure 5-11.00 © Copyright IBM Corporation 2007 WORKDEPT A00 C01 SUM 128500. 5-14 DB2 SQL Workshop © Copyright IBM Corp. 'C01') GROUP BY WORKDEPT ORDER BY WORKDEPT SELECT WORKDEPT. 1999.00 41250.00 90470. GROUP BY. HAVING (1 of 2) Now.Student Notebook GROUP BY. 'C01') GROUP BY WORKDEPT HAVING SUM(SALARY) > 50000 ORDER BY WORKDEPT WORKDEPT A00 B01 C01 SUM 128500. HAVING is used to qualify or disqualify a group from further processing. HAVING (1 of 2) CF124. SUM(SALARY) AS SUM FROM EMPLOYEE WHERE WORKDEPT IN ('A00'. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . This column need not be in the SELECT list.1 Notes: So far we built groups. With HAVING. 'B01'. I just want to see departments with total spent for salaries higher than 50000 SELECT WORKDEPT. SUM(SALARY) AS SUM FROM EMPLOYEE WHERE WORKDEPT IN ('A00'. 'B01'. Conditions for groups must be specified in the having clause.00 90470. HAVING may specify any column function on any column in a table being queried. we now can select groups satisfying certain conditions.

excluding managers. GROUP BY. 2007 Unit 5. . HAVING (2 of 2) CF124.00000000 26110. designer. and another example of HAVING. but different values in the JOB column.1 Notes: In the above example.000. JOB.0. we see another example of a GROUP BY clause with two columns. © Copyright IBM Corp.1 Student Notebook Uempty GROUP BY. Column Functions and Grouping 5-15 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. The first three rows in the result have the same value ('A00') in the WORKDEPT column. with an average salary higher than $25. SELECT FROM WHERE GROUP BY HAVING ORDER BY WORKDEPT. The results of a GROUP BY clause is one result row for each unique set of values across the columns being grouped. 'DESIGNER'. 1999. AVG(SALARY) AS AVG EMPLOYEE JOB NOT IN ('MANAGER'. JOB AVG(SALARY) > 25000 WORKDEPT.00000000 52750.V3. I need a listing of jobs. JOB WORKDEPT JOB A00 A00 A00 C01 CLERK PRES SALESREP ANALYST AVG 29250. and field representative.00000000 © Copyright IBM Corporation 2007 Figure 5-12.00000000 46500. 'FIELDREP') WORKDEPT. HAVING (2 of 2) By department.1.

WORKDEPT SELECT FROM GROUP BY HAVING ORDER BY WORKDEPT. The condition COUNT(*) > 1 applies to the result table of the left SELECT 5-16 DB2 SQL Workshop © Copyright IBM Corp. WORKDEPT WORKDEPT B01 E01 A00 C01 E21 E11 D21 D11 Figure 5-13. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . Examples with HAVING NUMB 1 1 3 3 4 5 6 9 © Copyright IBM Corporation 2007 WORKDEPT A00 C01 E21 E11 D21 D11 NUMB 3 3 4 5 6 9 CF124. The right SELECT determines the result table consisting of groups for which the search condition of the HAVING clause is true.Student Notebook Examples with HAVING Display the departments with more than one employee SELECT FROM GROUP BY ORDER BY WORKDEPT. COUNT(* ) AS NUMB EMPLOYEE WORKDEPT NUMB. COUNT(* ) AS NUMB EMPLOYEE WORKDEPT COUNT(*) > 1 NUMB.1 Notes: The left SELECT is displayed to show the result without a HAVING clause. 1999.

1 Student Notebook Uempty Restrictions Column functions may be specified only in – SELECT – HAVING SELECT may specify only – Column functions – Columns specified in 'GROUP BY' HAVING may specify – Any column function on any column in a table being queried. Restrictions CF124. This column need not be in the SELECT.V3. . Column functions may not be nested © Copyright IBM Corporation 2007 Figure 5-14.1. Column Functions and Grouping 5-17 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.1 Notes: © Copyright IBM Corp.0. 2007 Unit 5.

2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . SELECT Statement . summarized) based upon values in a column(s).Six Clauses SELECT DEP. The WHERE clause specifies which row(s) of information are to be used based upon some qualification criteria.Six Clauses CF124. The GROUP BY clause tells DB2 UDB which row(s) of information are to be grouped together (that is. Column functions tell DB2 UDB to produce a single summary row for a set of detail rows. The HAVING clause tells DB2 UDB which group(s) of information are to be processed based upon a group qualification criteria.Student Notebook SELECT Statement . The ORDER BY clause specifies sequence of the result rows in the final result table. JOB HAVING AVG(SAL) > 28000 ORDER BY 3 DESC © Copyright IBM Corporation 2007 Figure 5-15. 5-18 DB2 SQL Workshop © Copyright IBM Corp.1 Notes: The SELECT clause tells DB2 UDB which column(s) of information we want on our report. JOB. The FROM clause tells DB2 UDB which table(s) contain the requested data. 1999. The SELECT list contains a column function (also known as a summary function) AVG. AVG(SAL) FROM EMPL WHERE JOB <> 'M' GROUP BY DEP.

V3.1.0.1
Student Notebook

Uempty

Conceptual Execution of a SELECT (1 of 2)
BASE TABLE
JOB SAL S 31000 M 32000 S 30000 C 27000 S 33000 M 31000 S 32000 C 28000 S 30000 M 33000 S 31000 S 35000 C 27000 S 29000 S 29000 DEP BLU RED BLU GRE GRE BLU RED GRE RED GRE RED GRE BLU RED BLU

WHERE
JOB SAL DEP S 31000 BLU S C S S C S S S C S S 30000 BLU 27000 GRE 33000 GRE 32000 RED 28000 GRE 30000 RED 31000 35000 27000 29000 29000 RED GRE BLU RED BLU

GROUP BY
JOB SAL DEP C 27000 BLU S S S C C S S S S S S 31000 BLU 29000 BLU 30000 BLU 27000 GRE 28000 GRE 33000 GRE 35000 GRE 32000 30000 31000 29000 RED RED RED RED

© Copyright IBM Corporation 2007

Figure 5-16. Conceptual Execution of a SELECT (1 of 2)

CF124.1

Notes:
The clauses are executed in this sequence: • FROM • WHERE • GROUP BY Through the FROM clause DB2 UDB locates the table. The WHERE clause extracts into the conceptual intermediate table rows that will be processed further. The GROUP BY clause sorts the rows in the intermediate table into subsets.

© Copyright IBM Corp. 1999, 2007

Unit 5. Column Functions and Grouping

5-19

Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Student Notebook

Conceptual Execution of a SELECT (2 of 2)
HAVING
JOB SAL DEP DEP

SELECT
JOB AVG(SAL) DEP GRE RED BLU

ORDER BY
JOB S S S AVG(SAL) 34000 30500 30000

S S S

31000 BLU 29000 BLU 30000 BLU

BLU

S

30000

S S S S S S

33000 GRE 35000 GRE 32000 30000 31000 29000 RED RED RED RED

GRE

S

34000

RED

S

30500

© Copyright IBM Corporation 2007

Figure 5-17. Conceptual Execution of a SELECT (2 of 2)

CF124.1

Notes:
Only those groups passing the HAVING clause test are processed further.

5-20 DB2 SQL Workshop

© Copyright IBM Corp. 1999, 2007
Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

V3.1.0.1
Student Notebook

Uempty

Checkpoint
1. True or False? A scalar function produces a summary row for a set of rows. 2. A SELECT statement whose SELECT list includes a column function (SUM, AVG, MIN, MAX, COUNT, and so forth) and three columns not in column functions does not require a GROUP BY clause. 3. Which clause qualifies groups for further processing?
a. b. c. d. e. f. SELECT FROM WHERE GROUP BY HAVING ORDER BY
SELECT WORKDEPT, AVG(SALARY) FROM EMPLOYEE WHERE AVG(SALARY) > 20000 GROUP BY WORKDEPT HAVING COUNT(*) > 3 ORDER BY 2 DESC
© Copyright IBM Corporation 2007

4. True or False? The following query is syntactically correct.

Figure 5-18. Checkpoint

CF124.1

Notes:
Please write down your answers below: 1. 2. 3. 4.

© Copyright IBM Corp. 1999, 2007

Unit 5. Column Functions and Grouping

5-21

Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

you should be able to: Describe the difference between scalar and column functions List the more common DB2 column functions Group rows into sets based on one or more columns © Copyright IBM Corporation 2007 Figure 5-19. 1999.Student Notebook Unit Summary Having completed this unit.1 Notes: 5-22 DB2 SQL Workshop © Copyright IBM Corp. Unit Summary CF124. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. .

1 Student Notebook Uempty Unit 6. 1999. UNION What This Unit Is About This unit describes the syntax of an SQL statement that will produce a result table containing the results of two or more SELECT statements. UNION 6-1 .0. you should be able to: • Produce a single result table containing data from more than one query • State the UNION rules • State the difference between UNION and UNION ALL How You Will Check Your Progress Accountability: • Checkpoint • Machine labs © Copyright IBM Corp.1. Unit 6. What You Should Be Able to Do After completing this unit.V3. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

. Unit Objectives CF124. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook Unit Objectives After completing this unit.1 Notes: 6-2 DB2 SQL Workshop © Copyright IBM Corp. 1999. you should be able to: Produce a single result table containing data from more than one query State the UNION rules State the difference between UNION and UNION ALL © Copyright IBM Corporation 2007 Figure 6-1.

1 UNION © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.0. Unit 6. 1999.V3.1 Student Notebook Uempty 6.1. UNION 6-3 .

Multiple Queries . 6-4 DB2 SQL Workshop © Copyright IBM Corp. Sometimes we have a requirement for data returned by one query to appear in a report with data from additional queries.1 Notes: Each time a query is executed a single report is produced. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Multiple Reports QUERY 1 SELECT some_columns FROM some_table(s) WHERE some_conditions QUERY 2 SELECT some_columns FROM some_table(s) WHERE some_conditions DATA FROM QUERY 1 DATA FROM QUERY 2 © Copyright IBM Corporation 2007 Figure 6-2. .Multiple Reports CF124. 1999.Student Notebook Multiple Queries .

UNION 6-5 .1.0. 1999.1 Notes: By following the UNION RULES. UNIONing Queries Together .1 Student Notebook Uempty UNIONing Queries Together .Single Report -.QUERY 1 SELECT some_columns FROM some_table(s) WHERE some_conditions UNION ALL DATA FROM QUERY 2 -. © Copyright IBM Corp.V3. and adding a UNION ALL clause between the queries the data from both queries appears in a single report.QUERY 2 SELECT some_columns FROM some_table(s) WHERE some_conditions DATA FROM QUERY 1 © Copyright IBM Corporation 2007 Figure 6-3. Unit 6.Single Report CF124. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

1 Notes: If the first query in the stack returns x number of columns every query in the stack must return x number of columns.Student Notebook Union Rule Number 1 SELECT COL_6. COL_Y. . COL_8. COL_P FROM TEST_TAB_B WHERE COL_R < 7 Every query in the stack must return the same number of columns © Copyright IBM Corporation 2007 Figure 6-4. Union Rule Number 1 CF124. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. COL_A. COL_3. 1999. 6-6 DB2 SQL Workshop © Copyright IBM Corp. COL_14 FROM TEST_TAB_A WHERE COL_7 = 'Y' UNION ALL SELECT COL_D.

2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Column lengths do not have to match. COL_Y. COL_P FROM TEST_TAB_B WHERE COL_R < 7 The data types of the nth column of each query in the stack must be compatible © Copyright IBM Corporation 2007 Figure 6-5.1 Notes: If the nth column in the first query returns character data then the corresponding nth column in every query of the stack must return character data.2) data can appear in the same column).1 Student Notebook Uempty Union Rule Number 2 SELECT COL_6.V3. Numeric precision and scale do not have to match (integer and DEC(7. COL_14 FROM TEST_TAB_A WHERE COL_7 = 'Y' UNION ALL SELECT COL_D. COL_8. COL_A. © Copyright IBM Corp. Union Rule Number 2 CF124.1. UNION 6-7 . The report column will carry the largest precision and scale of all numbers in the column.0. Unit 6. 1999. COL_3.

COL_3. COL_A AS C3 FROM TEST_TAB_B WHERE COL_R < 7 ORDER BY C3 DESC. Union Rule Number 3 CF124. COL_8 AS C3 FROM TEST_TAB_A WHERE COL_7 = 'Y' UNION ALL SELECT COL_D.Student Notebook Union Rule Number 3 SELECT COL_6. . COL_Y. 1999. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes: Only one ORDER BY clause allowed on a UNIONed query. 2 The ORDER BY clause must be the last clause on the last query in the stack © Copyright IBM Corporation 2007 Figure 6-6. 6-8 DB2 SQL Workshop © Copyright IBM Corp.

HEATHER GENDER FEMALE MALE MALE FEMALE FEMALE FEMALE © Copyright IBM Corporation 2007 Figure 6-7. LASTNAME || '.1 Notes: The above example is one of several ways of translating coded data into more human terms (the value of M replaced with the word MALE. VINCENZO O'CONNELL. ' || FIRSTNME AS NAME. 'FEMALE' AS GENDER FROM EMPLOYEE WHERE SEX = 'F' AND WORKDEPT IN ('A00'.1 Student Notebook Uempty UNION ALL . last name concatenated to first name and in a GENDER column print MALE or FEMALE where appropriate SELECT WORKDEPT. the value of F replaced with the word FEMALE. DOLORES NICHOLLS.1. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Example 1 CF124. 'C01') ORDER BY WORKDEPT WORKDEPT A00 A00 A00 C01 C01 C01 NAME HAAS. LASTNAME || '. ' || FIRSTNME AS NAME. 'C01') UNION ALL SELECT WORKDEPT. CHRISTINE LUCCHESSI.V3. Unit 6. UNION 6-9 . SEAN KWAN.0. 1999.Example 1 For workdepts C01 and A00 display workdept. © Copyright IBM Corp. UNION ALL . 'MALE' AS GENDER FROM EMPLOYEE WHERE SEX = 'M' AND WORKDEPT IN ('A00'. SALLY QUINTANA.

MGRNO = E. Otherwise. .2 DESC MGRNO 000010 000010 000020 000020 000030 000030 000050 000050 Figure 6-8. If a column you wish to sort by has the same name in each query in the stack. the column's position number must be used. LASTNAME AS NAME FROM DEPARTMENT D.: Mgr.:'. MGRNO . EMPLOYEE E WHERE D.: Mgr. 'Mgr. 6-10 DB2 SQL Workshop © Copyright IBM Corp. DEPTNAME AS NAME FROM DEPARTMENT WHERE MGRNO IS NOT NULL UNION ALL SELECT MGRNO. THOMPSON PLANNING KWAN INFORMATION CENTER GEYER SUPPORT SERVICES CF124.:'.Example 2 Mgr.Example 2 SELECT As sketched out below.: Dept.1 © Copyright IBM Corporation 2007 Notes: The first SELECT establishes the column headings of the result.: Dept.: NAME HAAS SPIFFY COMPUTER SERVICE DIV. on line two print department information.: Dept. then the column name can be used in the ORDER BY clause. 1999. use two lines per department. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. On line one print manager's information.: Dept.EMPNO ORDER BY 1. 'Dept. UNION ALL .: Mgr.Student Notebook UNION ALL .

1.Example 3 CF124. EDLEVEL EMPLOYEE JOB = 'ANALYST' LASTNAME QUINTANA NICHOLLS EDLEVEL 16 18 SELECT FROM WHERE LASTNAME. EDLEVEL EMPLOYEE EDLEVEL = 18 LASTNAME HAAS THOMPSON NICHOLLS LUTZ EDLEVEL 18 18 18 18 SELECT FROM WHERE UNION ALL SELECT FROM WHERE LASTNAME.0.1 Notes: UNION ALL does not eliminate duplicates. EDLEVEL EMPLOYEE EDLEVEL = 18 © Copyright IBM Corporation 2007 LASTNAME QUINTANA NICHOLLS HAAS THOMPSON NICHOLLS LUTZ EDLEVEL 16 18 18 18 18 18 Figure 6-9. 1999. © Copyright IBM Corp.Example 3 SELECT FROM WHERE LASTNAME. UNION 6-11 .V3. Use UNION ALL if the queries cannot return duplicate rows. Unit 6. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. EDLEVEL EMPLOYEE JOB = 'ANALYST' LASTNAME.1 Student Notebook Uempty UNION ALL . UNION ALL .

EDLEVEL EMPLOYEE EDLEVEL = 18 LASTNAME HAAS LUTZ NICHOLLS THOMPSON EDLEVEL 18 18 18 18 SELECT FROM WHERE UNION SELECT FROM WHERE LASTNAME. The results are merged and ordered 3.1 Notes: UNION combines two sets of rows and removes duplicates. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. EDLEVEL EMPLOYEE JOB = 'ANALYST' LASTNAME NICHOLLS QUINTANA EDLEVEL 18 16 SELECT FROM WHERE LASTNAME. UNION ALL often performs better than UNION. The problem to list last name and education level of employees who are analysts or have an education level of 18 can be solved by means of a UNION performing the following three steps: 1. Duplicates are removed 6-12 DB2 SQL Workshop © Copyright IBM Corp. Each SELECT is evaluated separately 2. EDLEVEL EMPLOYEE JOB = 'ANALYST' LASTNAME. Therefore. 1999.Student Notebook UNION SELECT FROM WHERE LASTNAME. EDLEVEL EMPLOYEE EDLEVEL = 18 © Copyright IBM Corporation 2007 LASTNAME HAAS LUTZ NICHOLLS QUINTANA THOMPSON EDLEVEL 18 18 18 16 18 Figure 6-10. . UNION CF124.

3rd commission. SUBSTR(FIRSTNME.V3. 1999.' || MIDINIT. LASTNAME. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Numeric literals. CF124.0. M.I HAAS INCOME 52750. 2 AS SORT EMPLOYEE ALL EMPNO. In the above example. 1 AS SORT EMPLOYEE ALL EMPNO. ' '. To get the equivalent number of columns in the other SELECT statements. SALARY AS INCOME.00 . SORT 1 2 3 4 1 2 3 4 . BONUS. Unit 6. © Copyright IBM Corporation 2007 Figure 6-11.1 Student Notebook Uempty UNION .00 45350. 1) || '. 4 AS SORT EMPLOYEE EMPNO. 1.00 4220. a substring of first name concatenated with the MIDINIT and the last name are selected in the first SELECT.00 800. we need an additional sort column.00 57970. All column and scalar functions can be used.00 3300.Generation of Fitting Result Rows SELECT FROM UNION SELECT FROM UNION SELECT FROM UNION SELECT EMPNO. blanks (compatible to the data types of the concatenated value and the last name value) are specified as corresponding column values. current date or current time can be 'place holders' at the appropriate spots in UNIONed queries. UNION 6-13 . UNION .00 1000.1 FROM ORDER BY EMPNO 000010 000010 000010 000010 000020 000020 000020 000020 . and double bars (||) (in environments where they are supported). SUM: . To get a four row result for each employee in the desired order (1st salary.00 41250.Generation of Fitting Result Rows Notes: Any type of SELECT statement can be used as long as the rules for UNION are adhered to. 2nd bonus.L SUM: THOMPSON . 4th sum). The double exclamation points (!!).1. EMPNO must be selected to order the result table. ' '. 3 AS SORT EMPLOYEE ALL EMPNO. ' '. ' '. SORT LASTNAME C. 'SUM: '. COMM. are equivalent to the CONCAT operator (which is supported in all © Copyright IBM Corp. ' '. SALARY + BONUS + COMM.

6-14 DB2 SQL Workshop © Copyright IBM Corp.Student Notebook environments). 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Check the manuals for your environment to determine its supported operator concatenation operators. 1999. .

EXCEPT displays what is in the first result set but not in the second result set. They are also used for merging data. it displays ANALYSTS who are not edlevel 18. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. This would show duplicates if there were any. EDLEVEL EMPLOYEE EDLEVEL = 18 LASTNAME QUINTANA EDLEVEL 16 LASTNAME. INTERSECT ALL would not. In this case. but work differently.V3. EXCEPT and INTERSECT CF124. © Copyright IBM Corp.1 Notes: Here are two other sets of words that can be used in place of UNION ALL and UNION. EXCEPT ALL would not. EDLEVEL SELECT FROM EMPLOYEE WHERE EDLEVEL = 18 LASTNAME NICHOLLS EDLEVEL 18 © Copyright IBM Corporation 2007 Figure 6-12. 1999. This would show duplicates if there were any.0. it displays ONLY ANALYSTS who are ALSO edlevel 18.1 Student Notebook Uempty EXCEPT and INTERSECT SELECT FROM WHERE EXCEPT SELECT FROM WHERE LASTNAME. UNION 6-15 . EDLEVEL SELECT EMPLOYEE FROM JOB = 'ANALYST' WHERE INTERSECT LASTNAME. Unit 6. EDLEVEL EMPLOYEE JOB = 'ANALYST' LASTNAME. In this case.1. INTERSECT displays only what is in both the first result set and in the second result set.

When all duplicate rows have to be eliminated. 3. b.Student Notebook Checkpoint 1. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Checkpoint CF124. Why or when should you use UNION? a. 6-16 DB2 SQL Workshop © Copyright IBM Corp. 1999. When SELECTs with an arbitrary number of columns should be combined. 2. 2. Which rules do you have to consider when using UNION? © Copyright IBM Corporation 2007 Figure 6-13.1 Notes: Please write down your answers below: 1. 3. . True or False? The results of arbitrary SELECTs can be combined by means of UNION and ordered to get a new result.

0. you should be able to: Produce a single result table containing data from more than one query State the UNION rules State the difference between UNION and UNION ALL © Copyright IBM Corporation 2007 Figure 6-14.1 Student Notebook Uempty Unit Summary Having completed this unit. Unit 6.1 Notes: © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.V3. Unit Summary CF124. 1999.1. UNION 6-17 .

2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.Student Notebook 6-18 DB2 SQL Workshop © Copyright IBM Corp. .

V3. Using Subqueries 7-1 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. What You Should Be Able to Do After completing this unit. you should be able to: • Use subqueries in WHERE and HAVING clauses • Code subqueries using basic predicates • Code subqueries using the IN keyword How You Will Check Your Progress Accountability: • Checkpoint • Machine labs © Copyright IBM Corp.1 Student Notebook Uempty Unit 7. Using Subqueries What This Unit Is About This unit provides information on how to use subqueries.1. 2007 Unit 7.0. . 1999.

Unit Objectives CF124. you should be able to: Use subqueries in WHERE and HAVING clauses Code subqueries using basic predicates Code subqueries using IN © Copyright IBM Corporation 2007 Figure 7-15. . The inner query is referred to as a subquery. 1999. 7-2 DB2 SQL Workshop © Copyright IBM Corp.Student Notebook Unit Objectives After completing this unit. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1 Notes: It is often useful to imbed a query within another. This enables the inner query to provide an answer to a question for use in the outer query.

0.V3.1 Student Notebook Uempty 7. . Using Subqueries 7-3 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 2007 Unit 7.1 Using Subqueries © Copyright IBM Corp. 1999.1.

2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. you would have to do the following: • Evaluate the average salary of all employees (first select) • Code a second query using the result (27303) in the WHERE clause.1 © Copyright IBM Corporation 2007 Figure 7-16.Student Notebook Result Using Separate Selects Whose salary is higher than the average salary? First Select: SELECT FROM AVG(SALARY) EMPLOYEE 27303 Second Select: SELECT FROM WHERE EMPNO. 1999. LASTNAME EMPLOYEE SALARY > 27303 CF124. . Without subqueries. • Run the second query to find the employees whose salary is higher than the average salary (27303). Result Using Separate Selects Notes: Assume that you want to identify all employees whose salary is higher than the average salary of all employees. 7-4 DB2 SQL Workshop © Copyright IBM Corp.

1999. and the result is used to evaluate the WHERE condition of the main query. © Copyright IBM Corp. 2007 Unit 7.1. LASTNAME EMPLOYEE SALARY > (SELECT AVG(SALARY) FROM EMPLOYEE) © Copyright IBM Corporation 2007 Figure 7-17. described on the previous visual can be solved by means of a single execution of a query containing a subquery. The subquery runs first. The inner SELECT is referred to as the subquery.1 Student Notebook Uempty Same Result Using a Subquery SELECT FROM WHERE EMPNO. Using Subqueries 7-5 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. A subquery cannot contain an ORDER BY.1 Notes: The problem. As a consequence. We can code the first SELECT of the previous visual in the WHERE condition of the second SELECT of the previous visual at the place where the average is needed.V3. The results of the subquery are passed to the outer query and are not visible to the user. the second SELECT is executed by the outer(main) query. . Same Result Using a Subquery CF124.0.

LASTNAME. or any combination thereof). Subquery with Basic Predicates LASTNAME PARKER BONUS 300 CF124. the result of the predicate is true and the row is returned. BONUS FROM EMPLOYEE FROM EMPLOYEE) WHERE BONUS = (SELECT MIN (BONUS) EMPNO 000290 Figure 7-18. If the bonus of an employee matches the value provided by the subquery. we look for the employee(s) who earned the lowest bonus. A basic predicate is one that uses single value comparison operator (=.Student Notebook Subquery with Basic Predicates Who earned the lowest bonus? EMPNO 000010 000070 000290 LASTNAME HAAS PULASKI PARKER BONUS 1000 700 300 SELECT EMPNO. >. 1999. <. The subquery will determine the lowest bonus and it will be compared with the bonuses of all employees. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 7-6 DB2 SQL Workshop © Copyright IBM Corp.1 © Copyright IBM Corporation 2007 Notes: In this example. . A subquery in a basic predicate must not return more than one value.

IN means that for the condition to evaluate TRUE. Using Subqueries 7-7 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999. © Copyright IBM Corp. .1. the value in the first operand (EMPNO in the example) must be equal to at least one of the values in the list returned by the subquery. LASTNAME. Subquery Using IN LASTNAME HAAS THOMPSON KWAN GEYER STERN PULASKI HENDERSE N SPENSER EMPNO 000010 000020 000030 000050 000060 000070 000090 000100 000010 000020 000030 000050 000060 000070 000090 000100 CF124. EMPNO FROM EMPLOYEE WHERE EMPNO IN (SELECT MGRNO FROM DEPARTMENT) Final result Result of subquery FIRSTNME CHRISTINE MICHAEL SALLY JOHN IRVING EVA EILEEN THEODORE Figure 7-19.1 © Copyright IBM Corporation 2007 Notes: IN and NOT IN can be used to compare with a list of values. 2007 Unit 7.V3.1 Student Notebook Uempty Subquery Using IN List the names and employee numbers of employees who are managers of a department SELECT FIRSTNME.0.

also enclosed in parenthesis.1 Notes: In later releases of DB2 UDB it is possible to use the syntax illustrated in this WHERE clause. A list of items enclosed in parenthesis can be compared with a list of values. LASTNAME. 1999. JOB) = ('D21'. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. WORKDEPT. The comparison operator must be equal (=) or not equal (<>). . on the right hand side of the comparison operator. DB2 UDB will rewrite the WHERE clause to the equivalent of: WHERE WORKDEPT = 'D21' AND JOB = 'CLERK' Check the reference manuals for your environment to determine if the syntax is supported. Both lists must contain the same number of items and the items must be data type compatible. 'CLERK') EMPNO 000230 000240 000250 000260 000270 LASTNAME JEFFERSON MARINO SMITH JOHNSON PEREZ WORKDEPT D21 D21 D21 D21 D21 JOB CLERK CLERK CLERK CLERK CLERK © Copyright IBM Corporation 2007 Figure 7-20.Student Notebook Variations on Predicates SELECT EMPNO. JOB FROM EMPLOYEE WHERE (WORKDEPT. Variations on Predicates CF124. 7-8 DB2 SQL Workshop © Copyright IBM Corp.

1999. The comparison operator must be IN or NOT IN.1.1 Student Notebook Uempty Subquery Using IN List the people in Smith's department that have the same job as Smith SELECT FIRSTNME. even if the subquery returns only one row. WORKDEPT. JOB FROM EMPLOYEE WHERE (WORKDEPT. © Copyright IBM Corp.0. Using Subqueries 7-9 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 2007 Unit 7. JOB FROM EMPLOYEE WHERE LASTNAME = 'SMITH') LASTNAME JEFFERSON MARINO SMITH JOHNSON PEREZ SCHNEIDER PARKER SMITH SETRIGHT © Copyright IBM Corporation 2007 FIRSTNME JAMES SALVATORE DANIEL SYBIL MARIA ETHEL JOHN PHILIP MAUDE Figure 7-21.1 Notes: Later releases of DB2 UDB support the above syntax with a list of items enclosed in parenthesis compared with a multiple value comparison operator to a subquery returning zero. JOB) IN (SELECT WORKDEPT.V3. Subquery Using IN WORKDEPT D21 D21 D21 D21 D21 E11 E11 E11 E11 JOB CLERK CLERK CLERK CLERK CLERK OPERATOR OPERATOR OPERATOR OPERATOR CF124. one or more rows with the same number of items as in the list on the left. LASTNAME. .

.. Subquery Using NOT IN Notes: The subquery in the example builds a list of the DEPTNO values for every department of the PROJECT table. In other words. When the IN is prefaced with a NOT. the outer query will always produce an empty result set! 7-10 DB2 SQL Workshop © Copyright IBM Corp..Student Notebook Subquery Using NOT IN DEPARTMENT Table DEPTNO DEPTNAME SPIFFY COMPUTER SERVICE PLANNING INFORMATION CENTER Which departments do not have projects assigned to them? A00 B01 C01 . all of the rows of the outer query that do not match any of the values returned by the subquery will appear in the result set.. enables the outer query to compare the values in a column of the outer table with a list of values provided by the subquery. when used with a subquery. . For an IN predicate. SELECT FROM WHERE DEPTNO. as in the example. The IN predicate. the rows in the outer query that contain the matching value will appear in the final result set. If the NOT IN subquery returns a null.. the subquery should be written to return only non-null values. if any of the non-null values returned by the subquery match any of the non-null values of the column being searched in the outer query. The outer query determines the department number and department name of any department whose department number is NOT in the list returned by the subquery. the query is reporting on departments which do not have projects. Nulls in the subquery result will never match nulls in the outer query result. DEPTNAME DEPARTMENT DEPTNO NOT IN (SELECT DEPTNO FROM PROJECT WHERE DEPTNO IS NOT NULL) Result of Subquery Final Result DEPTNO DEPTNAME A00 SPIFFY COMPUTER SERVICE © Copyright IBM Corporation 2007 B01 C01 D01 D11 D21 E01 E11 E21 CF124. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.1 Figure 7-22. Then.

V3.0.33333333 26110. SELECT FROM WHERE GROUP BY HAVING WORKDEPT.1 Student Notebook Uempty Subquery within HAVING Clause I need a list of the departments whose average salary for non-managers is higher than the company-wide average for non-managers.1 Notes: Subqueries can be also used in a HAVING condition for GROUP values. Subquery within HAVING Clause AVG_WORKDEPT 42833. 1999. . Using Subqueries 7-11 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.80000000 CF124. © Copyright IBM Corp. The department with the highest average should be listed first. 2007 Unit 7.1.00000000 © Copyright IBM Corporation 2007 Result of Subquery 25188. AVG(SALARY) AS AVG_WORKDEPT EMPLOYEE JOB <> 'MANAGER' WORKDEPT AVG(SALARY) > (SELECT AVG(SALARY) FROM EMPLOYEE WHERE JOB <> 'MANAGER') ORDER BY AVG_WORKDEPT DESC Final Result WORKDEPT A00 C01 Figure 7-23.

. 1999.1 Notes: 7-12 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. Restrictions/Reminders CF124.Student Notebook Restrictions/Reminders Subqueries Must be on right side of search condition when used in WHERE clause or in HAVING clause Must be enclosed in parentheses Can return single or multiple values Number of values subquery can return must be compatible with operator in outer SELECT The subquery must return the same number of items as in the list to which it is compared © Copyright IBM Corporation 2007 Figure 7-24.

1 Student Notebook Uempty Checkpoint 1.) c. looking for a match? © Copyright IBM Corporation 2007 Figure 7-25. WHERE SALARY > ALL (SELECT SALARY FROM EMPLOYEE. 2. WHERE SALARY = (SELECT MAX(SALARY) FROM EMPLOYEE. © Copyright IBM Corp. 3. 2007 Unit 7.1.. 2..) b..1 Notes: Please write down your answers below: 1. If you want to list the employees whose salary is the highest. True or False? Subqueries can be used in WHERE or HAVING clauses of a SELECT. 1999.V3. What is the keyword taught in this topic that compares a single value to a set of values returned by a subquery. the outer query's WHERE clause may be coded: a. WHERE SALARY > (SELECT SALARY FROM EMPLOYEE.0.) 3. . Checkpoint CF124. Using Subqueries 7-13 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Student Notebook Unit Summary Having completed this unit. Unit Summary CF124. 1999.1 Notes: 7-14 DB2 SQL Workshop © Copyright IBM Corp. . you should be able to: Use subqueries in WHERE and HAVING clauses Code subqueries using basic predicates Code subqueries using IN © Copyright IBM Corporation 2007 Figure 7-26. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

Maintaining Data 8-1 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. These are used to add rows to a table (INSERT). Maintaining Data What This Unit Is About Along with the SELECT statement.1. the SQL Data Manipulation Language (DML) has three additional SQL statements. modify data within existing rows (UPDATE). you should be able to: • Create a simple table for testing DML statements • Create a simple view • Insert rows into a table • Update data in a table • Delete rows from a table • Drop a table How You Will Check Your Progress Accountability: • Checkpoint • Machine labs © Copyright IBM Corp. . 2007 Unit 8.V3. 1999.0. and remove rows (DELETE).1 Student Notebook Uempty Unit 8. What You Should Be Able to Do After completing this unit.

8-2 DB2 SQL Workshop © Copyright IBM Corp. Unit Objectives CF124.1 Notes: Creating tables is normally done by database administrators. After the test table has been created. UPDATE. .Student Notebook Unit Objectives After completing this unit. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. change rows. and remove rows. you should be able to: Create a simple table to test DML statements Create a view Insert rows into a table Update data in a table Delete rows from a table Drop a table © Copyright IBM Corporation 2007 Figure 8-27. For test purposes. INSERT. 1999. and DELETE statements can be used to add rows. it could be helpful to create a simple test table by yourself.

1 Maintaining Data © Copyright IBM Corp. 2007 Unit 8.1.0. Maintaining Data 8-3 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.V3. 1999.1 Student Notebook Uempty 8. .

00 - © Copyright IBM Corporation 2007 Figure 8-28.00 38250.00 25000. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . 1999.00 800. 8-4 DB2 SQL Workshop © Copyright IBM Corp.Student Notebook TESTEMP Table EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS 000010 000020 000030 000050 000111 HAAS THOMPSON KWAN GEYER SMITH A00 B01 C01 E01 C01 1965-01-01 1973-10-10 1975-04-04 1949-08-17 1998-06-25 52750.00 800.00 1000.1 Notes: The visual shows the contents of the TESTEMP table.00 800. This table will be the basis for the exercises of this unit.00 40175. TESTEMP Table CF124.00 41250.

IN DB_NAME.Table must be created in the named table space IN DATABASE DB_NAME .IN DATABASE DB_NAME -. Linux.or -. .1. The format of the IN clause depends on the environment. No IN clause if the table is to be created in the default database. Create Table TESTEMP CF124. ) -. z/OS: IN DB_NAME.IN TS_NAME © Copyright IBM Corporation 2007 CHAR(6) VARCHAR(15) CHAR(3) DATE DECIMAL(9. 1999. and the table is to be created in a table space which will be created by the system in this database. In addition to the column definitions. This is almost never allowed by administrators in a z/OS environment.or -.If a table space TS_NAME has been provided and the table should be created in this table space. .2) DECIMAL(9. © Copyright IBM Corp. . . UNIX. and Windows: IN TS_NAME .TS_NAME .1 Notes: This CREATE TABLE statement creates the TESTEMP table. Maintaining Data 8-5 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. an IN clause may be required.TS_NAME Figure 8-29.2) NOT NULL.1 Student Notebook Uempty Create Table TESTEMP CREATE TABLE TESTEMP (EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS -.If a database DB_NAME has been provided.V3. NOT NULL.0. 2007 Unit 8.

1999. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. .Student Notebook No IN clause if a default table space has been provided and the table should be created in the default table space. Databases or table spaces are generally provided by a database administrator. iSeries: the IN clause is not used. 8-6 DB2 SQL Workshop © Copyright IBM Corp.

Users referencing the view PERSINFO must use the new column name. LASTNAME.1. YEAR_SAL) AS SELECT EMPNO. © Copyright IBM Corp. 2007 Unit 8. In the above example. consisting of employee number.00 25000.00 Figure 8-30. Create View PERSINFO CF124.1 Student Notebook Uempty Create View PERSINFO CREATE VIEW PERSINFO AS SELECT EMPNO. Another possibility for renaming columns in a view is: CREATE VIEW PERSINFO (EMPNO. Maintaining Data 8-7 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. SALARY FROM EMPLOYEE WHERE WORKDEPT = 'C01' When using this syntax.1 Notes: This CREATE VIEW statement describes a part of the base table. and yearly salary of all employees in department C01. The AS clause in the subselect enables you to specify column names in the view different from the column names in the referenced table.0. SALARY AS YEAR_SAL FROM TESTEMP WHERE WORKDEPT = 'C01' SELECT * FROM PERSINFO EMPNO LASTNAME YEAR_SAL 000030 000111 KWAN SMITH © Copyright IBM Corporation 2007 38250. LASTNAME. . the number of column names listed in the parentheses must match the number of the selected columns.V3. last name. LASTNAME. "SALARY AS YEAR_SAL" is used to rename the SALARY column in the base table TESTEMP to YEAR_SAL in view PERSINFO. 1999.

1999. Other data is not visible.Student Notebook Users who are authorized to use view PERSINFO will get the listed columns for employees whose department number is C01. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 8-8 DB2 SQL Workshop © Copyright IBM Corp. .

'SMITH'. the column list is omitted. a column list is specified.0. The number of values provided for a row via the VALUES clause must be equal to the number of column names in the column list. in the order they are defined for the table or the view. inserts via a view are only possible if the view contains all columns that are defined with NOT NULL without a DEFAULT value in the base table. © Copyright IBM Corp. and so on. SALARY) VALUES ('000111'. 'C01'. In the second example.1 Notes: Use an INSERT statement to insert a row into a table. The first value is inserted in the first column of the list. 25000. In this case.1. Inserting a row via a view inserts the row into the table on which the view is based. the second value in the second column.00 BONUS - © Copyright IBM Corporation 2007 Figure 8-31.V3. LASTNAME. Maintaining Data 8-9 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 'SMITH'. 25000) EMPNO 000111 LASTNAME SMITH WORKDEPT C01 HIREDATE 1998-06-25 SALARY 25000. 1999. 'C01'. NULL) OR INSERT INTO TESTEMP (EMPNO. HIREDATE. WORKDEPT. a value for each column in the table or view must be provided in the VALUES clause. In the first example. Therefore. '1998-06-25'. '1998-06-25'. 2007 Unit 8. . Inserting Rows into the Table CF124.1 Student Notebook Uempty Inserting Rows into the Table INSERT INTO TESTEMP VALUES ('000111'.

Student Notebook DB2 UDB for Linux. . for example: INSERT INTO TESTEMP VALUES ('000111'. NULL). and Windows allows inserting multiple rows in one INSERT statement. 'JONES'. '2001-06-25'. 1999. ('000113'. 'SMITH'. 'C01'. 'THOMPSON'. 25000. '2001-06-25'. NULL). NULL) 8-10 DB2 SQL Workshop © Copyright IBM Corp. 25000. '1998-06-25'. UNIX. 'C01'. ('000114'. 25000. 'C01'. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

we assume that the rows for SMITH. If a columns list for the target table is used.BONUS FROM EMPLOYEE WHERE EMPNO <= '000050' SELECT * FROM TESTEMP EMPNO 000111 000113 000114 000010 000020 000030 000050 LASTNAME SMITH JONES THOMPSON HAAS THOMPSON KWAN GEYER WORKDEPT C01 C01 C01 A00 B01 C01 E01 HIREDATE 1998-06-25 2001-06-25 2001-06-25 1965-01-01 1973-10-10 1975-04-05 1949-08-17 SALARY 25000.00 25000.00 40175. The number and sequence of the selected columns of the EMPLOYEE table must match the number and sequence of the columns of the TESTEMP table. JONES and THOMPSON had already been inserted in the TESTEMP table.V3.SALARY. Insert Multiple Rows CF124.00 800.0.00 800. © Copyright IBM Corp.HIREDATE.00 © Copyright IBM Corporation 2007 Figure 8-32. 1999. and we use this INSERT to copy selected rows from the EMPLOYEE table into the TESTEMP table.00 25000. 2007 Unit 8.1 Student Notebook Uempty Insert Multiple Rows INSERT INTO TESTEMP SELECT EMPNO. . Maintaining Data 8-11 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.00 800.1 Notes: Instead of the VALUES clause.00 38250. In the example. include a SELECT statement in the INSERT statement to tell DB2 UDB that another table or view contains the data for the new rows.WORKDEPT.00 41250. all its NOT NULL columns must be included.00 52750.LASTNAME.1.00 BONUS 1000.

the columns must be separated by commas. Our new colleague. Mr Smith (employee number 000111) receives a first bonus and a salary raise. the UPDATE statement must be coded as follows: UPDATE TESTEMP SET BONUS = NULL WHERE EMPNO = '000111' 8-12 DB2 SQL Workshop © Copyright IBM Corp.00 800. To set BONUS to NULL.00 52750.00 25000.00 UPDATE TESTEMP SET BONUS = 500. we must set BONUS to 500 and SALARY to 26000 If more than one column is changed.00 800.00 40175. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook Update Columns (1 of 2) Before: EMPNO 000111 000113 000114 000010 000020 000030 000050 LASTNAME SMITH JONES THOMPSON HAAS THOMPSON KWAN GEYER WORKDEPT C01 C01 C01 A00 B01 C01 E01 HIREDATE 1998-06-25 2001-06-25 2001-06-25 1965-01-01 1973-10-10 1975-04-05 1949-08-17 SALARY 25000.00 BONUS 500.00 25000. Update Columns (1 of 2) Notes: The UPDATE statement updates the values of the specified columns for one ore more rows of a table or view.00 BONUS 1000.00 40175. SALARY = 26000 WHERE EMPNO = '000111' After: EMPNO 000111 000113 000114 000010 000020 000030 000050 LASTNAME SMITH JONES THOMPSON HAAS THOMPSON KWAN GEYER WORKDEPT C01 C01 C01 A00 B01 C01 E01 HIREDATE 1998-06-25 2001-06-25 2001-06-25 1965-01-01 1973-10-10 1975-04-05 1949-08-17 SALARY 26000.00 38250.00 25000.00 800. 1999.00 CF124.00 800.00 52750.00 800. Therefore.00 41250.00 25000.00 41250. .00 1000.00 38250.1 © Copyright IBM Corporation 2007 Figure 8-33.00 800. The SET keyword must precede the first column and must not be repeated.

00 800. Maintaining Data 8-13 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.00 52750. The salary of all employees should be increased by 1.00 41250.00 1000.000.V3.00 25000.00 BONUS 500. 1999.00 UPDATE TESTEMP SET SALARY = SALARY + 1000 WHERE WORKDEPT = 'C01' After: EMPNO 000111 000113 000114 000010 000020 000030 000050 LASTNAME SMITH JONES THOMPSON HAAS THOMPSON KWAN GEYER WORKDEPT C01 C01 C01 A00 B01 C01 E01 HIREDATE 1998-06-25 2001-06-25 2001-06-25 1965-01-01 1973-10-10 1975-04-05 1949-08-17 SALARY 27000. 2007 Unit 8.00 39250. © Copyright IBM Corp. all rows of the table or view are updated.1.00 40175.00 26000.00 CF124. To update selected rows.00 40175.00 800.00 52750.00 25000. use the WHERE clause to specify the rows to be updated.00 800.00 800. Update Columns (2 of 2) Notes: Our department C01 has finished a project with great success.00 41250. If you omit the WHERE clause.00 26000.1 Student Notebook Uempty Update Columns (2 of 2) Before: EMPNO 000111 000113 000114 000010 000020 000030 000050 LASTNAME SMITH JONES THOMPSON HAAS THOMPSON KWAN GEYER WORKDEPT C01 C01 C01 A00 B01 C01 E01 HIREDATE 1998-06-25 2001-06-25 2001-06-25 1965-01-01 1973-10-10 1975-04-05 1949-08-17 SALARY 26000.00 38250. .00 1000.00 800.0.00 BONUS 500.00 800.1 © Copyright IBM Corporation 2007 Figure 8-34.

00 41250.00 39250.00 800. 8-14 DB2 SQL Workshop © Copyright IBM Corp.00 800.00 41250.00 BONUS 1000. use the WHERE clause to specify the rows to be deleted.1 © Copyright IBM Corporation 2007 Notes: Mr Smith left the company. To delete specific rows. If you omit the WHERE clause.00 52750. all rows of the table or view are deleted. 1999.00 39250. the DELETE statement removes zero.00 800. or more rows of a table.00 26000.00 26000.00 800.00 BONUS 500.00 26000. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. we delete the row with employee number 000111. Depending on the search condition of the WHERE clause. .00 40175.Student Notebook Delete Rows Before: EMPNO 000111 000113 000114 000010 000020 000030 000050 LASTNAME SMITH JONES THOMPSON HAAS THOMPSON KWAN GEYER WORKDEPT C01 C01 C01 A00 B01 C01 E01 HIREDATE 1998-06-25 2001-06-25 2001-06-25 1965-01-01 1973-10-10 1975-04-05 1949-08-17 SALARY 27000.00 800. Therefore. The DELETE statement removes entire rows from a table.00 40175.00 800.00 1000. one.00 CF124.00 DELETE FROM TESTEMP WHERE EMPNO = '000111' After: EMPNO 000113 000114 000010 000020 000030 000050 Figure 8-35. Delete Rows LASTNAME JONES THOMPSON HAAS THOMPSON KWAN GEYER WORKDEPT C01 C01 A00 B01 C01 E01 HIREDATE 2001-06-25 2001-06-25 1965-01-01 1973-10-10 1975-04-05 1949-08-17 SALARY 26000.00 52750.

As a consequence. © Copyright IBM Corp. that is. DB2 UDB deletes the object. . DROP TABLE TESTEMP VIEW PERSINFO is automatically dropped © Copyright IBM Corporation 2007 Figure 8-36. the table. 2007 Unit 8. 1999.1 Notes: When executing the statement DROP TESTEMP. indexes) that are dependent on the table are deleted as well.V3.1 Student Notebook Uempty Drop Table TESTEMP Now test table TESTEMP is no longer needed. views. all rows and the table definition are deleted. Any objects (for example. Drop Table TESTEMP CF124.1.0. Maintaining Data 8-15 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

. 1999. Reminders CF124.1 Notes: See your Database Administrator (DBA) for appropriate authorizations. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook Reminders You must be authorized to execute CREATE/DROP table statements You must be authorized to execute statements which change table content Dropping a table drops dependent objects Search condition in the WHERE clause qualifies rows to be updated or deleted Inserting a row into a view actually inserts the row into the table on which the view is based Updating a row of a view updates the row of the table on which the view is based Omitting the WHERE clause of an UPDATE or DELETE statement causes all rows of the table or view to be updated or deleted © Copyright IBM Corporation 2007 Figure 8-37. 8-16 DB2 SQL Workshop © Copyright IBM Corp.

1999. Checkpoint CF124. you must execute a. © Copyright IBM Corp. 2. 2.1 Student Notebook Uempty Checkpoint 1. To delete all rows of a table and its description. DELETE FROM table name © Copyright IBM Corporation 2007 Figure 8-38.0. 2007 Unit 8. True or False? You must explicitly be authorized to INSERT data rows in your own table. DROP TABLE table name b. Maintaining Data 8-17 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.1. DELETE FROM table name and DROP TABLE table name c. .1 Notes: Please write down your answers below: 1.V3.

. you should be able to: Create a simple table to test DML statements Create a view Insert rows into a table Update data in a table Delete rows from a table Drop a table © Copyright IBM Corporation 2007 Figure 8-39. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.Student Notebook Unit Summary Having completed this unit.1 Notes: 8-18 DB2 SQL Workshop © Copyright IBM Corp. Unit Summary CF124.

. INT DECIMAL(x. time. 2007 Appendix A.0.y) SELECT INSERT UPDATE DELETE © Copyright IBM Corporation 2006 3. 4.1 Student Notebook AP Appendix A. DEC(x. timestamp sorting Extraction of components CHAR/CHARACTER (fixed length) VARCHAR (variable length) SMALLINT INTEGER. Checkpoint Solutions Unit 1 Checkpoint Solutions 1.y).1. © Copyright IBM Corp.V3. Checkpoint Solutions A-1 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999. True Automatic verification of correctness Date arithmetic Date. 2.

Student Notebook Unit 2 Checkpoint Solutions 1. c 3. SELECT. Apostrophes (or single quotes) 5. False They are joined by AND or OR. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. . © Copyright IBM Corporation 2007 A-2 DB2 SQL Workshop © Copyright IBM Corp. False 2. The keyword LIKE 6. FROM 4. 1999.

As short names for (qualified) tables To avoid ambiguity To establish correlated references © Copyright IBM Corporation 2007 © Copyright IBM Corp.0. b 3. Checkpoint Solutions A-3 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. True 2. 1999. .1.V3.1 Student Notebook AP Unit 3 Checkpoint Solutions 1. 2007 Appendix A.

DECIMAL SUBSTR COALESCE VALUE YEAR MONTH DAY DAYS ROUND © Copyright IBM Corporation 2007 A-4 DB2 SQL Workshop © Copyright IBM Corp. c 3. 1999. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.Student Notebook Unit 4 Checkpoint Solutions 1. . False 2.

0. False.V3. False. 3. © Copyright IBM Corporation 2007 © Copyright IBM Corp. False. A GROUP BY is required and all three columns not in column functions must be listed in the GROUP BY clause. 2. Column functions may not be used in a WHERE clause.1 Student Notebook AP Unit 5 Checkpoint Solutions 1. e 4. . A column function produces a summary row for a set of rows. 2007 Appendix A.1. Checkpoint Solutions A-5 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 1999.

When all duplicate rows have to be eliminated. b. . Why or when should you use UNION? a.Student Notebook Unit 6 Checkpoint 1. 3. When SELECTs with an arbitrary number of columns should be combined. 1999. Which rules do you have to consider when using UNION? © Copyright IBM Corporation 2007 A-6 DB2 SQL Workshop © Copyright IBM Corp. True or False? The results of arbitrary SELECTs can be combined by means of UNION and ordered to get a new result. 2. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

V3. 2007 Appendix A. .1 Student Notebook AP Unit 7 Checkpoint Solutions 1.0.1. True 2. Checkpoint Solutions A-7 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. IN © Copyright IBM Corporation 2007 © Copyright IBM Corp. 1999. b 3.

1999. . a. False. 2.Student Notebook Unit 8 Checkpoint Solutions 1. The owner of a table has all privileges on the table. b © Copyright IBM Corporation 2007 A-8 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

. Built-in Functions Following is a list of SQL functions generally available on most platforms.0. 1999.1 Student Notebook AP Appendix B. Please refer to the SQL Reference manual for your platform and version. Aggregate functions AVG MIN COUNT STDDEV COUNT_BIG SUM MAX VARIANCE Scalar functions ABS ATANH CHAR COS DAYNAME DAYS DECRYPT_CHAR ENCRYPT GETHINT IDENTITY_VAL_LOCAL LCASE LOCATE MICROSECOND MONTH POSSTR RAISE_ERROR REPLACE SECOND SMALLINT TAN TRANSLATE VALUE WEEK_ISO ACOS ATAN2 CLOB COSH DAYOFWEEK DBCLOB DEGREES EXP GRAPHIC INSERT LEFT LOG10 MIDNIGHT_SECONDS MONTHNAME POWER RAND RIGHT SIGN SPACE TANH TRUNCATE VARCHAR YEAR ASIN BLOB COALESCE DATE DAYOFWEEK_ISO DECIMAL DIGITS FLOAT HEX INTEGER LENGTH LOWER MINUTE MULTIPLY_ALT QUARTER REAL ROUND SIN SQRT TIME UCASE VARGRAPHIC ATAN CEILING CONCAT DAY DAYOFYEAR DECRYPT_BIT DOUBLE_PRECISION FLOOR HOUR JULIAN_DAY LN LTRIM MOD NULLIF RADIANS REPEAT RTRIM SINH SUBSTR TIMESTAMP UPPER WEEK © Copyright IBM Corp.1. Built-in Functions B-1 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 2007 Appendix B.V3. and to the SQL Reference for Cross-Platform Development.

1999. .Student Notebook B-2 DB2 SQL Workshop © Copyright IBM Corp. 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

V3.1 backpg Back page .0.1.

® .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times