ce 1319 stud

310
Enterprise Computing Solutions Business Intelligence Data Centre Cloud Mobility Comunidad de Madrid Dirección General de Formación CONSEJERÍA DE EMPLEO, TURISMO Y CULTURA UNIÓN EUROPEA FONDO SOCIAL EUROPEO El Fondo Social Europeo invierte en tu futuro EDUCATION S E R V I C E S Student Manual

Upload: rashokkumar82

Post on 27-Jan-2016

7 views

Category:

Documents


0 download

DESCRIPTION

IBM DB2

TRANSCRIPT

Page 1: Ce 1319 Stud

Enterprise Computing Solutions

Business Intelligence

Data Centre Cloud Mobility

Comunidad de Madrid

Dirección General de Formación

CONSEJERÍA DE EMPLEO,TURISMO Y CULTURA

UNIÓN EUROPEAFONDO SOCIAL EUROPEO

El Fondo Social Europeo invierte en tu futuro

EDUCATIONS E R V I C E S

Student Manual

Page 2: Ce 1319 Stud

V6.0

cover

Exclus

ivo fo

r

proy

ecto

Front cover

DB2 SQL Workshop for Experienced Users (Course code CE131)

Student NotebookERC 9.4

IBM certified course material

mación

C.F.T.I.C.

Page 3: Ce 1319 Stud

Student Notebook

.

Trademarks

The reader should recognize that the following terms, which appear in the content of this training document, are official trademarks of IBM or other companies:

IBM® and the IBM logo are registered trademarks of International Business Machines Corporation.

The following are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide:

Intel and Intel Core are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both.

Microsoft and Windows are trademarks 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.

Java and all Java-based trademarks and logos are trademarks or registered trademarks of Oracle and/or its affiliates.

VMware and the VMware "boxes" logo and design, Virtual SMP and VMotion are registered trademarks or trademarks (the "Marks") of VMware, Inc. in the United States and/or other jurisdictions.

Other product and service names might be trademarks of IBM or other companies.

AIX® AS/400® Command Center®DB2 Connect™ DB2® IMS™iSeries® MVS™ Notes®OS/390® OS/400® QMF™WebSphere® z/OS® zSeries®400®

xclus

ivo fo

rmac

ión

yecto

C.F.T.I.C

April 2013 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 2007, 2013.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.

E pr

o

Page 4: Ce 1319 Stud

Student NotebookV6.0

TOC

E

Contents

Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix

Course description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

Agenda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

Unit 0. DB2 SQL Workshop for Experienced Users . . . . . . . . . . . . . . . . . . . . . . . . . 0-1Course objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0-2Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0-3Agenda - Day 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0-4Agenda - Day 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0-5Agenda - Day 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0-6

Unit 1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2DB2 products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3DB2 system catalog tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4DB2 system catalog - Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6Databases in DB2 for Linux, UNIX, and Windows . . . . . . . . . . . . . . . . . . . . . . . . . 1-7Subsystem in DB2 for z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8Selecting rows and columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9Column functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11Summarizing group values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13GROUP BY ROLLUP (DB2 for Linux, UNIX, and Windows only) . . . . . . . . . . . . . 1-15GROUP BY CUBE (DB2 for Linux, UNIX, and Windows only) . . . . . . . . . . . . . . . 1-17Grouping function - Why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18Grouping function - Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-19GROUP BY grouping sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20Super groups - Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21OLAP function for ranking (for DB2 z/OS and DB2 for Linux, UNIX, and Windows) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-22Additional SQL in this course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-24Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-25Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-26

Unit 2. Create Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2Scenario (Part 1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3Creating tables and adding columns (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5Creating tables and adding columns (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7Scenario (Part 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9Check constraints - The need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10Check constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12Scenario (Part 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Contents iii

Page 5: Ce 1319 Stud

Student Notebook

.

Create index and primary key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-16Referential integrity (Part 1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-18Referential integrity - Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-20Referential integrity - DELETE rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-22ALTER TABLE - Adding referential constraints . . . . . . . . . . . . . . . . . . . . . . . . . . .2-23Referential integrity (Part 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-24Information integrity constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-26Scenario (Part 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-28Triggers - The need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-29CREATE TRIGGER (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-31CREATE TRIGGER (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-33MERGE data into tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-35View considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-36Sample view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-38View hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-40Read-only views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-42Changing data through views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-44INSTEAD OF triggers (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-46INSTEAD OF triggers (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-47CHECK OPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-48View Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-50View materialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-51Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-52Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-53

Unit 3. Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-1Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-2Sample tables for unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-3Join (original syntax) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-5INNER JOIN (newer syntax) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-7LEFT OUTER JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-8RIGHT OUTER JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-9FULL OUTER JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-10Anti - JOIN (1 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-11Anti - JOIN (2 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-12Anti - JOIN (3 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-13Joins of more than two tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-14Joins and local predicates (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-17Joins and local predicates (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-18Joining a table with itself (1 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-20Joining a table with itself (2 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-21Joining a table with itself (3 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-22Joining a table with itself - Another example (1 of 2) . . . . . . . . . . . . . . . . . . . . . . .3-23Joining a table with itself - Another example (2 of 2) . . . . . . . . . . . . . . . . . . . . . . .3-24UNION and UNION ALL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-25EXCEPT and INTERSECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-26Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-28Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-29

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

iv DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 6: Ce 1319 Stud

Student NotebookV6.0

TOC

E

Unit 4. CASE, CAST, Summary (Materialized Query) Tables and Temporary Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-1

Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2CASE expressions in SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3CASE expression in WHERE clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5CASE expression in a function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6Nested CASE expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7CAST specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8Summary tables and Materialized Query Tables (MQTs) . . . . . . . . . . . . . . . . . . . 4-10Summary tables or MQTs - Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12Materialized Query Tables - Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14Temporary tables - Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15Temporary tables - Types in DB2 z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17Temporary tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-20Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21

Unit 5. Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-1Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2Subquery with basic predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3Subquery with IN predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5Subquery with NOT IN predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7NOT IN predicate for nullable column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9Subquery with ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11Subquery with ALL predicate (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13Subquery with ALL predicate (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15Subquery with ANY or SOME predicate (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17Subquery with ANY or SOME predicate (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18Subquery with EXISTS predicate (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-20Subquery with EXISTS Predicate (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21Correlated subquery (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23Correlated subquery (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-24Correlated subquery with EXISTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-26Scalar Fullselect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-27UPDATE including subquery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-28Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-29Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-30

Unit 6. Scalar Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-1Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2Functions - Two types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3Scalar functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4Scalar function - SUBSTR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5Position string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7Scalar function - COALESCE/VALUE (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8Scalar function - COALESCE/VALUE (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10Scalar function - DECIMAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11Scalar function - ROUND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-13

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Contents v

Page 7: Ce 1319 Stud

Student Notebook

.

Scalar function - DIGITS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-15Scalar functions - SQRT and POWER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-17DB2 DATE, TIME and TIMESTAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-18Output formats for date and time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-20Scalar function - CHAR (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-22Scalar function - CHAR (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-24Scalar function - LENGTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-26Scalar functions - LTRIM / RTRIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-28Date-related scalar functions (1 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-31Date-related scalar functions (2 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-33Date-related scalar functions (3 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-36Date-related scalar functions (4 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-38Time/Timestamp-related scalar functions (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . .6-40Time/Timestamp-related scalar functions (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . .6-42Labeled durations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-43Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-45Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-46

Unit 7. Table Expressions and Recursive SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-1Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-2Nested Table Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-3Nested Table Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-4Nested table expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-5Nested table expressions in joins (1 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-7Nested table expressions in joins (2 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-9Nested table expressions in joins (3 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-10Nested table expressions in joins (4 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-11Common table expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-12Recursive SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-14Recursive SQL - Initialization SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-16Recursive SQL - First iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-17Recursive SQL - Second iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-19Recursive SQL - Main SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-21Controlling depth of recursion (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-22Controlling depth of recursion (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-24Recursive SQL - Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-25Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-27Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-28

Unit 8. An Introduction to UDTs/UDFs and Stored Procedures . . . . . . . . . . . . . . . .8-1Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-2User-defined Distinct Types - The need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-3User-defined Distinct Types - Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-4User-Defined Functions - The need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-6User-Defined Functions - Sourced . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-7User-Defined Functions - External . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-9User-Defined SQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-10Stored Procedures - Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-11

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

vi DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 8: Ce 1319 Stud

Student NotebookV6.0

TOC

E

Stored Procedures - Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14

Unit 9. SQL and DB2 Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-1Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2The Optimizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-3Index concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-5Clustered and Non-clustered indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6Index utilization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-11Predicate processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-12General guidelines - Correlated subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-14General guidelines - Minimize DB2 sorts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-16General guidelines - Use views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17General guidelines - Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-18General guidelines - NOT EQUAL Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-19General guidelines - Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-20General guidelines - Search value length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-21General guidelines - Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-22General guidelines - Retrieve only necessary data . . . . . . . . . . . . . . . . . . . . . . . . 9-23General guidelines - Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-24Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-25Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-26

Appendix A. Checkpoint Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-1

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Contents vii

Page 9: Ce 1319 Stud

Student Notebook

.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

viii DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 10: Ce 1319 Stud

Student NotebookV6.0

TMK

E

Trademarks

The reader should recognize that the following terms, which appear in the content of this training document, are official trademarks of IBM or other companies:

IBM® and the IBM logo are registered trademarks of International Business Machines Corporation.

The following are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide:

Intel and Intel Core are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both.

Microsoft and Windows are trademarks 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.

Java and all Java-based trademarks and logos are trademarks or registered trademarks of Oracle and/or its affiliates.

VMware and the VMware "boxes" logo and design, Virtual SMP and VMotion are registered trademarks or trademarks (the "Marks") of VMware, Inc. in the United States and/or other jurisdictions.

Other product and service names might be trademarks of IBM or other companies.

AIX® AS/400® Command Center®DB2 Connect™ DB2® IMS™iSeries® MVS™ Notes®OS/390® OS/400® QMF™WebSphere® z/OS® zSeries®400®

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Trademarks ix

Page 11: Ce 1319 Stud

Student Notebook

.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

x DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 12: Ce 1319 Stud

Student NotebookV7.0

pref

E

Course description

DB2 SQL Workshop for Experienced Users

Duration: 2.5 days

Purpose

This course teaches you how to make use of advanced SQL techniques to access DB2 databases in different environments. This course is appropriate for customers working in all DB2 environments, that is OS/390, z/OS, VM/VSE, iSeries, Linux, UNIX, and Windows.

Audience

Experienced SQL end users, application programmers, database administrators, and user support staff who need more advanced knowledge of SQL.

Prerequisites

Before taking this course, you should have experience with coding and executing basic SQL statements.

These skills can be developed by attending CE12, SQL Workshop, or equivalent experience.

Objectives

After completing this course, you should be able to:

• Discuss basic relational database concepts

• Use some of the OLAP features of DB2, such as GROUP and RANK functions

• Create tables, views and indexes

• Use referential integrity, check constraints and triggers

• Use outer joins, and join tables to themselves

• Use CASE expressions, and the CAST function

• Identify the impact of Summary Tables, Materialized Query Tables, and temporary tables

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Course description xi

Page 13: Ce 1319 Stud

Student Notebook

.

• Use complex subqueries

• Use a greater number of scalar SQL functions

• Use advanced SQL constructs, such as recursive SQL and table expressions

• Define User-Defined Distinct Types and User-Defined Functions

• Avoid several of the most common causes for poorly-performing SQL

Contents

This course covers the following major topics:

• Introduction and review

• Creating objects (tables, indexes, views, and so forth)

• Join

• CASE, CAST, Summary Tables, and Materialized Query Tables

• Subqueries

• Scalar Functions

• Table Expressions and Recursive SQL

• User-Defined Distinct Types and Functions

• Performance Considerations

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

xii DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 14: Ce 1319 Stud

Student NotebookV7.0

pref

E

Agenda

Day 1

Welcome Unit 1 - Introduction

(SQL Basics review, and OLAP features) Exercise 1 Unit 2 - Create Objects Exercise 2 Unit 3 - Join Exercise 3

Day 2

Unit 4 - CASE, CAST, Summary Tables, and MQTs Exercise 4 Unit 5 - Using Subqueries Exercise 5 Unit 6 - Scalar Functions Exercise 6

Day 3

Unit 7 - Table Expressions and Recursive SQL Exercise 7 Unit 8 - UDTs/UDFs and Performance Exercise 8 Unit 9 - SQL and DB2 PerformanceExercise 9

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Agenda xiii

Page 15: Ce 1319 Stud

Student Notebook

.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

xiv DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 16: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 0. DB2 SQL Workshop for Experienced Users

What this unit is about

This course provides information about the functions of IBM's

DB2, a relational database manager, can be installed on various operating systems and hardware platforms. Operating systems include z/OS, VM, Linux, UNIX, and Windows.

The focus of this class is on the services DB2 provides and how users work with DB2, not on its internal workings.

What you should be able to do

After completing this course, you should be able to:

• Discuss basic relational database concepts • Use some of the OLAP features of DB2, such as GROUP and

RANK functions • Create tables, views, and indexes • Define referential integrity, check constraints and triggers • Code outer joins, and join tables to themselves • Use CASE expressions, and the CAST function • Identify the impact of Summary Tables, Materialized Query Tables

and temporary tables • Write complex subqueries • Use a greater number of scalar SQL functions • Code advanced SQL constructs, such as recursive SQL and table

expressions • Understand User-Defined Distinct Types and User-Defined

Functions • Avoid several of the most common causes for poorly performing

SQL

How you will check your progress

• Checkpoints

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 0. DB2 SQL Workshop for Experienced Users 0-1

Page 17: Ce 1319 Stud

Student Notebook

.

Figure 0-1. Course objectives CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Course objectivesAfter completing this course, you should be able to: • Discuss basic relational database concepts

• Use some of the OLAP features of DB2, such as GROUP and RANK functions

• Create tables, views, and indexes

• Define referential integrity, check constraints and triggers

• Code outer joins, and join tables to themselves

• Use CASE expressions, and the CAST function

• Identify the impact of Summary Tables, Materialized Query Tables and temporary tables

• Write complex subqueries

• Utilize a greater number of scalar SQL functions

• Code advanced SQL constructs, such as recursive SQL and table expressions

• Understand User-Defined Distinct Types and User-Defined Functions

• Avoid several of the most common causes for poorly-performing SQL

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

0-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 18: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 0-2. Prerequisites CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Before attending this course, you should be able to:

Discuss basic relational database concepts

Code basic SQL statements

These skills can have been acquired by attending:

CE12 - SQL Workshop

Prerequisites

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 0. DB2 SQL Workshop for Experienced Users 0-3

Page 19: Ce 1319 Stud

Student Notebook

.

Figure 0-3. Agenda - Day 1 CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Welcome

Unit 1 : Introduction– Lab Exercises

Unit 2 : Create Objects– Lab Exercises

Unit 3 : Join – Lab Exercises

Agenda – Day 1

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

0-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 20: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 0-4. Agenda - Day 2 CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Unit 4 : CASE, CAST, Summary(Materialized Query) Tablesand Temporary Tables

– Lab Exercises

Unit 5 : Using Subqueries– Lab Exercises

Unit 6 : Scalar Functions– Lab Exercises

Agenda – Day 2

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 0. DB2 SQL Workshop for Experienced Users 0-5

Page 21: Ce 1319 Stud

Student Notebook

.

Figure 0-5. Agenda - Day 3 CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Unit 7 : Table Expressions andRecursive SQL

– Lab Exercises

Unit 8 : An Introduction to UDTs/UDFs and Stored Procedures

– Lab Exercises

Unit 9 : SQL and DB2 Performance– Paper Lab Exercises

Class Evaluation

Class Closing

Agenda – Day 3

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

0-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 22: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 1. Introduction

What this unit is about

This unit presents the key differences among the host and Intel/UNIX platforms, with an emphasis on the database and subsystem.

A brief review of some SQL highlights is included and a description of the new super groups features, ROLLUP and CUBE available in DB2 for Linux, UNIX, and Windows.

What you should be able to do

After completing this unit, you should be able to:

• Identify the purpose of the clauses in the SELECT statement

• Describe the key differences among the IBM DB2 platforms

• Describe and use some of the OLAP features of DB2, such as GROUPING functions like CUBE and ROLLUP, and the RANK, DENSE_RANK and ROW_NUMBER functions

How you will check your progress

• Checkpoint

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-1

Page 23: Ce 1319 Stud

Student Notebook

.

Figure 1-1. Unit objectives CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Unit objectives

After completing this unit, you should be able to:• Identify the purpose of the clauses in the SELECT statement• Describe the key differences among the IBM DB2 platforms• Describe and use some of the OLAP features of DB2, such

as GROUPING functions like CUBE and ROLLUP, and the RANK, DENSE_RANK and ROW_NUMBER functions

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 24: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-2. DB2 products CE1319.4

Notes:

DB2 is a relational database management system (RDBMS) in which users can create, update, and control relational databases using Structured Query Language (SQL). Designed to meet the information needs of small and large businesses alike, it is available on various platforms. These include large systems (z/OS, OS/390, VM, and VSE); mid-sized systems (iSeries, OS/400); single-user or LAN-based systems (Windows 32- and 64-bit operating systems); and UNIX systems such as AIX, HP-UX, and Solaris, and Linux (including Linux for zSeries).

All members of the DB2 family are based on the same relational theory and have common features and functions that ensure a high level of portability from one platform to another. SQL is also portable. Although the various environments have some differences in system implementation, the DDL, DML, and DCL are standard across the platforms.

© Copyright IBM Corporation 2007, 2013

UNIX Systems

DB2

Mainframe Systems

Intel Systems

DB2

Midrange Systems

OS/400

z/OS

VSEand VM

DB2

DB2

DB2

DB2 products

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-3

Page 25: Ce 1319 Stud

Student Notebook

.

Figure 1-3. DB2 system catalog tables CE1319.4

Notes:

• DB2 stores all information about your tables, indexes, columns, and other objects in the DB2 catalog.

• The DB2 catalog consists of normal tables and views, just like the ones in your own databases. They can be read with SQL, just like your own tables.

• On some platforms, including DB2 for z/OS, catalog information is presented in tables. On other platforms, including DB2 for Linux, UNIX, and Windows, catalog information is also presented in views. The SQL Reference for that platform contains full documentation of the catalog tables or views, whichever is preferred for that platform.

• Catalog tables and views can be distinguished by their high-level qualifiers, that is, authorization ID or schema names. The authorization ID used for catalog tables in DB2 for z/OS is SYSIBM. SYSIBM.SYSTABLES is a commonly used catalog table in DB2 for z/OS. SYSCAT and SYSSTAT are the schema names used for catalog views in DB2 for Linux, UNIX, and Windows. SYSCAT.TABLES is a commonly used catalog view in DB2 for Linux, UNIX, and Windows.

© Copyright IBM Corporation 2007, 2013

DB2 system catalog tables

• A set of System Catalog Tables is created and maintained to administer the DB2 objects.

• The System Catalog Tables can be read with a SELECT.

• Some of the System Catalog Tables include information on:– Table/Index definitions

– Column data types

– Defined constraints

– Object dependencies

– Object privileges

– Statistical information about objects

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 26: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• SYSIBM.SYSTABLES/SYSCAT.TABLES contains information about every table and view. SYSIBM.SYSCOLUMNS/SYSCAT.COLUMNS describes every column of every table and view, and SYSIBM.SYSTABAUTH/SYSCAT.TABAUTH lists table privileges granted to authorization IDs.

• DB2 uses the catalog extensively as you work. For example, if you execute a query against a table named EMPLOYEE, DB2:

- Looks in (SYS)TABLES to see whether the EMPLOYEE table exists.

- Looks in (SYS)TABAUTH to see whether you are allowed to access the EMPLOYEE table.

- Uses (SYS)COLUMNS to get the column names and the column sequence for the EMPLOYEE table so that the data can be obtained for you. If the table exists and you are authorized to use it, DB2 presents the result to you. Otherwise, it returns an error message identifying the problem.

Note

The DB2 Catalog is fully documented in the SQL Reference for each platform.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-5

Page 27: Ce 1319 Stud

Student Notebook

.

Figure 1-4. DB2 system catalog - Maintenance CE1319.4

Notes:

Although users can access the catalog with a SELECT statement, the other DML statements (INSERT, UPDATE, and DELETE) are not allowed. Rather, it is the DDL statements CREATE, ALTER, and DROP that are used to initially define, modify, and remove objects in the catalog.

Take frequent backups of the catalog tables (at least daily), to ensure a relatively quick recovery of those crucial objects.

Catalog use by users and the system impacts obtaining optimum performance. Optimizing catalog access has a significant impact on the overall performance of DB2. Depending upon the volatility of the contents, run the utilities REORG and RUNSTATS on a regular basis.

For z/OS environments, the only DB2 objects assigned to buffer pool BP0 are the catalog and directory tables. Place all “user” objects in other buffer pools. The z/OS customers can optionally define additional indexes on the catalog tables, which can also improve performance.

© Copyright IBM Corporation 2007, 2013

DB2 system catalog - Maintenance

• To change the contents of the catalog, use the

DDL statements CREATE, ALTER and DROP.

• Recovery and performance considerations:– Frequent backup (COPY)

– REORG as needed

– RUNSTATS as needed

– For z/OS, a “reserved” bufferpool … BP0

– Optional/additional indexes

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 28: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-5. Databases in DB2 for Linux, UNIX, and Windows CE1319.4

Notes:

• When installing DB2 for Linux, UNIX, and Windows, you create an instance of DB2. One instance can contain several databases.

• Each DB2 for Linux, UNIX, and Windows database has its own catalog.

• A database is a collection of tables. A table consists of a defined number of columns and a varying number of rows.

• In DB2 for Linux, UNIX, and Windows, you connect to a database with the CONNECT TO statement.

• With WebSphere Federation Server, joins between tables, in different databases, are possible. This software must be installed, and the appropriate configuration settings put into place in order to join tables that are in different databases.

© Copyright IBM Corporation 2007, 2013

Databases in DB2 for Linux, UNIX, and Windows

Database 1Table_1 Table_2 Table_n Catalog

- Tables- Columns

.

.

.

.

.- - - - - - - - -

Database 2Table_a Table_b Table_x Catalog

- Tables- Columns

.

.

.

.

.

- - - - - - - - -

- - - - - - - - -

- - - - - - - - -

Join

An “instance” of DB2

Requires “federation”

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-7

Page 29: Ce 1319 Stud

Student Notebook

.

Figure 1-6. Subsystem in DB2 for z/OS CE1319.4

Notes:

• In the z/OS environment, a DB2 subsystem is similar to a database in the Workstation environment because there is one catalog for the subsystem. They are also different since a DB2 subsystem usually contains many databases. In DB2 for z/OS, a database is a name for a group of related tables and other objects.

• You can connect to a subsystem explicitly with SQL statements, but connections are usually made implicitly with parameters in your DB2 software. For example, the subsystem name is identified in the DB2I (DB2 Interactive) Defaults panel. This identification permits DB2I to make the connection for you when you use tools like SPUFI. A user does not need to know the name of the database on order to join tables in different databases in z/OS. There is no need for additional software and configuration settings. Rather, simply coding the appropriate SELECT syntax to invoke the join is all that is required

© Copyright IBM Corporation 2007, 2013

Database 1Table_1 Table_2

Catalog

- Systables- Syscolumns

.

.

.

.

.

Database 2Table_3 Table_4

Table_n

Join

DB2 Subsystem

Subsystem in DB2 for z/OS

Bufferpools

DB2 Log

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 30: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-7. Selecting rows and columns CE1319.4

Notes:

• In a SELECT statement, the SELECT and FROM clauses are mandatory. All other clauses (WHERE, ORDER BY, GROUP BY, HAVING, and others) are optional.

• Select the column or columns by naming each column you want in the SELECT clause of the SELECT statement. The columns are displayed in the result set in the left-to-right order you specify in the SELECT clause, regardless of their sequence in the table.

• Identify one or more tables or views that contain your data with the FROM clause. • Use a WHERE clause to select the rows that you want. A WHERE clause specifies one

or more predicates. A predicate specifies a test you want DB2 to apply to each table row.

- Use LIKE to specify a character string that is similar to the column values of rows you want to select. Two wildcard characters can be used in the comparison string. Use a percent sign (%) to indicate any string of zero or more characters. Use an underscore (_) to indicate any single character.

- Within the pattern, a percent sign or an underscore has a special meaning. To search for a percent sign or an underscore, they must be preceded by an escape

© Copyright IBM Corporation 2007, 2013

000120 O'CONNELL 29250.00 CLERK 14

000060 STERN 32250.00 MANAGER 16

000100 SPENSER 26150.00 MANAGER 14000130 QUINTANA 23800.00 ANALYST 16000280 SCHNEIDER 26250.00 OPERATOR 17000250 SMITH 19180.00 CLERK 15

EMPNO LASTNAME SALARY JOB EDLEVEL

EMPLOYEE TableSELECT LASTNAME, SALARY, EMPNO

FROM EMPLOYEE WHERE EDLEVEL <= 16

AND SALARY BETWEEN 19000 AND 30000AND LASTNAME LIKE 'S%'AND JOB IN ('MANAGER', 'OPERATOR', 'ANALYST')

ORDER BY LASTNAME

Result 000100SPENSER 26150.00EMPNOLASTNAME SALARY

Selecting rows and columns

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-9

Page 31: Ce 1319 Stud

Student Notebook

.

character. If they are not preceded by an escape character, they are considered wildcard characters. The escape character must be identified with the ESCAPE clause. When used in the pattern, the escape character applies to the subsequent character of the pattern. This presentation indicates that the character is to be used as specified and not as a wildcard character. In a pattern, you can use the escape character multiple times. Assuming that column NAME of table SYSIBM.SYSTABLES contains the table names and you are searching for table names starting with EMP_; then specify the following SELECT statement:

SELECT * FROM SYSIBM.SYSTABLESWHERE NAME LIKE 'EMP$_%' ESCAPE '$'

Only % is used as a wildcard character.

• You can use BETWEEN to select rows in which a column has a value within a range. Specify the lower boundary of the BETWEEN predicate first, then the upper boundary. The range is inclusive.

• You can use the IN predicate to select each row that has a column value equal to one of several listed values.

• Use an ORDER BY clause to sort the data in the result set.

• The example in the above visual requests three columns, LASTNAME, SALARY, and EMPNO, from the EMPLOYEE table. Only rows which have an education level (EDLEVEL) of less than or equal to 16, a yearly salary (SALARY) in the range from 19,000 to 30,000 inclusive, a last name that starts with 'S', and a job of 'MANAGER', 'OPERATOR', or 'ANALYST' appear in the result. The result is sorted in ascending sequence by last name.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 32: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-8. Column functions CE1319.4

Notes:

A column function returns a single value for a group of rows. The column function operates on the column specified in the function argument and examines all the rows that satisfy the WHERE clause. If no GROUP BY is present, a column function returns a single value, no matter how many rows are read.

The main column functions are:

• SUM() Returns the total value.

• MIN() Returns the minimum value.

• AVG() Returns the average value.

• MAX() Returns the maximum value.

• COUNT(*) Returns the number of selected rows.

• COUNT(DISTINCT) Returns the number of unique values for a column name.

• COUNT(ALL) Returns the number of non-null values for a column name.

© Copyright IBM Corporation 2007, 2013

000120 O'CONNELL 29250.00 CLERK 14

000060 STERN 32250.00 MANAGER 16

000100 SPENSER 26150.00 MANAGER 14000130 QUINTANA 23800.00 ANALYST 16000280 SCHNEIDER 26250.00 OPERATOR 17000250 SMITH 19180.00 CLERK 15

EMPNO LASTNAME SALARY JOB EDLEVEL

EMPLOYEE TableSELECT SUM(SALARY) AS TOTAL, MIN(SALARY) AS MINIMUM,

MAX(SALARY) AS MAXIMUM,DECIMAL(AVG(SALARY),8,2) AS AVERAGE, COUNT(*) AS #EMP, COUNT(DISTINCT EDLEVEL) AS #LVL

FROM EMPLOYEE

Result 156880.00 4619180.00 32250.00 26146.66TOTAL MINIMUM MAXIMUM AVERAGE #EMP #LVL

Column functions

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-11

Page 33: Ce 1319 Stud

Student Notebook

.

Some newer column functions are:

• COUNT_BIG(*) Like COUNT(*) but result can be bigger than integer. STDDEV() Returns the standard deviation.

• VARIANCE() Returns the variance.

The example uses the function DECIMAL which is not a column function, but a scalar function. Scalar functions can be used whenever an expression can be used. They apply to all rows of the result rather than returning a value for a group of rows.

You can use SUM(), AVG(), VARIANCE() and STDDEV() only with values that are stored in columns defined as numeric. You can use MIN(), MAX(), COUNT(DISTINCT), COUNT_BIG(DISTINCT), COUNT(ANY), and COUNT_BIG(ANY) on any type of column. COUNT(*) and COUNT_BIG(*) do not have a column name as its argument so they can also be used on columns of any data type.

• Whenever an expression is used for a column of the result table, the column does not have a name unless an AS clause assigns one. AS clauses are useful in conjunction with column functions since they allow you to give the result of the column function a meaningful name.

• The example reads all rows of the table on the visual and determines the total yearly salary paid to all employees. The salary information includes the lowest yearly salary, the highest yearly salary, the average yearly salary, the number of employees, and the number of different education levels. In the result table, the corresponding columns are, in order, named TOTAL, MINIMUM, MAXIMUM, AVERAGE, #EMP, and #LVL.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-12 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 34: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-9. Summarizing group values CE1319.4

Notes:

GROUP BY is used to make DB2 summarize information for different groups within the data. For example, you can use GROUP BY to get the average salary for each department in a company.

The GROUP BY clause can identify one or more columns. When GROUP BY includes more than one column; there is one row in the result for each combination of values in the columns named in the GROUP BY clause.

When you use GROUP BY, the SELECT clause normally contains a mix of column names and column functions: the GROUP BY clause must specify all of the columns which are not arguments of the column functions in the SELECT clause.

The GROUP BY clause is optional. When used, it must follow the WHERE clause (or the FROM clause if there is no WHERE clause) and precede the HAVING clause (or the ORDER BY clause if there is no HAVING clause).

© Copyright IBM Corporation 2007, 2013

000120 O'CONNELL 29250.00 CLERK 14

000060 STERN 32250.00 MANAGER 16

000100 SPENSER 26150.00 MANAGER 14000130 QUINTANA 23800.00 ANALYST 16000280 SCHNEIDER 26250.00 OPERATOR 17000250 SMITH 19180.00 CLERK 15

EMPNO LASTNAME SALARY JOB EDLEVEL

EMPLOYEE Table

SELECT EDLEVEL, SUM(SALARY) AS GROUP_TOTALFROM EMPLOYEE GROUP BY EDLEVELHAVING COUNT(*) > 1

Result

EDLEVEL GROUP_TOTAL

14 55400.0016 56050.00

Summarizing group values

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-13

Page 35: Ce 1319 Stud

Student Notebook

.

Use HAVING to specify additional search conditions that the retrieved groups must satisfy. The HAVING clause acts like a WHERE clause for groups. It can contain the same kind of predicates as the WHERE clause.

A HAVING clause can also contain column functions like COUNT(*), unlike the WHERE clause which cannot directly use column functions. The search conditions in the HAVING clause test properties of each group rather than properties of individual rows in the group.

• The example reads the table in the graphic and produces a one-row summary for each of the education levels in the table. Each summary row contains the education level and the sum of the yearly salaries (GROUP_TOTAL) paid to people in the group. Education levels that have only one member are excluded from the result set with the HAVING clause.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-14 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 36: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-10. GROUP BY ROLLUP (DB2 for Linux, UNIX, and Windows only) CE1319.4

Notes:

GROUP BY ROLLUP is used when you need to analyze a collection of data in a single dimension, but at more than one level of detail. The example gives the average salary by EDLEVEL and the overall average salary.

GROUP BY ROLLUP is part of SUPER GROUPS. You usually employ these functions in online analytical processing, or OLAP, where there is a large collection of data spanning several dimensions.

GROUP BY ROLLUP can also be used on several levels. When you use the same EMPLOYEE table as above (restricted to certain employees only), you obtain following result:

© Copyright IBM Corporation 2007, 2013

000120 O'CONNELL 29250.00 CLERK 14

000060 STERN 32250.00 MANAGER 16

000100 SPENSER 26150.00 MANAGER 14000130 QUINTANA 23800.00 ANALYST 16000280 SCHNEIDER 26250.00 OPERATOR 17000250 SMITH 19180.00 CLERK 15

EMPNO LASTNAME SALARY JOB EDLEVEL

EMPLOYEE TableSELECT EDLEVEL, DECIMAL(AVG(SALARY),8,2)

AS AVG_SALARYFROM EMPLOYEE GROUP BY ROLLUP(EDLEVEL)

Result EDLEVEL

- (null)14151617

26146.6627700.0019180.0028025.0026250.00

EDLEVEL AVG_SALARY

GROUP BY ROLLUP (DB2 LUW only)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-15

Page 37: Ce 1319 Stud

Student Notebook

.

SELECT EDLEVEL, JOB, AVG(SALARY) AS AVG_SALARYFROM EMPLOYEE WHERE EMPNO IN('000060','000100','000120','000130','000250','000280')GROUP BY ROLLUP (EDLEVEL, JOB)------- -------- ---------------------------------EDLEVEL JOB AVG_SALARY------- -------- ---------------------------------- - 26146,66666666666666666666666614 - 27700,00000000000000000000000015 - 19180,00000000000000000000000016 - 28025,00000000000000000000000017 - 26250,00000000000000000000000014 CLERK 29250,00000000000000000000000014 MANAGER 26150,00000000000000000000000015 CLERK 19180,00000000000000000000000016 ANALYST 23800,00000000000000000000000016 MANAGER 32250,00000000000000000000000017 OPERATOR 26250,000000000000000000000000

11 record(s) selected.

In this example, we get the average salary for all employees, the average salary per EDLEVEL and the average salary per combination of EDLEVEL and JOB.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-16 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 38: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-11. GROUP BY CUBE (DB2 for Linux, UNIX, and Windows only) CE1319.4

Notes:

GROUP BY CUBE is used when you need to analyze a collection of data in a several dimensions. The example gives both average salary by EDLEVEL, by JOB, by the combination of EDLEVEL and JOB and the overall average salary.

The GROUP BY CUBE is similar to GROUP BY ROLLUP, but also includes all combinations of average salary:

• Average salary for all rows

• Average salary by EDLEVEL

• Average salary by JOB

• Average salary by the combination of EDLEVEL and JOB

© Copyright IBM Corporation 2007, 2013

000120 O'CONNELL 29250.00 CLERK 14

000060 STERN 32250.00 MANAGER 16

000100 SPENSER 26150.00 MANAGER 14000130 QUINTANA 23800.00 ANALYST 16000280 SCHNEIDER 26250.00 OPERATOR 17000250 SMITH 19180.00 CLERK 15

EMPNO LASTNAME SALARY JOB EDLEVEL

EMPLOYEE TableSELECT EDLEVEL,JOB,

DECIMAL(AVG(SALARY),8,2) AS AVG_SALARY

FROM EMPLOYEE WHERE EDLEVEL IN(14,15)GROUP BY CUBE(EDLEVEL,JOB)

Result

EDLEVEL

EDLEVEL JOB AVG_SALARY14 - 27700.0015 - 19180.00- - 24860.00

- CLERK 24215.00- MANAGER 26150.0014 CLERK 29250.0015 CLERK 19180.0014 MANAGER 26150.00

GROUP BY CUBE (DB2 LUW only)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-17

Page 39: Ce 1319 Stud

Student Notebook

.

Figure 1-12. Grouping function - Why? CE1319.4

Notes:

When using CUBE and ROLLUP it is sometimes difficult to tell what kind group the row represents. In the visual, there are two rows with NULL values in EDLEVEL. One of these rows represents the group that contains all rows selected in the WHERE clause, the other row represent the row with unknown (NULL) EDLEVEL.

© Copyright IBM Corporation 2007, 2013

000120 O'CONNELL 29250.00 CLERK 14

000060 STERN 32250.00 MANAGER 16

000100 SPENSER 26150.00 MANAGER 14000130 QUINTANA 23800.00 ANALYST 16000280 SCHNEIDER 26250.00 OPERATOR 17000250 SMITH 19180.00 CLERK -

EMPNO LASTNAME SALARY JOB EDLEVEL

EMPLOYEE Table

SELECT EDLEVEL, DECIMAL(AVG(SALARY),8,2)AS AVG_SALARY

FROM EMPLOYEE GROUP BY ROLLUP(EDLEVEL)

EDLEVEL

-14-1617

26146.6627700.0019180.0028025.0026250.00

EDLEVEL AVG_SALARY

Result

Grouping function – Why?

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-18 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 40: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-13. Grouping function - Example CE1319.4

Notes:

In CUBE and ROLLUP, the grouping function can be applied to any of the columns or expressions used inside CUBE or ROLLUP.

Grouping function gives 1 or 0 for each row. 1 means that this row represents a higher grouping, 0 means that grouping is on the same level as grouping function uses.

In the result-set above the second column represents grouping function. For the first row, the value 1 tells us that this row represents a higher grouping (aggregation) than grouping (EDLEVEL). This means in our example that the average salary on this row represents all employees. For all other rows, the value 0 indicates that each row is grouped by EDLEVEL level. The third row in the result set is the average salary for all employees with unknown (NULL) EDLEVEL.

This grouping function is useful together with CASE. We cover CASE in Unit 4.

© Copyright IBM Corporation 2007, 2013

000120 O'CONNELL 29250.00 CLERK 14

000060 STERN 32250.00 MANAGER 16

000100 SPENSER 26150.00 MANAGER 14000130 QUINTANA 23800.00 ANALYST 16000280 SCHNEIDER 26250.00 OPERATOR 17000250 SMITH 19180.00 CLERK -

EMPNO LASTNAME SALARY JOB EDLEVEL

EMPLOYEE TableSELECT EDLEVEL, GROUPING(EDLEVEL),DECIMAL(AVG(SALARY),8,2)

AS AVG_SALARYFROM EMPLOYEE GROUP BY ROLLUP(EDLEVEL)

-14-1617

26146.6627700.0019180.0028025.0026250.00

EDLEVEL AVG_SALARY

10000

Result

Grouping function – Example

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-19

Page 41: Ce 1319 Stud

Student Notebook

.

Figure 1-14. GROUP BY grouping sets CE1319.4

Notes:

Use the GROUPING SETS operator to list exactly the groups you want.

GROUP BY GROUPING SETS((JOB),(DAY(BIRTHDATE))) is equivalent to

• GROUP BY JOB

• GROUP BY DAY(BIRTHDATE)

If some of the grouping criteria include more than one column or expression, enclose those grouping criteria in parentheses. If you want an overall average salary, include a set of parentheses ().

Several grouping specifications can be combined in the same GROUP BY clause. The total number of groups produced by the GROUP BY clause is the product of the number of groups produced by each grouping specification.

© Copyright IBM Corporation 2007, 2013

SELECT JOB, DAY(BIRTHDATE), DECIMAL(AVG(SALARY),8,2) AS AVG_SALARY

FROM EMPLOYEE WHERE YEAR(BIRTHDATE) > 1938GROUP BY GROUPING SETS (JOB,DAY(BIRTHDATE))

Result

JOB AVG_SALARYCLERK

-24215.00

MANAGER 29200.00- 7 32250.00- 12 19180.00- 18 27700.00

DAY

000120 O'CONNELL 29250.00 CLERK 1942-10-18

000060 STERN 32250.00 MANAGER 1945-07-07

000100 SPENSER 26150.00 MANAGER 1956-12-18000130 QUINTANA 23800.00 ANALYST 1925-09-15000280 SCHNEIDER 26250.00 OPERATOR 1936-03-28000250 SMITH 19180.00 CLERK 1939-11-12

EMPNO LASTNAME SALARY JOB BIRTHDATE

EMPLOYEE Table

-

GROUP BY grouping sets

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-20 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 42: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-15. Super groups - Syntax CE1319.4

Notes:

This visual shows in a summary format the simplified syntax of the GROUP BY clause. For the complete syntax and detailed description of all the options, see the SQL Reference manual.

© Copyright IBM Corporation 2007, 2013

GROUP BY

ROLLUPCUBEGROUPING SETS

)(

,

( expr )( )

,

,expr

expr

Super groups – Syntax

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-21

Page 43: Ce 1319 Stud

Student Notebook

.

Figure 1-16. OLAP function for ranking (for DB2 z/OS and DB2 for Linux, UNIX, and Windows) CE1319.4

Notes:

To order the employees according to their salaries in a result table, where the employees are listed in ascending sequence by their employee number, use the RANK function. Online Analytical Processing (OLAP) functions permit returning ranking, row numbering and existing column function information as a scalar value in a query result.

The ranking function computes the ordinal rank of a row within the window. Rows that are not distinct with respect to the ordering within their window are assigned the same rank.

The results of ranking can be defined with or without gaps in the numbers resulting from duplicate values.

If RANK is specified, the rank of a row is defined as 1 plus the number of rows that strictly precede the row. Thus, if two or more rows are not distinct with respect to the ordering, then there is one or more gaps in the sequential rank numbering.

If DENSE_RANK is specified, the rank of a row is defined as 1 plus number of distinct rows that are distinct with respect to the ordering. Therefore, there are no gaps possible in the sequential rank numbering.

© Copyright IBM Corporation 2007, 2013

OLAP function for ranking (for DB2 z/OS and LUW)

EMPNO LASTNAME EDLEVEL DENSE_RANKING

------ --------------- ------- --------------------

000060 STERN 16 2

000100 SPENSER 14 4

000120 O'CONNELL 14 4

000130 QUINTANA 16 2

000250 SMITH 15 3

000280 SCHNEIDER 17 1

SELECT EMPNO, LASTNAME, EDLEVEL,

DENSE_RANK() OVER(ORDER BY EDLEVEL DESC)

FROM EMPLOYEE

ORDER BY EMPNO

EMPNO LASTNAME EDLEVEL ROW_NUMBER

------ --------------- ------- --------------------

000060 STERN 16 2

000100 SPENSER 14 5

000120 O'CONNELL 14 6

000130 QUINTANA 16 3

000250 SMITH 15 4

000280 SCHNEIDER 17 1

SELECT EMPNO, LASTNAME, EDLEVEL,

ROW_NUMBER() OVER(ORDER BY EDLEVEL DESC)

FROM EMPLOYEE

ORDER BY EMPNO

SELECT EMPNO, LASTNAME, EDLEVEL,

RANK() OVER(ORDER BY EDLEVEL DESC)

FROM EMPLOYEE

ORDER BY EMPNO

EMPNO LASTNAME EDLEVEL RANKING

------ --------------- ------- --------------------

000060 STERN 16 2

000100 SPENSER 14 5

000120 O'CONNELL 14 5

000130 QUINTANA 16 2

000250 SMITH 15 4

000280 SCHNEIDER 17 1

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-22 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 44: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

If ROW_NUMBER is specified, the sequential row number within ordering is shown. RANK, DENSE_RANK and ROW_NUMBER are supported on DB2 for Linux, UNIX, and Windows, now also on DB2 9 for z/OS.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-23

Page 45: Ce 1319 Stud

Student Notebook

.

Figure 1-17. Additional SQL in this course CE1319.4

Notes:

The visual lists the major topics we discuss during the rest of the course.

© Copyright IBM Corporation 2007, 2013

CREATE OBJECTS

SUBQUERY

PERFORMANCE

OUTER JOIN

TRIGGER

VIEWS

SCALAR FUNCTIONS

RECURSIVE SQL

CHECK CONSTRAINTS

TABLEEXPRESSIONS

CASEEXPRESSIONS

Additional SQL in this course

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-24 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 46: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 1-18. Checkpoint CE1319.4

Notes:

Write your answers here:

1.

2.

3.

4.

© Copyright IBM Corporation 2007, 2013

Checkpoint

1. List three operating systems that can run DB2.2. T/F. There is one DB2 catalog for an entire DB2 for z/OS

subsystem, which can contain many databases, and a separate DB2 catalog for each database in DB2 for Linux, UNIX, and Windows.

3. Assume that you need to list the minimum, the maximum, and the average salary in your company. Which functions do you use?

4. We have an EMPLOYEE table with one row per employee, which includes columns SALARY and UNION_CLASSIFICATION. Assume you have to prepare a report that lists each union classification on a line by itself,along with the total salary paid out to all employees in that union classification. Which clause will you need in your SQL statement to ensure that there is a separate line on the report for each union classification?

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 1. Introduction 1-25

Page 47: Ce 1319 Stud

Student Notebook

.

Figure 1-19. Unit summary CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Unit summary

Having completed this unit, you should be able to:• Identify the purpose of the clauses in the SELECT statement• Describe the key differences among the IBM DB2 platforms• Describe and use some of the OLAP features of DB2, such

as GROUPING functions like CUBE and ROLLUP, and the RANK, DENSE_RANK and ROW_NUMBER functions

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

1-26 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 48: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 2. Create Objects

What this unit is about

In this unit, you learn the process for creating objects.

Triggers and constraint checking can be used to reduce the number of business rule validations that must be coded in applications. The data and business rules are defined to the database manager. Thus, they are centralized, so that your applications do not have to perform these tasks.

What you should be able to do

After completing this unit, you should be able to:

• Code statements to:

- Create tables and views

- Alter tables

- Create indexes

- Implement referential integrity (RI)

- Define triggers and check constraints

• Identify impacts and advantages of referential integrity, including impacts of delete rules

• Identify considerations when using triggers and check constraints

• Identify the advantages of views

How you will check your progress

• Checkpointxclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-1

Page 49: Ce 1319 Stud

Student Notebook

.

Figure 2-1. Unit objectives CE1319.4

Notes:

Database administrators normally create objects. Still, it is useful to know something about the procedure so that you can discuss problems with your database administrator. Your employer can provide a test or personal environment so that you can develop your own databases or do testing.

Triggers and constraint checking can be used to reduce the number of business rule validations that must be coded in applications. The data and business rules are defined to the database manager. Thus, they are centralized, so that your applications do not have to perform these tasks.

© Copyright IBM Corporation 2007, 2013

Unit objectives

After completing this unit, you should be able to:• Code statements to:

– Create tables and views– Alter tables– Create indexes– Implement referential integrity (RI)– Define triggers and check constraints

• Identify impacts and advantages of referential integrity, including impacts of delete rules

• Identify considerations when using triggers and check constraints

• Identify the advantages of views

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 50: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-2. Scenario (Part 1) CE1319.4

Notes:

Bill and Susan have a small store. They want to implement a data model to administer his orders. The design was done by a consulting company. They need the following tables:

• An ORDER table with the following columns: ORDER_NO, ORDER_DATE, CUST_NO, and ORDER_REF. Each order, that is, row of the table, contains an order number (ORDER_NO), the date when the order was placed (ORDER_DATE), and a customer number (CUST_NO). The row does not need to have a reference to another order (ORDER_REF).

• An ORDER_ITEM table with the following columns: ORDER_NO, ART_NO, and ORDER_QTY. Each row of the table represents an ordered item and must contain the order number (ORDER_NO) of the associated order, the article number (ART_NO) of the ordered article, and the quantity ordered (ORDER_QTY).

• An ARTICLE table with the following columns: ART_NO, ART_NAME, and STOCK_QTY. Each row of the table represents an article sold by the company and must contain the article number for the article (ART_NO), the name for the article (ART_NAME), and the number of items in stock (STOCK_QTY).

© Copyright IBM Corporation 2007, 2013

How can we create a data model to

administer my orders?

Bill

We create tables:ORDER,

ORDER_ITEM,ARTICLE

Susan

Scenario (Part 1)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-3

Page 51: Ce 1319 Stud

Student Notebook

.

Bill asks Susan, a database administrator, for help in implementing the model in DB2.

Bill: “Susan, I need your help to implement a small data model.”

Susan: “Sure, what would you like?”

Bill: “I need to create a persistent data store for orders, articles, and order_items, as I earlier described to you.

Susan: “That is easy. I can create the tables for you....”

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 52: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-3. Creating tables and adding columns (1 of 2) CE1319.4

Notes:

Platform-dependent is the syntax for creating a table. In a DB2 for Linux, UNIX, and Windows environment, you can code CREATE TABLE.....IN tablespace-name. This code identifies the table space in which the table is created. If the IN clause is not specified, then the table is created in one of three possible default table spaces, depending on which of them exists. If none of the default table spaces exist, the CREATE fails.

In a z/OS environment, there are three valid approaches for creating a table:

• CREATE TABLE... IN database-name.tablespace-name. This approach puts the table in the specified table space of the specified database.

• CREATE TABLE... IN DATABASE database-name. This approach puts the table in the specified database and creates a partition-by-growth table space within that database to hold the table.

• CREATE TABLE.... If you specify neither a table space nor a database, a database is implicitly created with the name DSNxxxxx, where xxxxx is a five-digit number. A partition-by-growth table space is also implicitly created.

© Copyright IBM Corporation 2007, 2013

CREATE TABLE (ART_NO INTEGER NOT NULL,ART_NAME CHAR(20) NOT NULL,STOCK_QTY INTEGER NOT NULL)

IN DATABASE PRODDB

ARTICLE

CREATE TABLE (ORDER_NO INTEGER NOT NULL,ORDER_DATE DATE NOT NULL,CUST_NO SMALLINT NOT NULL)

ORDER

ALTER TABLE ADD ORDER_REF INTEGER

ORDER

-- z/OS only

Creating tables and adding columns (1 of 2)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-5

Page 53: Ce 1319 Stud

Student Notebook

.

Data types BLOB, CLOB, and DBCLOB are available in DB2 for z/OS and on DB2 for Linux, UNIX, and Windows.

Generally, you specify the columns of the table when you create the table. If you need to add a column to the table later, you can use the ALTER TABLE statement with the ADD option. The new column must be defined as nullable or with NOT NULL WITH DEFAULT since the table can already contain rows at that point in time. If the column was defined with NOT NULL, DB2 will not know which value to assign to the column for the existing rows. If the column is defined as nullable, DB2 assigns a value of null to the column for existing rows; if it is defined with NOT NULL WITH DEFAULT, the appropriate default value (system-defined or user-defined) is assigned to the column for existing rows.

• The first example creates a table named ORDER containing the three columns ORDER_NO, ORDER_DATE, and CUST_NO. The fourth column (ORDER_REF) for the table has been forgotten and is added later on. If created in DB2 for Linux, UNIX, and Windows, the table will go into one of three possible default table spaces, depending on which of these table spaces exist. If created in DB2 for z/OS, a partition-by-growth table space will be created in the implicitly created database DSNxxxxx, where xxxxx is a five-digit number, and the table will be put there.

• The second example adds the forgotten column ORDER_REF to the ORDER table. Fortunately, ORDER_REF need not always have a value so that it can be defined as nullable. If ORDER_REF always required a value, NOT NULL WITH DEFAULT would have to be used.

• The third example creates a table named ARTICLE containing three columns. This example could only be executed in DB2 for z/OS because the IN DATABASE clause is not supported in DB2 for Linux, UNIX, and Windows. DB2 will create a partition-by-growth table space in the PRODDB database and store the ARTICLE table there.

Note

In recent versions of DB2 for Linux, UNIX and Windows, and DB2 for z/OS, you can now use the ALTER TABLE statement to ALTER columns (to change data types, as long as they are compatible). You can also run a RENAME TABLE statement. In recent versions of DB2 for Linux, UNIX and Windows, you can now also use the ALTER TABLE statement to DROP columns (you will have to consider how this affects dependent objects on the table, for example, indexes, triggers, if you do this). In recent versions of DB2 for z/OS, you can also use the ALTER table statement to RENAME a column.

Refer to the appropriate SQL Reference for more details on the implementation.Exc

lusivo

form

ación

proy

ecto

C.F.T.I.C

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

2-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 54: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-4. Creating tables and adding columns (2 of 2) CE1319.4

Notes:

• This visual shows alternative syntax for creating a new table by taking the definitions from an existing table.

• CREATE TABLE....LIKE creates a new table with the same column definitions (column name, data type and null attribute) as the existing table.

- No unique constraints, foreign key constraints, triggers, or indexes are created.

- The table is not populated.

- This syntax is available in DB2 z/OS and DB2 for Linux, UNIX, and Windows.

• CREATE TABLE --- WITH NO DATA uses a query to define a new table.

- The table is not populated using the result of query and the REFRESH TABLE statement cannot be used.

- Although the syntax has some similarity to the creation of a Materialized Query Table, it does not in fact create an MQT. (We will cover Materialized Query Tables briefly in a later unit.)

© Copyright IBM Corporation 2007, 2013

CREATE TABLE NEW_ORDERLIKE OLD_ORDER;

CREATE TABLE NEW_STAFFAS

(SELECT COL,COL2,COL5 FROM STAFF)WITH NO DATA;

Creating tables and adding columns (2 of 2)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-7

Page 55: Ce 1319 Stud

Student Notebook

.

- The columns of the table are defined based on the definitions of the columns that result from the fullselect. If the fullselect references a single table in the FROM clause, the default option is EXCLUDING COLUMN DEFAULTS, the opposite of CREATE TABLE...LIKE. It is possible however to specify INCLUDING COLUMN DEFAULTS.

• The columns of the table are defined based on the definitions of the columns that result from the fullselect. If the fullselect references a single table in the FROM clause, select list items that are columns of that table are defined using the column name, data type, nullability characteristic and column default value of the referenced table.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 56: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-5. Scenario (Part 2) CE1319.4

Notes:

Bill is concerned. His warehouse is quite small and he does not want one customer to order a quantity that will reduce his stock so other customer orders can not be handled.

Bill: “I am pleased with the tables you created, but I have some special requirements. As you know, our warehouse is quite small. I want to ensure that we never accept an order for more than 100 of any article regardless of its article number. Since articles with an article number of 50000 and higher are very large, I want to make sure that we never accept an order for more than 10 of these items.

Susan: “That is easy. I will just add some Check Constraints to the tables for you.”

Bill: “And Susan, please make sure that there are no duplicate values in the key elements of the table.”

Susan: “OK, no problem.”

© Copyright IBM Corporation 2007, 2013

How can we limit the value of the ordered

quantity for each item not to exceed 100 ?

We will implementCheck Constraints!

Bill Susan

Scenario (Part 2)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-9

Page 57: Ce 1319 Stud

Student Notebook

.

Figure 2-6. Check constraints - The need CE1319.4

Notes:

• Many data rules are specific to the column values in a single table. Check constraints can be used to define such rules to the DB2 database manager. The database managerwill then enforce these rules.

• Constraints will be checked during INSERT and UPDATE operations.

• Installations requiring such data checking should consider using constraints rather than enforcing data content via applications. Constraints are defined once, directly within DB2, and are applied globally, eliminating the need for applications to do this work.

• The left side of the graphic shows applications that contain rules coded within the applications themselves. Unless the code for these rules is made common to the applications via modularization or copy books, there is a risk that, over time, the rules in the applications will start to diverge and function differently and, therefore, inconsistently, which could cause serious problems.

• The right side of the graphic shows a situation where the rules are in DB2 itself, rather than in the applications. Since there is only one copy of each rule, there is no possibility

© Copyright IBM Corporation 2007, 2013

No value in the ORDER_QTY column in the table ORDER_ITEM may exceed 100.

BUSINESS RULE:

ENFORCEMENT

APPLICATION

DATABASE

PGM n

RULE

DB2

PGM 1

RULE

PGM 2

RULE

PGM n

PGM 1 PGM 2

DB2

RULE

Check constraints – The need

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 58: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

of rule inconsistencies emerging over time. The absence of rules makes the applications smaller, which makes them easier to code and test.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-11

Page 59: Ce 1319 Stud

Student Notebook

.

Figure 2-7. Check constraints CE1319.4

Notes:

• Single-column constraints can be defined at the column level or the table level. Multi-column constraints must be defined at the table level.

• Check constraints can be defined on a new table with the CREATE TABLE statement.

• Check constraints can be added to an existing table with the ALTER TABLE statement.

• Constraints can include the basic WHERE clause constructs:

- Basic comparisons (>, <, =, >=, etc.) - BETWEEN - LIKE - IN

• Constraints can NOT include:

- Subqueries - Column functions - Special registers (such as CURRENT DATE) - On z/OS, the NOT logical operator

© Copyright IBM Corporation 2007, 2013

CREATE TABLE ORDER_ITEM(ORDER_NO INTEGER NOT NULL,ART_NO INTEGER NOT NULL,ORDER_QTY SMALLINT NOT NULL

CONSTRAINT order_limitCHECK(ORDER_QTY <= 100),

CONSTRAINT special_orderCHECK(ART_NO <= 50000 OR

ORDER_QTY <= 10) )

ALTER TABLE ORDER_ITEMDROP CONSTRAINT special_order

ALTER TABLE ORDER_ITEMADD CONSTRAINT special_order

CHECK(ART_NO < 50000 ORORDER_QTY <= 10)

Check constraints

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-12 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 60: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

See the SQL Reference for your platform for full details.

• The constraint can be explicitly named when it is defined. If it is not named, DB2 will create a name. Explicit naming is also possible on the ALTER TABLE statement by providing the CONSTRAINT clause before the CHECK specification, just as in the example above.

• Constraints can also be removed by means of the ALTER TABLE statement as illustrated in the above visual.

• The example implements the constraints that Bill requested:

- In the CREATE TABLE statement, the order quantity is constrained at the column level so that it never exceeds 100, regardless of the article number. At the table level, a second constraint named 'special_order' ensures that the order quantity does not exceed 10 if the article number is greater than or equal to 50000: it checks the row and makes sure the article number is less than or equal to 50000 or, if it is not, verifies that the order quantity is less than or equal to 10.

• The constraint implemented on the table level in the CREATE TABLE statement is not quite what Bill had in mind since it would accept orders of more than 10 items for the article with article number 50000. To remedy the problem, we need to drop constraint 'special_order' and add it again by means of the ALTER TABLE statement.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-13

Page 61: Ce 1319 Stud

Student Notebook

.

Figure 2-8. Scenario (Part 3) CE1319.4

Notes:

Bill: “Susan, I need your help again. I have another requirement for my new DB2 system.”

Susan: “No problem. What is up?”

Bill: “As I tested my system, I encountered some problems. There are order items in the ORDER_ITEM table whose order numbers are not in the ORDER table and whose article numbers are not in the ARTICLE table. Also, there are orders in the order table that refer to orders that do not exist. I am not sure how this happened, but we have to prevent it from

happening in the production system.”

Susan: “Sure, we have to implement referential integrity. What relationships do you have between your tables?”

Bill: “When creating a new row in the ORDER_ITEM table, the order number should already exist in the ORDER table and the article number should already exist in the ARTICLE table. If I delete an order in the ORDER table, all rows in the ORDER_ITEM table that refer to that order should be deleted automatically.”

© Copyright IBM Corporation 2007, 2013

How can we avoidorder_items for non-

existing orders?We will implement

Referential Integrity!

Bill Susan

Scenario (Part 3)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-14 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 62: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

“It should not be possible to delete a row in the ARTICLE table if the ORDER_ITEM table has rows which refer to this article. When creating a new row in the ORDER table, the ORDER_REF column can refer to another order of the ORDER table. This order must already exist.”

Susan: “OK, no problem.”

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-15

Page 63: Ce 1319 Stud

Student Notebook

.

Figure 2-9. Create index and primary key CE1319.4

Notes:

• If you are searching for names in a card file and you have no index, you have to read the complete card file. DB2 must also read (scan) a complete table if no index is specified on a column used in a predicate of a WHERE clause.

• An index is an ordered collection of the key values in the column on which the index is defined. Each key value has a RID (Row IDentifier) or RIDs that points to the actual data row(s) which contain the key value.

• A table can have many indexes. Each index can span one column or several columns. Each index can be unique or nonunique.

• The usefulness of an index depends on its key. Columns that you use frequently in WHERE clauses, joins, grouping and ordering operations are good candidates for indexes.

• DB2 indexes allow duplicate values by default. If you want to prevent duplicate values in a key column, use CREATE UNIQUE INDEX.

© Copyright IBM Corporation 2007, 2013

CREATE UNIQUE INDEX ORDER_INDON ORDER (ORDER_NO)

ORDER_NO: 01910

ALTER TABLE ORDER ADD PRIMARY KEY (ORDER_NO)

Create index and primary key

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-16 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 64: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• The example creates a unique index called ORDER_IND on the ORDER_NO column of the ORDER table. This satisfies one of Bill's requirements.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-17

Page 65: Ce 1319 Stud

Student Notebook

.

Figure 2-10. Referential integrity (Part 1) CE1319.4

Notes:

• The above example shows the relationships between the ORDER table and the ORDER_ITEM table on one hand and between the ARTICLE table and the ORDER_ITEM table on the other hand.

• The ORDER table has a primary key on the ORDER_NO column. The values in this column must be unique and not null.

• The ARTICLE table has a primary key on the ART_NO column. The values in this column must be unique and not null.

• The ORDER_ITEM table has a primary key on the combination of the ORDER_NO and ART_NO columns. The values in the primary key must be unique and not null. In other words, the combination of ORDER_NO and ART_NO must be unique and not null in each row of the table.

• The ORDER and ARTICLE tables are parent tables. For both parent tables, the ORDER_ITEM table is the dependent table. ORDER_NO in the ORDER_ITEM table is a foreign key, which means its value must exist in the ORDER table's primary key when

© Copyright IBM Corporation 2007, 2013

Foreign Key

Primary Key

Primary Key

ORDER_NO ART_NO ORDER_QTY

22333

22333

40000

42500

2

8

35555 40000 1

ORDER_ITEMDependent

Table

ORDERParentTable

ORDER_NO ORDER_DATE ORDER_QTY ORDER_REF

22333

22444

35555

ARTICLEParentTable

ART_NO ART_NAME STOCK_QTY

40000

42500

75500

Foreign Key

Primary Key

Referential integrity (Part 1)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-18 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 66: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

the ORDER_ITEM row is created. ART_NO in the ORDER_TABLE is also a foreign key, which means its value must exist in the ARTICLE table's primary key when the ORDER_ITEM row is created.

• DB2 permits one exception to the rule that a foreign key value must match a value in the corresponding primary/unique key: If desired by the user, a foreign key can also be null, even though a primary/unique key will never contain a null. In this example, the foreign keys are both part of a primary key so they will not be allowed to contain nulls.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-19

Page 67: Ce 1319 Stud

Student Notebook

.

Figure 2-11. Referential integrity - Keys CE1319.4

Notes:

A unique key is a key (set of columns) that is constraint so that no two values are equal. The columns of a unique key cannot contain null values.

• A primary key is a special case of a unique key. It is normally used to refer to the rows of the appropriate table.

• A table can have multiple unique keys, but cannot have more than one primary key. A key can either be defined as a unique key or as a primary key, but not both. Primary keys and unique keys can be defined using CREATE TABLE or ALTER TABLE.

• By definition, the values of a foreign key always match the values of a primary key or unique key.

• A referential constraint is a rule, enforced by the database manager, that controls the relationship between a primary key/unique key and a foreign key. Referential constraints are defined to DB2 when the foreign key is defined. They are enforced when rows containing a foreign-key value are inserted, updated, or loaded.

© Copyright IBM Corporation 2007, 2013

Referential integrity – Keys

• A PRIMARY KEY value must be UNIQUE and cannot beNULL

• By definition, a FOREIGN KEY matches a PRIMARY KEYor a UNIQUE KEY or can be null

• DB2 enforcement of referential constraints occurs during insert/update/load of a FOREIGN KEY

• DB2 ensures that a PRIMARY KEY/UNIQUE KEY is updated only if no FOREIGN KEY matches the original value of the primary/unique key

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-20 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 68: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• DB2 prevents the updating of primary key/unique key values if there are matching foreign key values. The new values are not propagated to the appropriate rows of the dependent table. In other words, a primary key/unique key value can only be updated if no foreign key value matches the original value of the primary/unique key.

If there is a real need to change an existing Primary Key value that has Foreign Keys “dependent” upon it, proceed as follows:

1. Create the “new” PK value in a new table row.

2. Update the FK’s to match this new PK value.

3. Delete the row with the original PK value.

Note

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-21

Page 69: Ce 1319 Stud

Student Notebook

.

Figure 2-12. Referential integrity - DELETE rules CE1319.4

Notes:

• The delete rules are defined in the REFERENCES clause of the foreign key definition in the CREATE TABLE or ALTER TABLE statement.

• The only difference between NO ACTION and RESTRICT is when the referential constraint is enforced. RESTRICT enforces the rule immediately and NO ACTION enforces the rule at the end of the statement.

© Copyright IBM Corporation 2007, 2013

Problem:When deleting a primary/unique key, what should be done torows that contain matching foreign keys?

DELETE Rules:CASCADE - Delete the rows with the matching values in the dependent table

SET NULL - Change the matching values in the dependent table to null

RESTRICT or NO ACTION - Disallow the DELETE in the parent table if matching values exist in the dependent table

Referential integrity – DELETE rules

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-22 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 70: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-13. ALTER TABLE - Adding referential constraints CE1319.4

Notes:

• The examples illustrate how to implement the relationships between the ORDER table and the ORDER_ITEM table and between the ARTICLE table and the ORDER_ITEM table.

• DB2 for z/OS usually requires that a unique index has been defined for the columns for the primary key before the primary key can be added by means of the ALTER TABLE statement. In DB2 for Linux, UNIX, and Windows, if not provided, the unique index is defined automatically.

• ALTER TABLE can also be used to add or drop check constraints and to add a column to an existing table as we have seen before.

© Copyright IBM Corporation 2007, 2013

ALTER TABLE ORDERADD PRIMARY KEY (ORDER_NO)

ALTER TABLE ARTICLEADD PRIMARY KEY (ART_NO)

ALTER TABLE ORDER_ITEMADD PRIMARY KEY (ORDER_NO, ART_NO)

ALTER TABLE ORDER_ITEMADD CONSTRAINT IN_ORDER

FOREIGN KEY(ORDER_NO)REFERENCES ORDERON DELETE CASCADE

ALTER TABLE ORDER_ITEMADD CONSTRAINT OF_ARTICLE

FOREIGN KEY(ART_NO)REFERENCES ARTICLEON DELETE RESTRICT

ALTER TABLE – Adding referential constraints

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-23

Page 71: Ce 1319 Stud

Student Notebook

.

Figure 2-14. Referential integrity (Part 2) CE1319.4

Notes:

• The primary/unique key does not necessarily have to be in a table other than the one for the foreign key. It can be in the same table. T he non-null values in column ORDER_REF of the ORDER table must have a counterpart in the ORDER_NO column of the ORDER table. Thus, ORDER_REF represents a foreign key in the same table as the associated primary key.

• If the foreign key is in the same table as the corresponding primary key, the delete rule for the referential constraint must be CASCADE or NO ACTION. It cannot be SET NULL or RESTRICT. This can have an impact on the programs for the application.

• In the example above, order 22444 refers to order 22333 and order 35555 to order 22444. The delete rule chosen is NO ACTION which, for example, prevents the deletion of order 22333 because order 22444 depends on it. Since order 35555 has no dependents, it can be deleted. If the delete rule was CASCADE, the deletion of order 22333 would cause the deletion of order 22444 which, in turn, would cause the deletion of order 35555.

© Copyright IBM Corporation 2007, 2013

Primary Key Foreign Key

ORDERParent andDependent

Table

ORDER_NO ORDER_DATE ORDER_QTY ORDER_REF2233322444

35555

22333

22444

Delete rule for self-referencing constraint can only beCASCADE or NO ACTION

ALTER TABLE ORDERADD FOREIGN KEY(ORDER_REF)

REFERENCES ORDERON DELETE NO ACTION

Referential integrity (Part 2)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-24 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 72: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• There also exist restrictions for other, more complex, referential structures. However, their discussion is outside the scope of this course.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-25

Page 73: Ce 1319 Stud

Student Notebook

.

Figure 2-15. Information integrity constraints CE1319.4

Notes:

• DB2 allows for the creation of RI constraints on tables which are not enforced by the database manger, but can still be exploited by the optimizer.

• Using this feature can lead to increased query performance in queries without increasing response times for INSERT-, UPDATE- or DELETE- statements.

• It is possible to switch a constraint “on” and “off” by modifying the attributes of the referential constraint.

• Constraint attributes:

- ENFORCED (default): The constraint is enforced by the database manager.

- NOT ENFORCED: The constraint is not enforced by the database manager. It should only be used for query optimization.

- ENABLE QUERY OPTIMIZATION (default): The constraint is used for query optimization.

© Copyright IBM Corporation 2007, 2013

Referential IntegrityConstraints

Information integrity constraints

a performance option

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-26 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 74: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

- DISABLE QUERY OPTIMIZATION: The constraint cannot be used for query optimization.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-27

Page 75: Ce 1319 Stud

Student Notebook

.

Figure 2-16. Scenario (Part 4) CE1319.4

Notes:

Bill: “Susan, I am very happy. I created a table by myself. It is a table for orders to my suppliers. The name of the table is SUP_ORDERS. Everything works fine except for a last problem.”

Susan: “How can I help?”

Bill: “I want DB2 to automatically insert a row in the SUP_ORDERS table if the stock quantity of an article in the ARTICLE table decreases to less than 50. I want the new row in the SUP_ORDERS table to contain the article number and the current stock on that article. Is that possible?”

Susan: “Sure, we can use a trigger.”

Bill: “That is great.”

© Copyright IBM Corporation 2007, 2013

Can DB2 automatically

create reorder information?

Sure, we can use triggers!

BillSusan

Scenario (Part 4)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-28 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 76: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-17. Triggers - The need CE1319.4

Notes:

A trigger is a set of actions that will be executed when a certain “change” SQL statement occurs. The SQL statements that change the data contents of the table are:

• INSERT

• UPDATE

• DELETE

Triggers are defined for a specific table and once defined, a trigger is automatically active. Trigger definitions are stored in the system catalog tables. Therefore, whatever application process issues the SQL that invokes the trigger, the triggering actions are consistently applied.

Triggers can be defined to DB2 so that the database manager automatically enforces business rules that should follow the triggering event operation against the specified table.

There are many benefits when you let DB2 enforce such rules:

• You eliminate the need to code programs to perform this task.

© Copyright IBM Corporation 2007, 2013

PGM n

DB2ENFORCEMENT

APPLICATION

DATABASE

BUSINESS RULES:

INSERT, UPDATE,and DELETEactions against atable may requireanother action orset of actionsto occur to meetthe needs of thebusiness.

When value in the ORDER_QTY column gets below 50, then insert reorder information into table SUP_ORDERS.

BUSINESS RULE:

Triggers – The need

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-29

Page 77: Ce 1319 Stud

Student Notebook

.

• Rules are defined once and enforced globally.

• If business requirements change, you can easily change the rules.

Some of the uses of a trigger include:

• Data Validation - Ensures that a new data value is within the proper range. This is similar to table-check constraints, but is a more flexible data validation mechanism.

• Data Conditioning - Implemented using triggers that fire before data record modification. This allows the new data value to be modified or conditioned to a predefined value.

• Data Integrity - Can be used to ensure that cross-table dependencies are maintained.

The triggered action could involve updating data records in related tables. This is similar to referential integrity, but is a more flexible alternative.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-30 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 78: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-18. CREATE TRIGGER (1 of 2) CE1319.4

Notes:

Triggers can be defined with the CREATE TRIGGER statement.

In the example above:

• AFTER UPDATE OF STOCK_QTY ON ARTICLE indicates that a triggered action should take place after the STOCK_QTY column in has been updated in the ARTICLE table. The triggered action can be made to occur either before or after an event. The event can be an INSERT, an UPDATE of a specific column or columns, an UPDATE of any column of a table, or a DELETE.

• REFERENCING is used to correlate the event with the triggered action. In this case, we want the row that is being added to the SUP_ORDERS table in the triggered action to contain the values in the ART_NO and STOCK_QTY columns, but we want these to be the values after the update of the ARTICLE table. In other words, we want the new values, not the old values.

We use the REFERENCING clause to designate the new values with a correlation name of N. We also use the REFERENCING clause to designate the old values with a

© Copyright IBM Corporation 2007, 2013

CREATE TRIGGER re_orderAFTER UPDATE OF STOCK_QTY ON ARTICLEREFERENCING NEW AS N OLD AS OFOR EACH ROWMODE DB2SQLWHEN (N.STOCK_QTY < 50 AND

O.STOCK_QTY >= 50)INSERT INTO SUP_ORDERS

VALUES(N.ART_NO, N.STOCK_QTY)

DROP TRIGGER re_order

Triggered action can contain INSERT, searchedUPDATE or DELETE, and single row SELECT

SQL extensions, such as CASE expressions, canbe exploited

CREATE TRIGGER (1 of 2)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-31

Page 79: Ce 1319 Stud

Student Notebook

.

correlation name of O. We want to ensure that the trigger is fired only if the old stock quantity is greater than or equal to 50 and the new stock quantity is less than 50. These variables defined by the REFERENCING clause are called transition variables.

• FOR EACH ROW indicates that the trigger should fire for each row inserted. FOR EACH STATEMENT, which causes the trigger to fire once per triggering statement, is another option. The difference between these two options should be obvious if you consider an INSERT that adds more than one row to a table:

If FOR EACH ROW is used, the triggered action will take place once for each row updated.

If FOR EACH STATEMENT is used, the triggered action will be performed only once, no matter how many rows are updated.

• MODE DB2SQL identifies the mode of the trigger.

• WHEN restricts the triggered action. The action will only be executed if the condition of the WHEN evaluates to TRUE. It is important that the triggered action occur only the first time the stock quantity goes below 50. If the WHEN condition was 'N.STOCK._QTY < 50', the trigger would be fired every time the value went below 50, even if it had previously been below 50. Each time the trigger was fired, we would place an additional order for the product and would soon be flooded with new orders of the product.

• INSERT INTO SUP_ORDERS... is the triggered action. Note that it takes advantage of the correlation name established with the REFERENCING clause.

Triggers can be removed with the DROP TRIGGER statement. Any packages that have a dependency on the trigger will be marked invalid. Consult the SQL Reference for more details about the variations possible within a CREATE TRIGGER statement.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-32 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 80: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-19. CREATE TRIGGER (2 of 2) CE1319.4

Notes:

This visual shows how to create a trigger which performs several SQL-statements.

• The significance of the required ATOMIC keyword is that the set of SQL statements is treated as an atomic unit. That is, either all of the statements are executed or none. If, for example, the second UPDATE statement shown in the visual fails, all changes made to the database as part of the triggering operation are backed out.

• REFERENCING is used to correlate the event with the triggered action. In both row and statement triggers it might be necessary to refer to the whole set of affected rows. For example, triggered SQL statements might need to apply aggregations over the set of affected rows (MIN, MAX, or AVG of some column values). A trigger can refer to the set of affected rows by using transition tables that can be specified in the REFERENCES clause of the CREATE TRIGGER statement. The columns in transition tables are referred to using the column names of the triggering table. As with transition variables, there are two kind of transition tables, which are specified as OLD_TABLE and NEW_TABLE together with a table-name.

© Copyright IBM Corporation 2007, 2013

CREATE TRIGGER newprojAFTER INSERT ON PROJECTREFERENCING NEW AS N_ROWFOR EACH ROW MODE DB2SQLBEGIN ATOMIC

UPDATE DEPARTMENT SET PROJ_COUNT = PROJ_COUNT+1 WHERE DEPTNO=N_ROW.RESP_DEPT;

CALL MY_PROC(N_ROW.RESP_DEPT,N_ROW.PROJNAME);

UPDATE EMPLOYEE SET PROJ_COUNT=PROJ_COUNT+1WHERE EMPNO=N_ROW.RESP_EMP;

END

CREATE TRIGGER (2 of 2)U

nit o

f Wor

k

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-33

Page 81: Ce 1319 Stud

Student Notebook

.

Here is an example of using a transitional table:

CREATE TRIGGER LARG_ORDAFTER INSERT ON INVOICEREFERENCING NEW_TABLE AS N_TABLEFOR EACH STATEMENT MODE DB2SQLSELECT LARGE_ORDER_ALERT(CUST_NO,TOTAL_PRICE,DELIVERY_DATE)FROM N_TABLE WHERE TOTAL_PRICE > 20000

This example shows the use of a transition table in a statement trigger. The UDF named LARGE_ORDER_ALERT will be invoked for each row in the transition table that corresponds to an order worth more that 20000. This example also shows the use of a SELECT statement to invoke a user-defined function for each row in a transition table.

A new transition table always has the full set of updated rows, even if referenced in an after row trigger. That is, all updated or inserted rows are included, even when an after row trigger is activated for the first row. Transition tables are read-only.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-34 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 82: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-20. MERGE data into tables CE1319.4

Notes:

In addition to SQL INSERT, UPDATE and DELETE for adding, updating and deleting rows there is also the MERGE statement.

In the above example, the EMP_UPDATE table has a row that already exists on EMP_COPY ('000030', the ON checks for a match on EMPNO).

• Therefore, when a match is found, the WORKDEPT for the EMP_COPY row is updated.

• Where there is a row in EMP_UPDATE that does not exist already on EMP_COPY (there are two here, '000050' and '000060') they are not matched, and are therefore Inserted into the EMP_COPY table instead.

The MERGE statement is sometimes referred to as an “UPSERT” … that is, UPDATE + INSERT = UPSERT.

© Copyright IBM Corporation 2007, 2013

MERGE data into tables

EMPNO LASTNAME WORKDEPT

------ --------------- --------

000010 HAAS A00

000020 THOMPSON B01

000030 KWAN Z99

000050 GEYER E01

000060 STERN D11

EMP_COPY after MERGE:

MERGE INTO EMP_COPY A USING (SELECT * FROM EMP_UPDATES) B

ON (A.EMPNO = B.EMPNO)

WHEN MATCHED THEN

UPDATE SET A.WORKDEPT = B.WORKDEPT

WHEN NOT MATCHED THEN

INSERT (EMPNO, LASTNAME, WORKDEPT)

VALUES(B.EMPNO, B.LASTNAME, B.WORKDEPT)

EMP_UPDATES:EMPNO LASTNAME WORKDEPT

------ --------------- --------

000030 KWAN Z99

000050 GEYER E01

000060 STERN D11

EMP_COPY before MERGE:

EMPNO LASTNAME WORKDEPT

------ --------------- --------

000010 HAAS A00

000020 THOMPSON B01

000030 KWAN C01

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-35

Page 83: Ce 1319 Stud

Student Notebook

.

Figure 2-21. View considerations CE1319.4

Notes:

A view is a named specification of a result table. The specification is an SQL SELECT statement that is effectively executed whenever the view is referenced in an SQL statement.

The data needed to satisfy queries is often stored in many large tables. However, we often need only a small subset of this data in order to create reports. A view can act like a filter so that a report contains only the information we need, rather than a lot of extra detail.

A view can be used to:

• Make a subset of a table's data available to its users. For example, a view based on an employee table might contain only some of the rows and columns of the table, such as employee number, last name, birth-date, and salary of certain employees in departments A00 and C01.

• Show data that was obtained by joining two or more tables as if it were a single table. A view that is based on a join cannot be updated.

© Copyright IBM Corporation 2007, 2013

View000010

1949-02-21

QUINTANA

52750.00C01

MANAGER

INFORMATION CENTERANALYSTA00

000030

1939-06-21

PLANNING

D01HAAS

SPIFFY COMPUTER SERVICE DIV.B01

23800.00

KWAN

000130

38259.00

HAAS A00 SPIFFY COMPUTER SERVICE DIV.QUINTANA C01 INFORMATION CENTER

A View Definition cannot include: ORDER BY

View considerations

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-36 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 84: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• Display data with different column names and sequences than those which appear in the base tables.

• Display derived data, including the results of any function or operation that you can use in a SELECT statement. For example, a view can be written to display a list of the departments with the average salary of the people in that department without showing the details of the employees in that department.

• Control access to data. Usually, the user will be granted access only to the view. No access to the underlying table(s) is granted in most cases. This has the effect of hiding the other columns and rows from those who do not need to see them.

You cannot modify an existing view, but you can drop it and create a new one. Dropping and creating views does not affect the base tables or the data in those tables. However, when INSERTs, UPDATEs and DELETEs are issued against a view, the data in the underlying tables is affected.

Note

A view does not contain any data. It is simply a logical table or virtual table that points to rows in real tables. As a result, the data in the view is always current. There is no need to do anything to keep the view synchronized with the table and no extra storage space is needed for the rows in the view.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-37

Page 85: Ce 1319 Stud

Student Notebook

.

Figure 2-22. Sample view CE1319.4

Notes:

• Assume a user needs the last names of the employees in the EMPLOYEE table shown on the visual and, for each employee, also the name of his/her department. However, the user should not be able to see the salaries of the employees. The department name must be obtained by joining to the DEPARTMENT table.

• When a view is created, a name is assigned to the view. The name has the same format as table names. It can be used to refer to the view in other SQL statements.

• The column list, which appears in parentheses after the view name, can be used to give the columns from the base tables different names in the view or to name calculated columns.

• The user of a view needs only the authority to access the view. Access to the original table is not needed. It is possible to give a user SELECT authority on a view without granting any authority on the base table.

© Copyright IBM Corporation 2007, 2013

EMPLOYEE TableEMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

DEPARTMENT TableDEPTNAMEDEPTNO MGRNO

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE DIV.

D01

A00C01 INFORMATION CENTER

000010000030

CREATE VIEW EMP_DEPT (NAME, DEPARTMENT)AS SELECT LASTNAME, DEPTNAME

FROM EMPLOYEE, DEPARTMENTWHERE WORKDEPT = DEPTNO

SELECT * FROM EMP_DEPT;

NAME DEPARTMENT

HAASO'CONNELLKWANNICHOLLSQUINTANA

SPIFFY COMPUTER SERVICE DIV.SPIFFY COMPUTER SERVICE DIV.INFORMATION CENTERINFORMATION CENTERINFORMATION CENTER

EMP_DEPT View

Sample view

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-38 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 86: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• The CREATE VIEW statement shown on the visual describes the data that should be made available. It does not make the data available. To see the data described by the view, the statement:

SELECT * FROM EMP_DEPT

can be used which refers to the view rather than to the base tables. It displays the full contents of the view. The join result is now treated as if it were a single table. There is no need to specify the join condition, control the columns or rows returned, or do any other SQL clause except ORDER BY, because the view definition established all of the conditions that were needed.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-39

Page 87: Ce 1319 Stud

Student Notebook

.

Figure 2-23. View hierarchy CE1319.4

Notes:

Views can be based on one or more tables. Views can also be based on other views or views joined with tables. The views upon which a view is based must have previously been defined.

In the example above, EMPLOYEE, DEPARTMENT, and PROJECT are tables. PROJ_DEPT is a view based on an inner join of tables PROJECT and DEPARTMENT. It could be created by the following CREATE VIEW statement:

CREATE VIEW PROJ_DEPTAS SELECT PROJNAME, DEPTNAME, MGRNOFROM PROJECT P INNER JOIN DEPARTMENT DON P.DEPTNO = D.DEPTNO

© Copyright IBM Corporation 2007, 2013

DEPARTMENT Table

DEPTNAMEDEPTNO MGRNO

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE DIV.

D01

A00C01 INFORMATION CENTER

000010000030

PROJECT Table

PROJNAMEPROJNO RESPEMPDEPTNO

USER EDUCATION

ADMIN SERVICES

IF2000

AD3100IF1000 QUERY SERVICES

000010000030000030

D01C01C01

DEPARTMENTPROJECT MANAGER

ADMIN SERVICES

USER EDUCATIONQUERY SERVICES

SPIFFY COMPUTER SERVICE DIV.

INFORMATION CENTERINFORMATION CENTER

KWANKWAN PROJ_DEPT_MGR View

EMPLOYEE Table

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

DEPTNAMEPROJNAME MGRNO

ADMIN SERVICES

USER EDUCATIONQUERY SERVICES

SPIFFY COMPUTER SERVICE DIV.

INFORMATION CENTERINFORMATION CENTER

000030000030

PROJ_DEPT View

View hierarchy

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-40 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 88: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

PROJ_DEPT_MGR is a view that is based on a left outer join of view PROJ_DEPT to table EMPLOYEE. Its definition could be as follows:

CREATE VIEW PROJ_DEPT_MGR (PROJECT, DEPARTMENT, MANAGER)AS SELECT PROJNAME, DEPTNAME, LASTNAMEFROM PROJ_DEPT LEFT OUTER JOIN EMPLOYEESON MGRNO = EMPNO

The tables used in the above visual are pure subsets of the DB2 sample tables. Instead of the tables on the visual, views on the DB2 sample tables could be used increasing the number of levels in the view hierarchy.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-41

Page 89: Ce 1319 Stud

Student Notebook

.

Figure 2-24. Read-only views CE1319.4

Notes:

• A view is read-only if any of the following statements is true:

- The first FROM clause identifies more than one table or view, that is, the view joins multiple tables or views.

- The first SELECT clause specifies the keyword DISTINCT, that is, the view eliminates duplicate rows (to be precise: all but one of each set of duplicate rows).

- The first SELECT clause contains a column function, that is, a column of the logical table described by the view is derived by means of a column function.

- The outer subselect contains a GROUP BY clause, that is, the rows of the logical table described by the view can be derived from multiple rows of the base tables or views rather than from a single row.

- The view contains a subquery such that the base object of the outer select, and of the subquery (known as a correlated subquery), is the same table. This means that the table or view from which the view extracts data is also used to determine the rows being selected.

© Copyright IBM Corporation 2007, 2013

EMPLOYEE TableEMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

DEPARTMENT TableDEPTNAMEDEPTNO MGRNO

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE DIV.

D01

A00C01 INFORMATION CENTER

000010000030

NAME DEPARTMENT

HAASO'CONNELLKWANNICHOLLSQUINTANA

SPIFFY COMPUTER SERVICE DIV.SPIFFY COMPUTER SERVICE DIV.INFORMATION CENTERINFORMATION CENTERINFORMATION CENTER

EMP_DEPT View

UPDATE EMP_DEPTSET DEPARTMENT = 'DEVELOPMENT CENTER'WHERE NAME = 'KWAN'

Read-only views

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-42 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 90: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

- The first FROM clause identifies a read-only view, that is, the view is based on a read-only view.

• A read-only view cannot be the object of an INSERT, UPDATE, or DELETE statement. In other words, you cannot write changes into the tables using a read-only view.

• The UPDATE statement in the example fails because view EMP_DEPT is based on a join of tables EMPLOYEE and DEPARTMENT and, thus, is read-only.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-43

Page 91: Ce 1319 Stud

Student Notebook

.

Figure 2-25. Changing data through views CE1319.4

Notes:

Not all views are read-only. Views that are not read-only can have some restrictions on which columns of the view can be updated. For example, a view that contains derived data like age of a person cannot be used to update the derived data.

In addition:

• You must have the appropriate authorization to insert, update, or delete rows using a view.

• A view that you can use to update data is subject to the referential and table check constraints for the table upon which the view is based.

• When you use a view to insert a row in a table, the view definition must specify all columns in the base table that are not nullable nor have a default value. The row being inserted must contain a value for each of those columns.

• When you use a view to update, you can only update columns that are in the view. The columns of the view form a projection of the base table. You cannot update derived columns, even if they are in the view.

© Copyright IBM Corporation 2007, 2013

UPDATE

INSERT

DELETE

View Table

Changing data through views

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-44 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 92: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• When you delete with a view, one or more entire base table rows (the selection of rows from the base table) is removed, not just the part of the table that is visible through the view. Make sure that the data which is not visible through the view is data that you want to delete before you delete through a view.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-45

Page 93: Ce 1319 Stud

Student Notebook

.

Figure 2-26. INSTEAD OF triggers (1 of 2) CE1319.4

Notes:

The view V1 here is a read-only view. As we saw earlier, this view is the result of the DISTINCT statement in the SELECT.

For DB2 to delete a row from a view it must be able to map the row specified in the view to only one row in a base table. To update a column in a view, it also must be able to map the column to be updated to a column in a base table. All views that can be updated must, by definition, also be deletable.

With a view using DISTINCT, each row in the view can be mapped to potentially many rows in the base table. Because of the potential for multiple rows, it is a read-only view. As a result, DELETE and UPDATE fail.

© Copyright IBM Corporation 2007, 2013

INSTEAD OF triggers (1 of 2)CREATE TABLE EMP1 AS (SELECT EMPNO, WORKDEPT FROM EMPLOYEE) WITH NO DATA

INSERT INTO EMP1 SELECT EMPNO, WORKDEPT FROM EMPLOYEE WHERE JOB = ‘MANAGER’

CREATE VIEW V1(EMPNO, WORKDEPT) AS SELECT DISTINCT EMPNO, WORKDEPT FROM EMP1 ;

– This is a read-only view!

DELETE FROM V1 WHERE WORKDEPT = ‘E21’ -- fails!

UPDATE V1 SET EMPNO = ‘444444’, WORKDEPT = ‘YYY’ -- fails!

;

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-46 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 94: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-27. INSTEAD OF triggers (2 of 2) CE1319.4

Notes:

An INSTEAD OF trigger prevents DB2 from trying to interpret the view definition for the update operation. Instead of doing that, it will execute the body of the trigger, relying on the definer to come up with meaningful semantics.

INSTEAD OF is a very clear clause. It does not mean execute the trigger before attempting the delete or update. It does not mean do it after. It literally means, forget about the delete or update. Execute this piece of code instead.

© Copyright IBM Corporation 2007, 2013

INSTEAD OF triggers (2 of 2) CREATE TRIGGER V1_DELETE INSTEAD OF DELETE ON V1 REFERENCING OLD AS O FOR EACH ROW MODE DB2SQL DELETE FROM EMP1 WHERE O.EMPNO = EMPNO AND O.WORKDEPT = WORKDEPT

DELETE FROM V1 where WORKDEPT = ‘E21’ --works!

CREATE TRIGGER V1_UPDATE INSTEAD OF UPDATE ON V1REFERENCING NEW AS N OLD AS O FOR EACH ROW MODE DB2SQL UPDATE EMP1 SET EMPNO = N.EMPNO,WORKDEPT = N.WORKDEPT WHERE EMPNO = O.EMPNO AND WORKDEPT = O.WORKDEPT

UPDATE V1 SET EMPNO = ‘444444’, WORKDEPT = ‘YYY’WHERE EMPNO = ‘004000’; --works!

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-47

Page 95: Ce 1319 Stud

Student Notebook

.

Figure 2-28. CHECK OPTION CE1319.4

Notes:

WITH CHECK OPTION (which is identical to WITH CASCADED CHECK OPTION) specifies that every row which is inserted or updated via the view must conform to the view definition. If the row being inserted or updated via the view does not conform to the view definition, the insert or update fails and no rows are inserted or updated.

If the view is based on another view which has a search condition, this search condition is enforced as well. If that other view is again based on a third view, the search condition of the third view is enforced as well, and so on. With other words, both the search conditions of the view itself and of the views upon which the view is based, directly or indirectly, are enforced.

WITH CHECK OPTION must not be specified if the view is read-only or if its search condition includes a subquery, or if the search condition of an underlying view includes a subquery.

If WITH CHECK OPTION is specified for an updateable view that does not allow inserts, the constraint applies only to updates.

© Copyright IBM Corporation 2007, 2013

WORKS!

UPDATE SALARIES_1SET SALARY = 42000WHERE EMPNO = '000030'

CREATE VIEW SALARIES_1AS SELECT EMPNO, LASTNAME, SALARY

FROM EMPLOYEEWHERE SALARY < 40000

EMPLOYEE Table

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

FAILS!

UPDATE SALARIES_2SET SALARY = 42000WHERE EMPNO = '000030'

CREATE VIEW SALARIES_2AS SELECT EMPNO, LASTNAME, SALARY

FROM EMPLOYEEWHERE SALARY < 40000WITH CHECK OPTION

CHECK OPTION

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-48 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 96: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

WITH CHECK OPTION can even be specified if the view is updateable, but does not have a search condition. In this case, the search conditions of those views are enforced upon which the view is directly or indirectly based.

If WITH CHECK OPTION is omitted, the search condition of the view is not used in the checking of insert or update operations. The view can then be used to insert rows that do not conform to the search condition of the view and to update rows so that they no longer conform to that search condition. Such rows cannot be retrieved using that view. If the view depends (directly or indirectly) on a view that has been defined with WITH CHECK OPTION, the search conditions of that view and of all views upon which, in turn, that view depends are still enforced.

In the first example, view SALARIES_1 is created without WITH CHECK OPTION. An update that raised the yearly salary to 40000 or more, which is beyond the scope of the view, would succeed. After the update, the changed row is no longer visible through the view, but still exists in the base table.

In the second example, view SALARIES_2 is created. It has the same definition as SALARIES_1 except that WITH CHECK OPTION is specified. This time, an update that tried to raise the yearly salary to 40000 or higher would fail. WITH CHECK OPTION ensures that the update is not permitted if it would remove the changed row from the scope of the view.

The WITH CHECK OPTION has two forms: cascaded and local. The difference between these two forms is meaningful only when a view is defined on top of another view. If a view called VIEW1 is defined by a query on another view called VIEW2, we will refer to VIEW2 as the underlying view.

When VIEW1 is defined with a local check option, operations on VIEW1 must satisfy the definitions of VIEW1 and of all underlaying views that also have a check option; however, they need not satisfy the definitions of underlaying views that do not have a check option. On the other hand, when VIEW1 is defined with cascaded check option, all operations on VIEW1 my satisfy the definitions of VIEW1 and of all underlaying views, whether they have a check option or not. If a CREATE VIEW statement simply specifies WITH CHECK OPTION, the default is cascaded check option.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-49

Page 97: Ce 1319 Stud

Student Notebook

.

Figure 2-29. View Merge CE1319.4

Notes:

In the view merge process, the statement that references the view is combined with the subselect that defined the view. This combination creates a logically equivalent statement. The equivalent statement is executed against the table upon which the view is based.

In the example, the following happens:

• The FROM clause of the view definition becomes the FROM clause of the combined statement.

• The WHERE clause of the defining subselect, WHERE DEPTNO = 'C01', is combined with the WHERE clause of the SELECT, WHERE SALARY > 25000, to give the logically equivalent WHERE DEPTNO = 'C01' AND SALARY > 25000.

• The column name NAME of the SELECT statement becomes the column name of an AS clause redefining the name of the appropriate base table column: LASTNAME AS NAME.

© Copyright IBM Corporation 2007, 2013

FUNCTIONS AS

CREATE VIEW EMP_SALARY_DEPT (NAME, SALARY, DEPARTMENT)AS SELECT LASTNAME, SALARY, DEPTNAME

FROM EMPLOYEE INNER JOIN DEPARTMENTON WORKDEPT = DEPTNO

WHERE DEPTNO = 'C01'

SELECT NAME, SALARYFROM EMP_SALARY_DEPTWHERE SALARY > 25000

SELECT LASTNAME AS NAME, SALARYFROM EMPLOYEE INNER JOIN DEPARTMENT

ON WORKDEPT = DEPTNOWHERE DEPTNO = 'C01' AND

SALARY > 25000

View Merge

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-50 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 98: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-30. View materialization CE1319.4

Notes:

In some cases, a view cannot be merged with a SELECT statement because the two cannot be combined into a single statement. In these cases, the view is materialized, that is, written to a work file on disk to be processed like a table.

In the example, the view applies the SUM() function to the SALARY column to build a list of the departments and their total salaries. The subsequent SELECT statement determines the largest of the sums returned by the function. The SELECT statement cannot determine the largest sum until all sums have been determined. This requires writing the results of the view to disk so that the SELECT statement can scan the view result to find the largest sum.

The need to write to disk can cause a negative impact on performance. Therefore, view merge is preferred over view materialization when the DB2 Optimizer selects the access path during the BIND process.

© Copyright IBM Corporation 2007, 2013

EMPLOYEE Table

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

SELECT MAX(TOTAL_SALARY) AS MAXIMUM_SALARYFROM DEPT_SALARIES

CREATE VIEW DEPT_SALARIES (DEPARTMENT, TOTAL_SALARY)AS SELECT WORKDEPT, SUM(SALARY)

FROM EMPLOYEEGROUP BY WORKDEPT

DEPT_SALARIES View

DEPARTMENT TOTAL_SALARY

STEP 1

MAXIMUM_SALARY

90470.00

STEP 2

View materialization

90470.00C01

82000.00A00

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-51

Page 99: Ce 1319 Stud

Student Notebook

.

Figure 2-31. Checkpoint CE1319.4

Notes:

Write your answers here:

1.

2.

3.

4.

5.

© Copyright IBM Corporation 2007, 2013

Checkpoint1. Assume that you have created a new table. How could you make

sure that you will not have any duplicate rows in the table?

2. T/F. Check constraints can be used to enforce business rules requiring evaluation of one or more columns in a table row.

3. Assume that you want to ensure that all of the foreign key values in a dependent table exist in a key of the parent table. What will you need to define in the parent table? What will you need to define in the dependent table?

4. Assume that you need to insert a record in a table any time an employee receives a salary increase of more than 10 percent. Howcould you ensure that this happens without writing any application code?

5. T/F. Views can be based on one or more tables, but not on other views.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-52 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 100: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 2-32. Unit summary CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Unit summary

Having completed this unit, you should be able to:• Code statements to:

– Create tables and views– Alter tables– Create indexes– Implement referential integrity (RI)– Define triggers and check constraints

• Identify impacts and advantages of referential integrity, including impacts of delete rules

• Identify considerations when using triggers and check constraints

• Identify the advantages of views

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 2. Create Objects 2-53

Page 101: Ce 1319 Stud

Student Notebook

.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

2-54 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 102: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 3. Join

What this unit is about

After reviewing inner joins, we will discuss outer joins. Outer joins allow you to pick up rows whose join-column values do not match any values in the other table, as well as those which have matching values in the join columns.

We will then look at joining tables to themselves. Sometimes the answer to the query can be found in just one table but you need to look through that table more than once to find the answer.

What you should be able to do

After completing this unit, you should be able to:

• Use inner and outer joins to retrieve data from more than one table

• Use joins of tables to themselves

How you will check your progress

• Checkpoints

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-1

Page 103: Ce 1319 Stud

Student Notebook

.

Figure 3-1. Unit objectives CE1319.4

Notes:

After reviewing inner joins, we will discuss outer joins. Outer joins allow you to pick up rows whose join-column values do not match any values in the other table, as well as those which have matching values in the join columns.

We will then look at joining tables to themselves. Sometimes the answer to the query can be found in just one table but you need to look through that table more than once to find the answer.

© Copyright IBM Corporation 2007, 2012

Unit objectives

After completing this unit, you should be able to:• Use inner and outer joins to retrieve data from more than

one table • Use joins of tables to themselves

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 104: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-2. Sample tables for unit CE1319.4

Notes:

The visual shows the tables used for the examples of this unit. They are subsets of the original tables, including only some of the columns and some of the rows from each table, in order to simplify the example queries in this unit.

© Copyright IBM Corporation 2007, 2012

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY000010000030000120000130000140000400

HAASKWANO'CONNELLQUINTANANICHOLLSWILSON

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.0025400.00

DEPARTMENT

DEPTNAMEDEPTNO MGRNO

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE DIV.

D01

A00

C01 INFORMATION CENTER

000010

000030

PROJECT

PROJNAMEPROJNO RESPEMPDEPTNO

USER EDUCATION

ADMIN SERVICES

IF2000

AD3100IF1000 QUERY SERVICES

000010000030000030

D01C01C01

-

-

Sample tables for unit

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-3

Page 105: Ce 1319 Stud

Student Notebook

.

The tables are derived from the DB2 sample tables by:

• Selecting the columns shown on the visual. • Selecting the rows with employee numbers 000010, 000030, 000120, 000130, and

000140 from the sample EMPLOYEE table. • Adding the row with employee number 000400 to cater for reasonable outer join

examples. Although the definition of column WORKDEPT allows for null values, the original sample tables do not have an employee that has not been assigned to a department.

• Selecting the rows with department numbers A00, C01, and D01 from the sample DEPARTMENT table.

• Selecting the rows with project numbers AD3100, IF1000, and IF2000 from the sample PROJECT table.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 106: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-3. Join (original syntax) CE1319.4

Notes:

Sometimes, not all of the information that you want to see is in a single table. To get the report you want, you might need to retrieve some columns from one table and some columns from another table. You can use a SELECT statement to get columns from one or more tables in a single result set.

If you read from two tables, the rows of the result set will always be a concatenation of rows from the first table with rows from the second table. However, if you do not provide a join condition, DB2 will join each row of the first table with every row of the second table.

This is called a Cartesian Product and is usually considered very undesirable since most of the relationships between the rows are false. For example, a Cartesian Product involving the rows of tables EMPLOYEE and DEPARTMENT on the previous visual would include a row with the HAAS row of the EMPLOYEE table concatenated with the D01 row of the DEPARTMENT table, which is clearly not appropriate.

Normally, you should only concatenate a row in the first table to a row in the second table if the two rows have the same values in some columns. This is accomplished by equating the

© Copyright IBM Corporation 2007, 2012

EMPNO LASTNAME DEPTNO DEPTNAME

000010000120

000140

HAASO'CONNELLKWANNICHOLLSQUINTANA

A00C01C01C01

SPIFFY COMPUTER SERVICE DIV.A00SPIFFY COMPUTER SERVICE DIV.INFORMATION CENTERINFORMATION CENTERINFORMATION CENTER

RESULT

000030

000130

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAMEFROM EMPLOYEE, DEPARTMENTWHERE WORKDEPT = DEPTNO

EMPLOYEE.WORKDEPT

DEPARTMENT.DEPTNO

A00C01 D01NULL

Join (original syntax)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-5

Page 107: Ce 1319 Stud

Student Notebook

.

corresponding columns in the SQL statement by means of a join condition. The columns based on which the tables are combined (joined) are referred to as join columns.

In the example on the current visual, the WHERE clause assures that a row in the EMPLOYEE table is only joined to a row in the DEPARTMENT table if the department numbers in both tables are the same, that is, WORKDEPT in table EMPLOYEE has the same value as DEPTNO in the DEPARTMENT table.

This normal join is also referred to as inner join, see next page.

Logically additional predicates in the WHERE clause are applied after the join but in practice DB2 applies these so called local predicates as early in the join process as possible for performance reasons.

Note This join syntax is available in all versions of DB2 on every platform.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 108: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-4. INNER JOIN (newer syntax) CE1319.4

Notes:

The syntax used in this example means exactly the same as the syntax shown on the previous visual. This new standard syntax for an Inner Join is available on all DB2 platforms. The old Inner Join syntax remains supported on all platforms so you do not have to stop using it.

In this new syntax, the keywords 'INNER JOIN' replace the comma in the FROM clause and the ON keyword allows you to specify the Join condition as part of the FROM clause, rather than in the WHERE clause. A WHERE clause would follow the ON clause if there was a need for filtering rows to limit the result set. Also note that coding simply “JOIN” implies inner join, so that is another variation of the syntax that is valid.

Remember that the EMPLOYEE table contains an employee, WILSON, who has not been assigned to a department and the DEPARTMENT table contains a department (D01) that no employee works for. This is an Inner Join so neither of these “orphan” rows appears in the result set.

© Copyright IBM Corporation 2007, 2012

000010000120000030000140000130

HAASO'CONNELLKWANNICHOLLSQUINTANA

A00C01C01C01

SPIFFY COMPUTER SERVICE DIV.A00SPIFFY COMPUTER SERVICE DIV.INFORMATION CENTERINFORMATION CENTERINFORMATION CENTER

RESULT

EMPNO LASTNAME DEPTNO DEPTNAME

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAMEFROM EMPLOYEE INNER JOIN DEPARTMENT

ON WORKDEPT = DEPTNO

EMPLOYEE.WORKDEPT

DEPARTMENT.DEPTNO

A00C01

NULL D01

INNER JOIN (newer syntax)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-7

Page 109: Ce 1319 Stud

Student Notebook

.

Figure 3-5. LEFT OUTER JOIN CE1319.4

Notes:

The result set of a LEFT OUTER JOIN contains all the rows that would have been created by an Inner Join, plus all orphan rows from the left-hand table (the one to the left of the keywords LEFT OUTER JOIN).

In the example, we list all employees who have been assigned to a department with the names of their departments and all employees that have not been assigned to a department (of course, without a department name). Rows that refer to employees without a department contain nulls in the columns that originated in the DEPARTMENT table.

© Copyright IBM Corporation 2007, 2012

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAMEFROM EMPLOYEE

LEFT OUTER JOIN DEPARTMENTON WORKDEPT = DEPTNO

RESULT

EMPNO LASTNAME DEPTNO DEPTNAME000010

000130

HAAS

QUINTANA000140 NICHOLLS

SPIFFY COMPUTER SERVICE DIV.A00

C01C01

INFORMATION CENTER

INFORMATION CENTER

000030 KWAN C01 INFORMATION CENTER000120 O'CONNELL A00 SPIFFY COMPUTER SERVICE DIV.

000400 WILSON - -

EMPLOYEE.WORKDEPT

DEPARTMENT.DEPTNO

D01A00C01

NULL

INFORMATION CENTER

LEFT OUTER JOIN

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 110: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-6. RIGHT OUTER JOIN CE1319.4

Notes:

An Outer Join gives you all the same rows as an inner join, plus the “orphan” rows that do not have matching values in the other tables.

There are three types of Outer Joins:

• RIGHT OUTER JOIN

• LEFT OUTER JOIN

• FULL OUTER JOIN

The result set of a RIGHT OUTER JOIN contains all the rows that would have been created by an Inner Join, plus all orphan rows from the right-hand table (the one to the right of the keywords RIGHT OUTER JOIN).

In the example, we list all employees who have been assigned to departments, plus all departments to which no employees have been assigned. Rows that refer to departments without an employee contain nulls in the columns that originated in the EMPLOYEE table.

© Copyright IBM Corporation 2007, 2012

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAMEFROM EMPLOYEE

RIGHT OUTER JOIN DEPARTMENTON WORKDEPT = DEPTNO

RESULT

EMPNO LASTNAME DEPTNO DEPTNAME000010000120000030000130000140

HAASO'CONNELLKWANQUINTANANICHOLLS

A00SPIFFY COMPUTER SERVICE DIV.A00SPIFFY COMPUTER SERVICE DIV.

C01 INFORMATION CENTERC01C01

INFORMATION CENTERINFORMATION CENTER

D01 DEVELOPMENT CENTER- -

EMPLOYEE.WORKDEPT

DEPARTMENT.DEPTNO

D01NULLA00C01

RIGHT OUTER JOIN

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-9

Page 111: Ce 1319 Stud

Student Notebook

.

Figure 3-7. FULL OUTER JOIN CE1319.4

Notes:

The result set of a FULL OUTER JOIN contains all the rows that would have been created by an Inner Join, plus all orphan rows from both tables.

In the example, we list all employees and all departments. Rows that refer to departments without an associated employee contain nulls in the columns that originated in the EMPLOYEE table. Rows that refer to employees without a department contain nulls in the columns that originated in the DEPARTMENT table.

© Copyright IBM Corporation 2007, 2012

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAMEFROM EMPLOYEE

FULL OUTER JOIN DEPARTMENTON WORKDEPT = DEPTNO

EMPLOYEE.WORKDEPT

DEPARTMENT.DEPTNO

D01NULL

RESULT

EMPNO LASTNAME

DEPTNO DEPTNAME

000030 KWAN C01000130000140

QUINTANANICHOLLS

C01C01

INFORMATION CENTERINFORMATION CENTER

D01 DEVELOPMENT CENTER-000400

-WILSON - -

000010 HAAS SPIFFY COMPUTER SERVICE DIV.A00000120 O'CONNELL A00 SPIFFY COMPUTER SERVICE DIV.

A00C01

INFORMATION CENTER

FULL OUTER JOIN

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 112: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-8. Anti - JOIN (1 of 3) CE1319.4

Notes:

By design, the SQL join syntax will always return the values from the joined tables where the join condition matches. But sometimes the user may want to exclude the matching, shared information, and simply return the “orphan” information. An SQL statement to meet that particular business need is referred to as an “anti-JOIN”. In essence, an anti-join is a form of join with “reverse logic”. Rather than returning rows when there is a match (according to the join predicate) between the L-hand table and R-hand table, an anti-join returns only the information for which there is no match.

Suppose that the user wants to simply display the department number and department name of those departments that do not have any employees assigned to them. In order to determine the answer to that problem, the user needs to access both the DEPARTMENT and EMPLOYEE tables. However, he or she wishes to exclude from the result set any departments that do have employees assigned to them.

The query above shows one way to code the SQL in order to obtain the desired result, by adding the predicate WORKDEPT IS NULL to an otherwise “normal” join statement. That additional predicate filters out the matching information that is [by default] returned.

© Copyright IBM Corporation 2007, 2012

EMPLOYEE.WORKDEPT

DEPARTMENT.DEPTNO

D01

NULL

RESULT

Anti–JOIN (1 of 3)

METHOD 1

SELECT D.DEPTNO, D.DEPTNAME

FROM EMPLOYEE E RIGHT OUTER JOIN DEPARTMENT D

ON E.WORKDEPT = D.DEPTNO

WHERE E.WORKDEPT IS NULL ;

DEPTNO DEPTNAMEDEVELOPMENT CENTERD01

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-11

Page 113: Ce 1319 Stud

Student Notebook

.

Figure 3-9. Anti - JOIN (2 of 3) CE1319.4

Notes:

This second method uses one of the “set” operators to achieve the correct results. In the compound query above, the first part of the query returns the results for an outer join (the matching information, plus the “orphan” information from the R-hand table, and the EXCEPT keyword uses second part of the query to then exclude the matching information.

© Copyright IBM Corporation 2007, 2012

EMPLOYEE.WORKDEPT

DEPARTMENT.DEPTNO

D01

NULL

Anti–JOIN (2 of 3)

METHOD 2

SELECT DEPTNO, DEPTNAME

FROM EMPLOYEE E RIGHT OUTER JOIN DEPARTMENT D

ON E.WORKDEPT = D.DEPTNO

EXCEPT

SELECT DEPTNO, DEPTNAME

FROM EMPLOYEE E INNER JOIN DEPARTMENT D

ON E.WORKDEPT = D.DEPTNO ;

RESULTDEPTNO DEPTNAME

DEVELOPMENT CENTERD01

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-12 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 114: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-10. Anti - JOIN (3 of 3) CE1319.4

Notes:

In this third example of the anti-JOIN, the SQL statement is using a subquery in conjunction with the NOT keyword of an IN list to solve the problem. The subquery returns the department number of all departments that DO have employees assigned to them, and then the NOT IN excludes those departments from the result set.

© Copyright IBM Corporation 2007, 2012

EMPLOYEE.WORKDEPT

DEPARTMENT.DEPTNO

D01

NULL

RESULT

Anti–JOIN (3 of 3)

DEPTNO DEPTNAMEDEVELOPMENT CENTERD01

METHOD 3

SELECT DEPTNO, DEPTNAME

FROM DEPARTMENT

WHERE DEPTNO NOT IN

(SELECT WORKDEPT

FROM EMPLOYEE) ;

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-13

Page 115: Ce 1319 Stud

Student Notebook

.

Figure 3-11. Joins of more than two tables CE1319.4

Notes:

Many tables can be joined in a single SELECT statement. The maximum number of tables in the join depends on the version of DB2 you are using and the operating system. For example, some of the limits are as follows:

• For DB2 for Linux, UNIX, and Windows, the maximum number of tables that can be joined depends on the available storage.

• In DB2 for z/OS, the maximum number of tables that can be joined is 225 or fewer, depending on the complexity of the SELECT statement.

If three or more tables are joined, different types of joins may be used to join the various tables. Of course, the application logic, that is, the desired result, dictates which types of joins must be used.

If n tables are joined, there must be at least n-1 different join conditions and one column from each table must appear in at least one of the join conditions. If this rule is not observed a Cartesian Product will result.

© Copyright IBM Corporation 2007, 2012

SELECT P.PROJNO,P.PROJNAME, D.DEPTNO, D.MGRNO,E.LASTNAME AS MGRNAME

FROM PROJECT PINNER JOIN DEPARTMENT D ON P.DEPTNO = D.DEPTNOLEFT OUTER JOIN EMPLOYEE E ON D.MGRNO = E.EMPNO

Left and right joins are not symmetrical.

RESULT DEPTNO MGRNO MGRNAMEPROJNO PROJNAME

IF1000 QUERY SERVICES C01 000030 KWANAD3100 ADMIN SERVICES D01 - -

IF2000 USER EDUCATION C01 000030 KWAN

D01C01

000030

NULLDEPARTMENT D

EMPLOYEEPROJECT P

000120000130000140000400

RESULTLeft Outer Join

on MGRNO=EMPNO

Inner Join onP.DEPTNO = D.DEPTNO

Joins of more than two tables

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-14 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 116: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

A single join operation combines two tables. If more than two tables are joined, the subsequent join operation combines the intermediate result table of the preceding join operations with the next table.

Your application implies a sequence in which the tables and intermediate result tables should be joined, and, you must express this in the FROM clause. As there may be different ways that you can achieve the desired output, DB2 may decide on joining the tables differently or in a different sequence.

The purpose of the example above is to determine, for each project that has been assigned to a department, the manager of the department performing the project. Projects that have been assigned to departments without manager should be listed as well. You can use different methods to solve this problem:

• The SELECT statement on the visual offers one possibility. You can first join the PROJECT table to the DEPARTMENT table via an Inner Join to determine, for each project, its project number, its project name, the department number of the department performing the project, and the employee number of the manager for the department. Then, you can Left Outer Join the intermediate result table to the EMPLOYEE table to add the name of the manager for those projects whose responsible department has a manager.

• As an alternative, you can first determine, for each department, its department number, the employee number of its manager, and, if the department has a manager, his/her name. This can be achieved by a Left Outer Join of the DEPARTMENT table to the EMPLOYEE table. Then, you can perform an Inner Join of the PROJECT table to the intermediate result table to determine, for each project, its project number, its project name, the department number of the department performing the project, the employee number of the manager of the department, and if the department has a manager, his/her name.

You can formulate the appropriate SELECT statement as follows. It provides the same result as the SELECT statement on the visual:

SELECT PROJNO, PROJNAME, P.DEPTNO, MGRNO, LASTNAME AS MGRNAME FROM PROJECT P INNER JOIN (DEPARTMENT D LEFT OUTER JOIN EMPLOYEE ON MGRNO = EMPNO) ON P.DEPTNO = D.DEPTNO;

Inner Joins and Full Outer Joins are symmetrical. For them, the order of the two tables being joined does not matter. Accordingly, PROJECT P and (DEPARTMENT D LEFT OUTER JOIN EMPLOYEE) in the preceding SELECT statement could be interchanged without changing the result set.

Left or Right Outer Joins are not symmetrical. Therefore, the order in which the tables are specified is important. If you reverse the order of the tables, a Left Outer Join becomes a

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-15

Page 117: Ce 1319 Stud

Student Notebook

.

Right Outer Join and vice versa. In the preceding SELECT statement, (DEPARTMENT D LEFT OUTER JOIN EMPLOYEE) can also be formulated as (EMPLOYEE RIGHT OUTER JOIN DEPARTMENT D) without changing the result set.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-16 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 118: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-12. Joins and local predicates (1 of 2) CE1319.4

Notes:

There are two types of predicates (search conditions): join predicates and local predicates:

• Join predicates are those specified via the ON keyword in the FROM clause. They tell DB2 which columns in the participating tables of a join operation should be matched to join the rows correctly.

• Local predicates are those specified via the WHERE clause.

For the purpose of predicting which rows will be returned from a SELECT statement, assume that the join operation is performed before the other clauses in the statement.

© Copyright IBM Corporation 2007, 2012

EMPNO LASTNAME WORKDEPT SALARY DEPTNAMEDEPTNO MGRNO

000130 QUINTANA C01 23800.00

000140 NICHOLLS C01 28420.00

SPIFFY COMPUTER SERVICE DIV.A00 000010

C01 INFORMATION CENTER 000030

- - --

000010 HAAS A00 52750.00

000030 KWAN C01 38250.00

000120 O'CONNELL A00 29250.00

000400 WILSON 25400.00- -- -

DEVELOPMENT CENTERD01 -

C01 INFORMATION CENTER 000030

C01 INFORMATION CENTER 000030

SPIFFY COMPUTER SERVICE DIV.A00 000010

INTERMEDIATE RESULT TABLE

SELECT EMPNO, LASTNAME, SALARY, DEPTNO, DEPTNAME

WHERE SALARY > 30000 ANDDEPTNAME LIKE '%CENTER%'

Joins and local predicates (1 of 2)

FROM EMPLOYEE FULL OUTER JOIN DEPARTMENT

ON WORKDEPT = DEPTNO

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-17

Page 119: Ce 1319 Stud

Student Notebook

.

Figure 3-13. Joins and local predicates (2 of 2) CE1319.4

Notes:

As mentioned on previous page, for the purpose of predicting which rows will be returned from a SELECT statement, assume that the join operation is performed before the other clauses in the statement.

In the example above, the WHERE clause for the SELECT statement is shown underneath the intermediate result table to emphasize that the predicates of the WHERE clause are applied to the intermediate result table. The WHERE clause selects rows from the intermediate result table with a salary higher than 30000 (SALARY > 30000) and a department name containing the character string 'CENTER' (DEPTNAME LIKE '%CENTER%'). The only row in the intermediate result table that satisfies both conditions is the row for employee KWAN.

The result of this Outer Join is equivalent to the result of an Inner Join and, in this case, coding an Inner Join is preferable.

If the intention was to Full Outer Join all employees having a salary higher than 30000 to all departments whose name contains the character string 'CENTER', the query would have to be formulated differently and would require nested table expressed. They will be discussed

© Copyright IBM Corporation 2007, 2012

EMPNO LASTNAME WORKDEPT SALARY DEPTNAMEDEPTNO MGRNO

000130 QUINTANA C01 23800.00

000140 NICHOLLS C01 28420.00

SPIFFY COMPUTER SERVICE DIV.A00 000010

C01 INFORMATION CENTER 000030

- - --

000010 HAAS A00 52750.00

000030 KWAN C01 38250.00

000120 O'CONNELL A00 29250.00

000400 WILSON 25400.00- -- -

DEVELOPMENT CENTERD01 -

C01 INFORMATION CENTER 000030

C01 INFORMATION CENTER 000030

SPIFFY COMPUTER SERVICE DIV.A00 000010

SELECT EMPNO, LASTNAME, SALARY, DEPTNO, DEPTNAMEFROM EMPLOYEE

FULL OUTER JOIN DEPARTMENTON WORKDEPT = DEPTNO

WHERE SALARY > 30000 AND DEPTNAME LIKE '%CENTER%'

LASTNAME DEPTNO DEPTNAMEEMPNO SALARY000030 KWAN INFORMATION CENTERC0138250.00

Joins and local predicates (2 of 2)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-18 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 120: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

in a later unit. As you can easily see, the result would be different since employees KWAN and HAAS both have a salary higher than 30000 and, thus, both would appear in the result set. This FULL OUTER JOIN could also have been satisfied by applying the local predicates to each base table, before the full join, by using nested table expressions:

SELECT EMPNO, LASTNAME, SALARY, DEPTNO, DEPTNAME FROM (SELECT * FROM EMPLOYEE WHERE SALARY > 30000) AS E FULL OUTER JOIN (SELECT * FROM DEPARTMENT WHERE DEPTNAME LIKE '%CENTER%') AS D ON E.WORKDEPT = D.DEPTNO

Nested Table Expressions are covered in more detail in a later topic.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-19

Page 121: Ce 1319 Stud

Student Notebook

.

Figure 3-14. Joining a table with itself (1 of 3) CE1319.4

Notes:

In the DEPARTMENT table, the column ADMRDEPT contains the number of the department (DEPTNO) that this department reports to; for example, department B01 reports to department A00 (the highest department, A00, reports to itself). ADMRDEPT and DEPTNO are columns in the same table; therefore, the DEPARTMENT table can be joined to itself.

© Copyright IBM Corporation 2007, 2012

Joining a table with itself (1 of 3)

DEPTNO DEPTNAME MGRNO ADMRDEPT

A00 SPIFFY COMPUTER SERVICE DIV. 000010 A00

B01 PLANNING 000020 A00

… … … …

DEPARTMENT

Display the name of department B01 and the name of the department it reports to . . .

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-20 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 122: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-15. Joining a table with itself (2 of 3) CE1319.4

Notes:

To meet the requirements at the top of 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.

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, and one for the higher department (named SUP in the visual).

Of course, in reality, DB2 will NOT create a copy of the table; it will join each candidate row of the table with other rows of the same table.

© Copyright IBM Corporation 2007, 2012

DEPARTMENT (DEP)

. . .

. . .

Joining a table with itself (2 of 3)

DEPTNO DEPTNAME MGRNO ADMRDEPT

DEPTNO DEPTNAME MGRNO ADMRDEPT

DEPARTMENT (SUP)

A00 SPIFFY COMPUTING SERVICE DIV. 000010 A00

B01 PLANNING 000020 A00

A00 SPIFFY COMPUTING SERVICE DIV. 000010 A00

B01 PLANNING 000020 A00

Display the name of department B01 and the name of the department it reports to

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-21

Page 123: Ce 1319 Stud

Student Notebook

.

Figure 3-16. Joining a table with itself (3 of 3) CE1319.4

Notes:

Table qualifiers (correlation names) are required because a table is referenced twice within the FROM clause of the query.

Also, notice that we made the results set easier to read and interpret by using the AS clause in the SELECT to specifically name the supervisory department name.

© Copyright IBM Corporation 2007, 2012

SELECT DEP.DEPTNAME,

SUP.DEPTNAME AS SUP_DEPTNAME

FROM DEPARTMENT DEP, DEPARTMENT SUP

WHERE DEP.ADMRDEPT = SUP.DEPTNO

AND DEP.DEPTNO = ‘B01’

DEPTNAME SUP_DEPTNAME

PLANNING SPIFFY COMPUTER SERVICE DIV.

Joining a table with itself (3 of 3)

. . .Display the name of department B01 and the name of the department it reports to

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-22 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 124: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-17. Joining a table with itself - Another example (1 of 2) CE1319.4

Notes:

To get the required data, the EMPLOYEE table must be accessed twice.

Using the EMPLOYEE table, for each employee we read his/her department number. Then, from the department table we find his/her manager's employee number. Using the manager's employee number, we access the EMPLOYEE table again to retrieve the manager's date of birth. Then, the dates of birth are compared and for each employee that is older than his/her manager information is put into the result table.

© Copyright IBM Corporation 2007, 2012

DEPTNAME ADMRDEPT

LASTNAME

LASTNAME

WORKDEPT

WORKDEPT

BIRTHDATE

BIRTHDATE

.

.

. . .

2. Obtain department number from DEPARTMENT (D)

3. Retrieve row for manager from EMPLOYEE (M)

DEPTNO

EMPNO

EMPNO

000100

000330

SPENSER

LEE

E21

E21

1956-12-18

1941-07-18

E21 E21

.

.

000100

.

.

SOFTWARE SUPPORT

.

.

000100

000330

SPENSER

LEE

E21

E21

1956-12-18

1941-07-18

MGRNO

. . . . . .

. . . . . . . . .

1. Retrieve employee's row from EMPLOYEE (E)Which

employees are olderthan theirmanager?

Joining a table with itself –Another example (1 of 2)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-23

Page 125: Ce 1319 Stud

Student Notebook

.

Figure 3-18. Joining a table with itself - Another example (2 of 2) CE1319.4

Notes:

Once again, table qualifiers (correlation names) are required because a table is referenced twice within the FROM clause of the query. Also notice the use of the AS clause to avoid any confusion over employee numbers and birth dates in the results set. The employee’s number is labeled EMPNO and the manager’s number is labeled MGRNO. The manager’s birth date is labeled as M_BIRTHDATE to distinguish it from the birth date of the employee E_BIRTHDATE.

© Copyright IBM Corporation 2007, 2012

Which employees are older thantheir manager?

SELECT E.EMPNO, E.LASTNAME,

E.BIRTHDATE,

M.BIRTHDATE AS M_BIRTHDATE,

M.EMPNO AS MGRNO

FROM EMPLOYEE E,

DEPARTMENT D,

EMPLOYEE M

WHERE E.WORKDEPT = D.DEPTNO

AND D.MGRNO = M.EMPNO

AND E.BIRTHDATE < M.BIRTHDATE

EMPNO LASTNAME BIRTHDATE

LUCCHESIQUINTANABROWNJEFFERSONSMITHJOHNSONSCHNEIDERSMITHSETRIGHTMEHTA…

000110000130000200000230000250000260000280000300000310000320…

1929-11-051925-09-151941-05-291935-05-301939-11-121936-10-051936-03-281936-10-271931-04-211932-08-11…

MGRNO

000010000030000060000070000070000070000090000090000090000100…

1933-08-141941-05-111945-07-071953-05-261953-05-261953-05-261941-05-151941-05-151941-05-151956-12-18…

M_BIRTHDATE

Joining a table with itself –Another example (2 of 2)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-24 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 126: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-19. UNION and UNION ALL CE1319.4

Notes:

Here is a brief review of the UNION set operator.

UNION combines two sets of rows, sorts them, compares them, and removes duplicates. UNION ALL does not eliminate duplicates. Therefore, UNION ALL will perform better than UNION. The use UNION ALL is recommended if the query cannot return duplicate rows, or if the application process or end user can “tolerate” duplicate rows.

© Copyright IBM Corporation 2007, 2012

EDLEVEL1618

EDLEVEL18181818

EDLEVEL161818181818

LASTNAMEHAASTHOMPSONNICHOLLSLUTZ

LASTNAMEQUINTANANICHOLLSHAASTHOMPSONNICHOLLSLUTZ

LASTNAMEQUINTANANICHOLLS

SELECTFROMWHEREUNION ALLSELECTFROMWHERE

LASTNAME, EDLEVELEMPLOYEEJOB = 'ANALYST'

LASTNAME, EDLEVELEMPLOYEEEDLEVEL = 18

SELECTFROMWHERE

LASTNAME, EDLEVELEMPLOYEEEDLEVEL = 18

SELECTFROMWHERE

LASTNAME, EDLEVELEMPLOYEEJOB = 'ANALYST'

UNION and UNION ALL

LASTNAMEHAASLUTZNICHOLLSQUINTANATHOMPSON

EDLEVEL1818181618

SELECTFROMWHEREUNIONSELECTFROMWHERE

LASTNAME, EDLEVELEMPLOYEEJOB = 'ANALYST'

LASTNAME, EDLEVELEMPLOYEEEDLEVEL = 18

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-25

Page 127: Ce 1319 Stud

Student Notebook

.

Figure 3-20. EXCEPT and INTERSECT CE1319.4

Notes:

Here are two other keywords that can be used for merging data, but work differently.

EXCEPT displays what is in the first result set but not in the second result set. In this case, it displays ANALYSTS who are not EDLEVEL 18. This would not show duplicates if there were any; EXCEPT ALL would show duplicates.

INTERSECT displays only what is in both the first result set and in the second result set. In this case, it displays ONLY ANALYSTS who are ALSO EDLEVEL 18. This would not show duplicates if there were any; INTERSECT ALL would show duplicates.

The result set rows for an EXCEPT must qualify the first SELECT, but not qualify the second SELECT. An alternate means of obtaining the same result set as the EXCEPT example above would be to code:

SELECT LASTNAME, EDLEVELFROM EMPLOYEEWHERE JOB = ‘ANALYST’ AND NOT EDLEVEL = 18 ;

© Copyright IBM Corporation 2007, 2012

EXCEPT and INTERSECT

EDLEVELLASTNAME

NICHOLLS 18

SELECTFROMWHEREINTERSECTSELECTFROMWHERE

LASTNAME, EDLEVELEMPLOYEEJOB = 'ANALYST'

LASTNAME, EDLEVELEMPLOYEEEDLEVEL = 18

EDLEVELLASTNAME

QUINTANA 16

SELECTFROMWHEREEXCEPTSELECTFROMWHERE

LASTNAME, EDLEVELEMPLOYEEJOB = 'ANALYST'

LASTNAME, EDLEVELEMPLOYEEEDLEVEL = 18

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-26 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 128: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

The result set rows for an INSTERSECT must qualify the first SELECT and qualify the second SELECT. An alternate means of obtaining the same result set as the INSTERSECT example above would be to code:

SELECT LASTNAME, EDLEVELFROM EMPLOYEEWHERE JOB = ‘ANALYST’ AND EDLEVEL = 18;

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-27

Page 129: Ce 1319 Stud

Student Notebook

.

Figure 3-21. Checkpoint CE1319.4

Notes:

Enter your answers here:

1.

2.

3.

© Copyright IBM Corporation 2007, 2012

Checkpoint

1. There are two tables: one contains employees and the other contains one row for each of the employee's children (plus the employee number). Assume that you want to list all employees and their children. You want the report to include employees who have no children. Which join technique will you need?

2. T/F. If you specify two or more tables in the FROM clause, but omit all join conditions, the result set will be empty.

3. Using the same tables as in the first question, assume that you need to list only employees who have no children. Is this possible with a join?

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-28 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 130: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 3-22. Unit summary CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2012

Unit summary

Having completed this unit, you should be able to:• Use inner and outer joins to retrieve data from more than

one table • Use joins of tables to themselves

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 3. Join 3-29

Page 131: Ce 1319 Stud

Student Notebook

.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

3-30 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 132: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 4. CASE, CAST, Summary (Materialized Query) Tables and Temporary Tables

What this unit is about

CASE expressions can be very useful to avoid coding long SQL statements. The CAST function enables SQL users to transform one DB2 data type into another data type.

Materialized Query Tables (MQTs), including Summary Tables, are very useful in OLAP and Data Warehouse application environments. Temporary tables can be used to improve performance, but can also be used as working tables to make complicated queries easier.

What you should be able to do

After completing this unit, you should be able to:

• Identify when CASE expressions can be useful

• Code CASE expressions in the SELECT list and in the WHERE clause

• Identify when CAST specifications can be used

• Identify the advantages of using Summary (Materialized Query) Tables and Temporary tables

How you will check your progress

• Checkpoint

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-1

Page 133: Ce 1319 Stud

Student Notebook

.

Figure 4-1. Unit objectives CE1319.4

Notes:

CASE expressions can be very useful to avoid coding long SQL statements. The CAST function enables SQL users to transform one DB2 data type into another data type.

Materialized Query Tables (MQTs), including Summary Tables, are very useful in OLAP and Data Warehouse application environments. Temporary tables can be used to improve performance, but can also be used as working tables to make complicated queries easier.

© Copyright IBM Corporation 2007, 2013

Unit objectives

After completing this unit, you should be able to:• Identify when CASE expressions can be useful• Code CASE expressions in the SELECT list and in the

WHERE clause• Identify when CAST specifications can be used• Identify the advantages of using Summary (Materialized

Query) Tables and Temporary tables

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 134: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 4-2. CASE expressions in SELECT CE1319.4

Notes:

CASE expressions allow a user to code IF-THEN-ELSE logic instead of simple values.

DB2 evaluates the WHEN conditions in order from top to bottom. The value returned from the CASE expression is the result of the first true WHEN condition. If no case is satisfied, the ELSE value is used. If no ELSE is coded, the value returned is NULL.

CASE expressions can appear in various places in an SQL statement, including the SELECT clause, the WHERE clause, the GROUP BY clause, and the HAVING clause. It can also appear in IN and VALUES clauses. The examples in the visual above illustrate the use of CASE expressions in the SELECT clause.

The first CASE expression returns a salary classification for the employees rather than the salary itself. The salary classification is based on the range of the salary. If the salary is less than 25000, the salary classification is 'LOW'. It is 'AVERAGE' if the salary is equal to or higher than 25000, but is lower than 40000. Salaries of 40000 and higher are considered 'HIGH' as specified via the ELSE keyword. The appropriate column of the result table is called SALARY_CLASS. This example, which has no expression between the CASE keyword and the first THEN keyword, is called a searched WHEN clause.

© Copyright IBM Corporation 2007, 2013

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

EMPNO LASTNAME SALARY_CLASS AREA_TYPE

000010000030

000140

HAASKWAN

NICHOLLS

HIGHAVERAGE

AVERAGE

ADMINISTRATIONCUSTOMER SERVICE

000120 O'CONNELL AVERAGE ADMINISTRATION000130 QUINTANA LOW CUSTOMER SERVICE

CUSTOMER SERVICE

SELECT EMPNO, LASTNAME,

FROM EMPLOYEE

CASEWHEN SALARY < 25000 THEN 'LOW'WHEN SALARY >= 25000 AND SALARY < 40000 THEN 'AVERAGE'ELSE 'HIGH'

END AS SALARY_CLASS,

CASE SUBSTR(WORKDEPT,1,1)WHEN 'A' THEN 'ADMINISTRATION'WHEN 'C' THEN 'CUSTOMER SERVICE'WHEN 'D' THEN 'DEVELOPMENT' ELSE NULL

END AS AREA_TYPE

CASE expressions in SELECT

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-3

Page 135: Ce 1319 Stud

Student Notebook

.

The second example, which has an expression between the CASE keyword and the first THEN keyword, is called a simple WHEN clause. The expression uses the SUBSTR() scalar function to determine the first character of the WORKDEPT column and the THEN clauses are evaluated for each expected value of that character. If the value is 'A', 'ADMINISTRATION' is displayed in the result, if the value is 'C', 'CUSTOMER SERVICE' is displayed, and, if the value is 'D', 'DEVELOPMENT' is displayed. Null is displayed for all other values.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 136: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 4-3. CASE expression in WHERE clause CE1319.4

Notes:

This visual shows an example of CASE statement usage to protect from division by 0 errors. It finds the employees who earn more than 8% of their income from commission, but are not fully paid on commission (in other words, their salary is not zero).

Of course, an alternate approach to fulfilling this particular business requirement is to code the SELECT with two local predicates:

SELECT EMPNO, LASTNAME, SALARY+COMM AS TOT_SALFROM EMPLOYEEWHERE SALARY > 0 AND COMM/SALARY > 0.08 ;

© Copyright IBM Corporation 2007, 2013

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY COMM

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

4220.003060.002340.001904.002274.00

EMPNO LASTNAME TOT_SAL

000140 NICHOLLS 31194.00

SELECT EMPNO,LASTNAME,SALARY+COMM AS TOT_SAL FROM EMPLOYEEWHERE (CASE WHEN SALARY = 0 THEN NULL

ELSE COMM / SALARYEND ) > 0.08

CASE expression in WHERE clause

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-5

Page 137: Ce 1319 Stud

Student Notebook

.

Figure 4-4. CASE expression in a function CE1319.4

Notes:

This visual shows an example of the CASE expression occurring within a function. It finds the minimum salary for all employees except those with a salary equal to zero.

The same results would also be returned by the following query:

SELECT MIN(SALARY) FROM EMPLOYEE WHERE SALARY <> 0 ; Quite often there may be a number of alternative ways of writing an SQL query, and each of them will yield the desired results. This demonstrates the flexibility of SQL. When there are various ways of writing the query and ensuring the correct results, consideration should then be given to performance. We will address performance in the final unit of this course.

© Copyright IBM Corporation 2007, 2013

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY COMM

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.000.00

29250.0023800.0028420.00

4220.003060.002340.001904.002274.00

MIN_SAL

23800.00

SELECT MIN ( CASEWHEN SALARY = 0 THEN NULLELSE SALARY END) AS MIN_SAL

FROM EMPLOYEE

CASE expression in a function

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 138: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 4-5. Nested CASE expression CE1319.4

Notes:

This visual shows an example of a nested CASE expression. It computes a different salary increase depending on the department worked in and the employee number. Notice that Haas (EMPNO 000010) is not included in the result set, since her salary does not meet the criteria of the local predicate SALARY < 50000.

© Copyright IBM Corporation 2007, 2013

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY COMM

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.000.00

29250.0023800.0028420.00

4220.003060.002340.001904.002274.00

SELECT EMPNO, CASE WHEN WORKDEPT LIKE 'A%' THEN SALARY + 100 WHEN WORKDEPT LIKE 'C%' THEN

CASE WHEN EMPNO= '000130' THEN SALARY+ 200

ELSE SALARY+ 50 END

ELSE SALARYEND AS NEW_SAL

FROM EMPLOYEEWHERE SALARY < 50000

EMPNO NEW_SAL000030000120000130000140

50.0029350.0024000.0028470.00

Nested CASE expression

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-7

Page 139: Ce 1319 Stud

Student Notebook

.

Figure 4-6. CAST specifications CE1319.4

Notes:

Casting is often used in programming languages to refer to the process of changing a value from one data type to another. Casting in SQL has pretty much the same meaning. DB2 supports the CAST syntax as defined by the SQL92 Standard.

In the example above, the division of COMM/SALARY in COL2 gets a default data type depending on the data types of the columns COMM and SALARY. With CAST, the division can be presented with different data type, in this case DECIMAL(9,2). What this accomplishes is it limits the number of places to the right of the decimal point as shown in COL3 to only two. Without the CAST specified (the second column in the result set) there are many more digits of precision to the right of the decimal point.

CAST is also useful when a value of a particular data type is needed as the parameter of a function.

© Copyright IBM Corporation 2007, 2013

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY COMM

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

4220.003060.002340.001904.002274.00

SELECT EMPNO,COMM/SALARY AS COL2,CAST(COMM/SALARY AS DEC(9,2))

AS COL3FROM EMPLOYEEWHERE EMPNO='000140'

EMPNO COL2 COL3

000140 0.0976073187895847994370 0.09

CAST specifications

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 140: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

A special use of this function is to cast a NULL value to a particular data type:

SELECT EMPNO, CAST(NULL AS SMALLINT) FROM EMPLOYEE WHERE EMPNO < '000050‘ ; This can be useful to present null values in a result set.

In the last unit of the course, Unit 8, we cover UDT, User-Defined Distinct Types. There you will see which CASTing functions DB2 automatically creates when UDTs are created.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-9

Page 141: Ce 1319 Stud

Student Notebook

.

Figure 4-7. Summary tables and Materialized Query Tables (MQTs) CE1319.4

Notes:

A Materialized Query Table (MQT) is a table whose definition is based on the result of a query. As such, the MQT typically contains precomputed results based on the data existing in the table or tables on which its definition is based. MQT’s are especially useful in a data warehouse environment, where multiple users may be accessing thousands or millions of table rows and making similar calculations and aggregations.

A summary table is a specialized type of MQT. A MQT whose fullselect contains a GROUP BY clause is summarizing data from the tables referenced in the fullselect. Such a MQT is also known as a summary table.

If the optimizer determines that a dynamic query will run more efficiently against a summary table than the base table, the query executes against the summary table, and we should receive the result of the query faster than access directly to the base tables. The query rewrite function of the optimizer (AQR, or Automatic Query Rewrite) will access a summary table if it determines that the query can be answered by using the data in the summary table instead of accessing the base table or tables. You can also now decide

© Copyright IBM Corporation 2007, 2013

Summary tables and Materialized Query Tables (MQTs)

• Materialized views (Materialized Query Table, MQT)

• Aggregate Aware Optimization

• Contains pre-computed results

• Queries can reuse Summary Tables without accessing base tables

• Improved performance

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 142: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

whether DB2 should attempt to use your summary table or not; the syntax now allows for ENABLE/DISABLE QUERY OPTIMIZATION.

Normally it would be the responsibility of a DBA to create MQT’s or summary tables. The DBA who supports the decision support types of applications should be familiar with the nature of the queries executed by the users he or she supports.

Please note that for DB2 for Linux, UNIX, and Windows, you can set the optimization level. For the optimizer to consider the use of summary tables, the optimization level has to be 2 or greater.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-11

Page 143: Ce 1319 Stud

Student Notebook

.

Figure 4-8. Summary tables or MQTs - Example CE1319.4

Notes:

There are a number of options to consider when creating an MQT:

• REFRESH - Indicates how the data in the table is maintained.

• DEFERRED - The data in the table can be refreshed at any time using the REFRESH TABLE statement. The data in the table only reflects the result of the query at the time of the REFRESH TABLE statement is processed (a snapshot).

• IMMEDIATE - The changes made to the underlying tables as part of a DELETE, INSERT or UPDATE are cascaded to the summary table. In this case, the contents of the table, at any point-in-time, are the same as the specified subselect were to be processed.

When REFRESH DEFERRED or REFRESH IMMEDIATE is specified, the fullselect cannot include:

• References to a view or a summary table • Functions that have external action • Table of view references to system objects (explain tables also should not be specified)

© Copyright IBM Corporation 2007, 2013

CREATE TABLE DEPT_GROUPAS ( SELECT WORKDEPT,

SUM(SALARY) AS SALARY,SUM(BONUS) AS BONUSFROM EMPLOYEEGROUP BY WORKDEPT )

DATA INITIALLY DEFERREDREFRESH DEFERRED

Summary tables or MQTs – Example

• DATA INITIALLY DEFERRED– Data is not inserted into table as part of the CREATE TABLE statement

• REFRESH DEFERRED– Data in the table can be refreshed at any time using REFRESH TABLE

statement

• REFRESH TABLE statement is the only way to populate the table

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-12 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 144: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

There are a number of considerations with the REFRESH DEFERRED and REFRESH IMMEDIATE options. Please see the appropriate SQL Reference for details.

Another option when you create an MQT is MAINTAINED BY.

• MAINTAINED BY SYSTEM (the default) does not allow INSERT/UPDATE/DELETE on the MQT but allows REFRESH.

• MAINTAINED BY USER allows INSERT/UPDATE/DELETE on the MQT but only allows REFRESH DEFERRED.

To exploit Summary table and ensure that we are not using old data from the Summary table: SET CURRENT REFRESH AGE

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-13

Page 145: Ce 1319 Stud

Student Notebook

.

Figure 4-9. Materialized Query Tables - Considerations CE1319.4

Notes:

To Check whether or not a Summary Table is being used by a query, produce the Explain output.

Ideally, each Summary Table should serve many queries.

It is recommended that the DBA create indexes on summary tables and update statistics on summary tables.

Consider physical storage requirements in DASD-constrained environments.

© Copyright IBM Corporation 2007, 2013

Materialized Query Tables – Considerations

• If optimizer decides to use a summary table, access to base tables is eliminated.

• You should create non-unique indexes.

• Some of the key restrictions regarding summary tables: – You cannot alter a summary table.

– You cannot alter the length of a column for a base table ifthat table has a summary table.

– You cannot create a unique index on a summary table or MQT.

– You may not be able to create a summary table based on the result of a query that references one or more nicknames.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-14 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 146: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 4-10. Temporary tables - Usage CE1319.4

Notes:

There are a number of reasons why you might want to implement DB2 temporary tables in your application environment.

1. To store intermediate results sets. If the results from a first query have to be fed as input to a second query, the results of the first query can be stored in a temporary table. That temporary table could then be joined into the second query without incurring the performance cost of re-running the firs query. This would be particularly important if the first query was complex or accessed many, many rows.

2. To enable easier conversion from another relational database product. Other major vendor products such as Oracle and Microsoft SQL Server support the usage of temporary tables. Without this feature, it is more challenging to port applications from another relational database into DB2.

3. To access non-relational data sources. Temporary tables can be used to enable the processing of non-relational data by SQL. A temporary table can be populated with data from VSAM, physical sequential files, IMS, etc. by an application program. Later during

© Copyright IBM Corporation 2007, 2013

Temporary tables - Usage

• Intermediate Results

• Conversion to/from other relational data

• Accessing non-relational data

• Returning result set multiple times

• “Work” table

DECLARE TEMPORARY TABLE …

CREATE

TEMPORARY

TABLE …

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-15

Page 147: Ce 1319 Stud

Student Notebook

.

the course of that program, the temporary table storing the non-relational data can be accessed by SQL.

4. To return a result set more than once by the same program. For example, a complex, multi-table join consumes considerable CPU and I/O resources to execute, and that join needs to be run multiple times during the course of the application. Instead of executing the expensive join three times, you can run it just once and store the results in the temporary table.

5. To use as a general work table. Temporary tables are, by nature, not persistent, and therefore some of the normal DB2 operations (such as locking, logging, recovery, etc.) are not invoked, thereby offering a performance advantage over the use of the more permanent DB2 tables.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-16 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 148: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 4-11. Temporary tables - Types in DB2 z/OS CE1319.4

Notes:

The table above summarizes some of the major differences between CREATEd and DECLAREd temporary tables in the z/OS environment.

When defining a CREATEd temporary table using DDL, DB2 creates an entry in the DB2 catalog tables. Therefore the table is shareable by other DB2 application users and/or processes (Of course, those other users would need to be GRANTed the appropriate privileges on the table). Each DB2 application that uses the temporary table generates a new instance of the table for its own use.

When the application process using a CREATEd temporary table terminates, the instance of the table for that DB2 process is deleted, but the definition persists in the DB2 catalog.

There are some limitations, restrictions and considerations when using CREATEd temporary tables:

1. Indexes cannot be created on such tables, and therefore all access is accomplished by scanning the complete table object.

2. Constraints may not be created

© Copyright IBM Corporation 2007, 2013

Temporary tables – Types in DB2 z/OS

YESNOCheck Constraints

YESNOIndexes

NOYESShareable by other applications

NOYESDefined in DB2 catalog?

DECLARED temp tables

CREATEDtemp tables

Feature/Function

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-17

Page 149: Ce 1319 Stud

Student Notebook

.

3. These tables cannot be referenced by DB2 utilities

4. The DSNDB07 work file database is used to manage the data for CREATEd temporary tables. Therefore, ensure that your DB2 environment has and adequate allocation for these work file data sets for the anticipated usage by temporary tables.

When defining a DECLAREd temporary table using DDL, DB2 does not create an entry in the DB2 catalog tables. Therefore the table is not shareable by any other DB2 application process or user. This type of temporary table is known only to the process that issued the DECLARE statement. When the application process using a DECLAREd temporary table terminates, the table is deleted.

DECLAREd temporary tables, unlike CREATEd temporary tables, may have indexes defined on the table, thereby giving the DB2 optimizer additional access paths to consider, which usually results in better query performance. DECLAREd temporary tables may also have check constraints defined on them.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-18 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 150: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 4-12. Temporary tables CE1319.4

Notes:

A declared temporary table is only available to SQL statements that are issued by the application or session that created it. It does not persist beyond the duration of the connection to the database.

Temporary tables can be used to make complicated queries easier, by enabling a working table and can improve the performance of some queries or applications, because, for example, it is not in the DB2 Catalog. Since the introduction of nested table expressions (providing some similar capability), the need for defining temporary tables may be declining.

In addition to ON COMMIT PRESERVE ROWS, there is also ON COMMIT DELETE ROWS. This option controls whether or not DB2 retains its cursor position in the FETCH processing of result set rows after the application issues a COMMIT statement.

© Copyright IBM Corporation 2007, 2013

Temporary tablesDECLARE GLOBAL TEMPORARY TABLE T1

LIKE REAL_T1

ON COMMIT PRESERVE ROWS

INSERT INTO SESSION.T1

SELECT FROM REAL_T1 WHERE DEPTNO=’A00’

– Now T1 is available until the end of the session

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-19

Page 151: Ce 1319 Stud

Student Notebook

.

Figure 4-13. Checkpoint CE1319.4

Notes:

Write your answers here:

1.

2.

© Copyright IBM Corporation 2007, 2013

Checkpoint

1. T/F. CASE can be coded in both the SELECT-list and in the WHERE clause.

2. T/F. CAST specifications can be used to cast the value NULL to a parameter.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-20 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 152: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 4-14. Unit summary CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Unit summary

Having completed this unit, you should be able to:• Identify when CASE expressions can be useful• Code CASE expressions in the SELECT list and in the

WHERE clause• Identify when CAST specifications can be used• Identify the advantages of using Summary (Materialized

Query) Tables and Temporary tables

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 4. CASE, CAST, Summary (Materialized Query) Tables and 4-21

Page 153: Ce 1319 Stud

Student Notebook

.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

4-22 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 154: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 5. Using Subqueries

What this unit is about

It is often useful to embed one query within another. This enables the inner query to get an answer to a question and then plug that answer into the outer query to control which rows are returned by the outer query. This inner query is called a subquery.

When working with subqueries, the challenge is to how to approach problem so that the subquery is correctly written.

What you should be able to do

After completing this unit, you should be able to:

• Code subqueries using the ALL, ANY/SOME, and EXISTS keywords

• Code correlated subqueries

• Choose the proper type of subquery to use in each case

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-1

Page 155: Ce 1319 Stud

Student Notebook

.

Figure 5-1. Unit objectives CE1319.4

Notes:

It is often useful to embed one query within another. This enables the inner query to get an answer to a question and then plug that answer into the outer query to control which rows are returned by the outer query. This inner query is called a subquery.

© Copyright IBM Corporation 2007, 2012

Unit objectives

After completing this unit, you should be able to:• Code subqueries using the ALL, ANY/SOME, and EXISTS

keywords• Code correlated subqueries• Choose the proper type of subquery to use in each case

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 156: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-2. Subquery with basic predicate CE1319.4

Notes:

A basic predicate is one that uses single value comparison operator (=, <, >, or any combination thereof). A subquery in a basic predicate must not return more than one value. In this example, we look for the employee(s) who earned the highest salary.

The SELECT statement in the example above executes as follows:

• Step 1 - The subquery will determine the highest salary and it will be compared with the salaries of all employees.

• Step 2 - If the salary of the employee row currently being read matches the value provided by the subquery, the result of the predicate is true and the row becomes part of the result set that will eventually be returned to the user or application program. Otherwise the row is bypassed, and DB2 repeats Step 2 with the next row in the table.

It is possible that several people may earn the same salary. If the maximum salary for the table is earned by multiple employees, the subquery would still return just that one value but the outer query would return all rows of the employees who made that exact salary figure.

© Copyright IBM Corporation 2007, 2012

Who has thehighest yearly

salary?

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

EMPNO LASTNAME SALARY

000010 52750.00

SELECT EMPNO, LASTNAME, SALARYFROM EMPLOYEEWHERE SALARY =

(SELECT MAX(SALARY)FROM EMPLOYEE);

MAX(SALARY)

52750.00

Subquery with basic predicate

HAAS

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-3

Page 157: Ce 1319 Stud

Student Notebook

.

Note

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 158: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-3. Subquery with IN predicate CE1319.4

Notes:

The IN predicate compares a value or values with a collection of values. The fullselect must identify a number of columns that is the same as the number of expressions specified to the left of the IN keyword. The fullselect may return any number of rows.

The IN predicate, when used with a subquery, enables the outer query to compare the values in columns of the outer table with a list of comparable values provided by the subquery. If any of the non-null values returned by the subquery match any of the non-null values of the column being searched by the outer query, the rows in the outer query that contain matching values will appear in the final result set. Nulls in the subquery result will never match nulls in the outer query result.

Only SALLY KWAN is currently assigned to a project that belongs to her department.

Also note the syntax of the WHERE clause. In later releases of most DB2 platforms, it is possible to use the syntax in the example here.

A list of items enclosed in parenthesis can be compared with a list of values, also enclosed in parenthesis, on the right hand side of the comparison operator. Both lists must contain

© Copyright IBM Corporation 2007, 2012

Which employees areresponsible for projects

within their department?

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

PROJNAMEPROJNO RESPEMPDEPTNO

USER EDUCATION

ADMIN SERVICES

IF2000

AD3100IF1000 QUERY SERVICES

000010000030000030

D01C01C01

PROJECT

SELECT EMPNO, LASTNAMEFROM EMPLOYEEWHERE (EMPNO,WORKDEPT) IN

(SELECT RESPEMP, DEPTNO FROM PROJECT);RESPEMP DEPTNO

000030 C01000010 D01

Subquery with IN predicate

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-5

Page 159: Ce 1319 Stud

Student Notebook

.

the same number of items and the items must be of compatible data types. The comparison operator must be equal or not equal.

Check the reference manuals for your environment and release to determine if the syntax is supported.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 160: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-4. Subquery with NOT IN predicate CE1319.4

Notes:

The subquery in the example builds a list of the employee numbers of employees that are responsible for projects (RESPEMP in the PROJECT table). The outer query determines the employee number and last name of any employee whose employee number is NOT in the list returned by the subquery. In other words, the query is reporting on employees who are not responsible for projects.

When IN is prefaced with NOT, as in the example on the visual, the subquery should be written to return only non-null values. Then, 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. If the NOT IN subquery returns a null value, the outer query will always produce an empty result set.

There are three methods available to ensure that the subquery does not return the null value:

1. Defining the columns in the subquery as NOT NULL

2. Coding the WHERE IS NOT NULL predicate in the subquery

© Copyright IBM Corporation 2007, 2012

Which employeesare not responsible for

projects?

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

PROJNAMEPROJNO RESPEMPDEPTNO

USER EDUCATION

ADMIN SERVICES

IF2000

AD3100IF1000 QUERY SERVICES

000010000030000030

D01C01C01

PROJECT

SELECT EMPNO, LASTNAMEFROM EMPLOYEEWHERE EMPNO

(SELECT RESPEMPFROM PROJECT);

RESPEMP

000030000010NOT IN

Subquery with NOT IN predicate

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-7

Page 161: Ce 1319 Stud

Student Notebook

.

3. Using the COALESCE function

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 162: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-5. NOT IN predicate for nullable column CE1319.4

Notes:

As mentioned in conjunction with the previous visual, it is necessary to be careful with nulls in queries using NOT IN. This is illustrated by the visual.

For this example, it is assumed that employee WILSON has been added to the EMPLOYEE table with an employee number of 000400, a salary of 25400.00, but without a work department.

If you want to find all departments for which no employees are working, you might be tempted to use the following query:

SELECT DEPTNO, DEPTNAME FROM DEPARTMENT WHERE DEPTNO NOT IN (SELECT WORKDEPT FROM EMPLOYEE) ;This would not work. The result set would be empty. The subquery result set would contain the department numbers A00, C01, A00, C01, C01, and null (for WILSON). That

© Copyright IBM Corporation 2007, 2012Wrong result!

Which departments have no employees?

DEPARTMENT

DEPTNAMEDEPTNO MGRNO

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE DIV.

D01

A00C01 INFORMATION CENTER

000010000030

DEPTNAMEDEPTNO

EMPTY!!!

SELECT DEPTNO, DEPTNAMEFROM DEPARTMENTWHERE DEPTNO

(SELECT WORKDEPTFROM EMPLOYEE);

WORKDEPT

C01A00

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140000400

HAASKWANO'CONNELLQUINTANANICHOLLSWILSON

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.0025400.00NULL

C01NULL

A00

You must avoidthe NULL value!

NOT IN

NOT IN predicate for nullable column

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-9

Page 163: Ce 1319 Stud

Student Notebook

.

would make the whole predicate in the outer query evaluate to UNKNOWN, rather than TRUE or FALSE, and no rows in the DEPARTMENT table would satisfy the query.

To get the desired result, you must change the query to:

SELECT DEPTNO, DEPTNAME FROM DEPARTMENT WHERE DEPTNO NOT IN (SELECT WORKDEPT FROM EMPLOYEE WHERE WORKDEPT IS NOT NULL) ;

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 164: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-6. Subquery with ORDER BY CE1319.4

Notes:

If the result set is an ordered result set you can restrict this with FETCH FIRST n ROWS ONLY. The result set only contains n rows ordered by the ORDER BY clause.

© Copyright IBM Corporation 2007, 2012

Example: Show only employees that are in the last three departments (alphabetically) in the department table

SELECT EMPNO, LASTNAME, WORKDEPTFROM EMPLOYEEWHERE WORKDEPT IN

( SELECT DEPTNO FROM DEPARTMENTORDER BY DEPTNO DESCFETCH FIRST 3 ROWS ONLY );

Subquery with ORDER BY

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-11

Page 165: Ce 1319 Stud

Student Notebook

.

The intermediate result of the subquery in fig. 5-6 is: E21, E11 and E01 (DEPTNO column values in DESCending order). Therefore the final result set for the query is:

10 record(s) selected.

EMPNO LASTNAME WORKDEPT------ ------------ ------------000050 GEYER E01000090 HENDERSON E11000100 SPENSER E21000280 SCHNEIDER E11000290 PARKER E11000300 SMITH E11000310 SETRIGHT E11000320 MEHTA E21000330 LEE E21000340 GOUNOT E21

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-12 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 166: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-7. Subquery with ALL predicate (1 of 2) CE1319.4

Notes:

The purpose of the query is to determine the department with the highest salary costs. For that department, its department number and salary cost should be listed.

To solve the posed problem, we formulate the purpose of the query more precisely: Find the department(s) for which the sum of all salaries for employees working for the department(s), that is, the total salary paid by the department(s), is the highest of all departments. Note that there may be more than one department with the highest total salary.

To find the greatest value out of a set of values, you must determine if the value of the set that is greater than or equal to all values of the set including the value itself.

• The requested information can be extracted from the EMPLOYEE table.

• To find the total salaries for the departments, you need to use the SUM() column function and group the salaries by departments (WORKDEPT).

Since the outer query needs to select departments rather than employees, a HAVING clause is needed rather than a WHERE clause. The HAVING clause must express the

© Copyright IBM Corporation 2007, 2012

Which department

has the highestsalary cost?

SELECT WORKDEPT AS DEPTNO,SUM(SALARY) AS TOTAL_SALARY FROM EMPLOYEEGROUP BY WORKDEPTHAVING

Sum of salaries forALL departments

Sum of salaries >= SUM(SALARY)82000.0090470.00

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

Subquery with ALL predicate (1 of 2)

A00 SUM

C01 SUM

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-13

Page 167: Ce 1319 Stud

Student Notebook

.

condition that, for a summary row of the outer query to be displayed, the total salary (sum of salaries for the department) must be higher than or equal to the sum of salaries for all departments as illustrated by the visual.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-14 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 168: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-8. Subquery with ALL predicate (2 of 2) CE1319.4

Notes:

A quantified predicate compares each value in the outer query with the set of values provided by the subquery. A quantified predicate is one that uses one of the following keywords: ALL, ANY, or SOME. As usual, the subselect must specify a single result column. It may return any number of values.

When the ALL keyword is specified, the result of the predicate is:

• True if the subquery returns no values or if the specified relationship is true for every value returned by the subquery.

• False if the specified relationship is false for at least one value returned by the subquery.

• Unknown if the specified relationship is not false for any values returned by the subquery and at least one comparison is unknown because of a null value. Unknown is treated as false.

The subquery in the example provides a list of values. Each value is the total salary paid by a department to the employees working for that department. There is one value for each

© Copyright IBM Corporation 2007, 2012

Which department

has the highestsalary cost?

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

DEPTNO TOTAL_SALARY

C01 90470.00

SELECT WORKDEPT AS DEPTNO,SUM(SALARY) AS TOTAL_SALARY FROM EMPLOYEEGROUP BY WORKDEPTHAVING

ALL (SELECT SUM(SALARY)FROM EMPLOYEEGROUP BY WORKDEPT);

SUM(SALARY)>=SUM(SALARY)

82000.0090470.00

Subquery with ALL predicate (2 of 2)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-15

Page 169: Ce 1319 Stud

Student Notebook

.

department. The outer query ensures that a grouped row (by WORKDEPT) of the outer table is only displayed if the appropriate department's total salary is higher than or equal to every value (all values) in the list returned by the subquery. In the example, the total salary of only one of the departments in the outer result is higher than or equal to every value in the subquery result. Thus, that is the department with the highest salary cost.

Since we are trying to find the maximum SUM of the salaries grouped by WORKDEPT, a user may be inclined to think that a viable solution would be to utilize the MAX column function. However, column functions cannot be nested, and so MAX(SUM…) would be flagged as an SQL syntax violation.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-16 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 170: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-9. Subquery with ANY or SOME predicate (1 of 2) CE1319.4

Notes:

The task is to determine all employees whose salary is higher than the average salary of at least one department. For these employees, their employee number, last name, and salary should be listed.

We can reformulate the problem as follows, which indicates the predicate needed for the subquery: Determine all employees whose salary is higher than the average salary of any one of the departments. If you wanted, you could also say: “... higher than the average salary of some of the departments”. SQL recognizes both the ANY and SOME keywords as being valid syntax to invoke this function.

Note that the salaries of the employees are to be compared with the average salaries of the departments and not with the average salary for all employees. As indicated in the visual, the averages for the departments are 41000.00000000 (A00) and 30156.66666666 (C01), whereas the average salary for all listed employees would be 34494.00000000.

Because employees have to be determined rather than departments, no grouping is required for the outer query.

© Copyright IBM Corporation 2007, 2012

Subquery with ANY or SOME predicate (1 of 2)

Which employees havea salary that is higher thanthe average of at least one

department?

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

SELECT EMPNO, LASTNAME, SALARY FROM EMPLOYEEWHERE SALARY >

Average salary ofANY department

AVG(SALARY)

41000.0000000030156.66666666

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-17

Page 171: Ce 1319 Stud

Student Notebook

.

Figure 5-10. Subquery with ANY or SOME predicate (2 of 2) CE1319.4

Notes:

The predicate keywords ANY and SOME have the same meaning and effect. When either ANY or SOME are used, the result of the predicate is:

• True if the specified relationship is true for at least one value returned by the subquery.

• False if the subquery returns no value or if the specified relationship is false for every value returned by the subquery.

• Unknown if the specified relationship is not true for any values returned by the subquery and at least one comparison is unknown because of a null value.

The subquery in the example provides a list of values. Each value is the average salary paid by one department. To obtain the average salaries of the departments, grouping by WORKDEPT is necessary in the subquery.

The outer query compares the salary of each employee to the average-salary values for the departments in the list returned by the subquery. If the salary for an employee is higher than any of the values returned by the subquery, even the lowest one, the employee number, last name, and salary for the employee are displayed. In other words, if any

© Copyright IBM Corporation 2007, 2012

Subquery with ANY or SOME predicate (2 of 2)

Which employees havea salary that is higher thanthe average of at least one

department?

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

EMPNO LASTNAME SALARY

000010 HAAS 52750.00

SELECT EMPNO, LASTNAME, SALARY FROM EMPLOYEEWHERE SALARY >

ANY (SELECT AVG(SALARY)FROM EMPLOYEEGROUP BY WORKDEPT);

AVG(SALARY)

41000.0000000030156.66666666

000030 KWAN 38250.00

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-18 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 172: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

employee's individual salary is higher than the average salary for any of the departments, we will report on that individual.

In the example, the salary of employee HAAS is higher than all of the department averages. Employee KWAN qualifies because her salary is higher than the average salary of department C01 (her own), although her salary is not higher than the average salary of department A00.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-19

Page 173: Ce 1319 Stud

Student Notebook

.

Figure 5-11. Subquery with EXISTS predicate (1 of 2) CE1319.4

Notes:

In this example, the task is to produce a listing of all employees earning more than 30000, if there is at least one employee earning less than 25000. The listing should contain the employee number, last name, salary, and hiring date.

Let us reformulate the problem so that we can see more clearly which predicate must be used to solve the problem: List employee number, last name, salary, and hiring date for all employees if and only if there exists at least one employee who has a salary less than 25000.

Accordingly, the subquery must return a value of true if the EMPLOYEE table contains at least one employee with a salary less then 25000. The predicate is then true for all rows retrieved by the outer query. The subquery must return a value of false if no such employee exists. In this case, the predicate is false for all rows retrieved by the outer query resulting in no rows being displayed in the result set.

The result returned from the EXISTS predicate is either true or false. It does not return values or computed results as the other subquery predicates do.

© Copyright IBM Corporation 2007, 2012

I would like to have alist of all employees earning

more than 30,000, if there is any employee earning less than

25,000.

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

SELECT EMPNO, LASTNAME, SALARY, HIREDATEFROM EMPLOYEEWHERE SALARY > 30000 AND

EXISTS at least one employeeearning less than $25,000

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000130 QUINTANA C01 23800.00 1971-07-28

Subquery with EXISTS predicate (1 of 2)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-20 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 174: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-12. Subquery with EXISTS Predicate (2 of 2) CE1319.4

Notes:

The EXISTS predicate simply determines if something is true or not. If the EXISTS predicate is combined with other predicates in the outer query via AND, the outer query will only display a non empty final result if the condition in the subquery is true. The result of the EXISTS predicate is true if the result table returned by the subquery contains at least one row. Otherwise, the result is false.

An EXISTS predicate can be negated by preceding it with the keyword NOT.

The subquery in the example determines if there are any employees in the EMPLOYEE table that has a salary less than 25000. As indicated below the SQL statement, there is one such row, namely, the row for employee QUINTANA with employee number 000130 and a salary of 23800. The EXISTS predicate does not return this row. It only returns true to indicate that the row exists in table EMPLOYEE. If no row existed in table EMPLOYEE satisfying the WHERE condition of the subquery, false would be returned.

The EXISTS predicate is not the only predicate of the WHERE clause for the outer query, but is in this case true for all rows that qualify the other predicate(s) of the outer query. The outer query returns employee number, last name, salary, and hiring date of all employees

© Copyright IBM Corporation 2007, 2012

I would like to have alist of all employees earning

more than 30,000, if there is any employee earning less than

25,000.

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000130000140

QUINTANANICHOLLS

C01C01

23800.0028420.00

000010000030

HAASKWAN

A00C01

52750.0038250.00

1965-01-011975-04-05

000120 O'CONNELL A00 29250.00 1963-12-051971-07-281976-12-15

SELECT EMPNO, LASTNAME, SALARY, HIREDATEFROM EMPLOYEEWHERE SALARY > 30000 AND

EXISTS (SELECT * FROM EMPLOYEEWHERE SALARY < 25000) ;

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000130 QUINTANA C01 23800.00 1971-07-28

Subquery with EXISTS Predicate (2 of 2)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-21

Page 175: Ce 1319 Stud

Student Notebook

.

in the EMPLOYEE table that have a salary greater than 30000. The EXISTS effectively acts like an 'IF': a non empty result is only displayed if someone has a salary less than 25000.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-22 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 176: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-13. Correlated subquery (1 of 2) CE1319.4

Notes:

In this example, the task is to find the employees who have a salary that is higher than the average salary of their department.

Again, let us reformulate the problem: List employee number, last name, and salary of all employees that have a salary that is higher than the average salary of their corresponding department, that is, the department to which the employee belongs.

In other words, we do not want to find all employees, whose salary is higher than the average salary of any department or of all departments as was the case for the ANY or ALL predicate, respectively. This time, the employee's salaries must be matched with the average salaries of the departments to which the employees belong. That means that there is a correlation between an employee and his/her department that must be expressed in the subquery determining the average salaries.

© Copyright IBM Corporation 2007, 2012

EMPLOYEE

Which employees havea salary that is higher than

the average of theirdepartment?

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

SELECT EMPNO, LASTNAME, SALARY FROM EMPLOYEEWHERE SALARY >

Average salary ofcorresponding department

AVG(SALARY)

41000.00000000A00AVG(SALARY)

30156.66666666C01

Correlated subquery (1 of 2)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-23

Page 177: Ce 1319 Stud

Student Notebook

.

Figure 5-14. Correlated subquery (2 of 2) CE1319.4

Notes:

Non-correlated subqueries execute the subquery once at the beginning and use the result to control the rows returned by the outer query. A correlated query works differently:

1. A row is read by the outer query

2. Then a value from that row is passed to the subquery

3. That value is then used to control which rows or values are returned by the subquery.

4. Then, the predicate in the outer query is compared to the subquery results to determine if the outer table row will appear in the final result set. This four-step process is repeated for each row of the outer table until each one has been examined and either written to the result set or omitted from it. Because of this behavior, correlated subqueries can be expensive from a performance standpoint.

In the example above, the outer query reads a row in the EMPLOYEE table and gets the employee number, last name, salary, and department (WORKDEPT). The (work) department for the employee is passed to the subquery. The subquery calculates the

© Copyright IBM Corporation 2007, 2012

Which employees havea salary that is higher than

the average of theirdepartment?

EMPNO LASTNAME SALARY

000030 KWAN000010 HAAS 52750.00

38250.00

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

SELECT EMPNO, LASTNAME, SALARY FROM EMPLOYEEWHERE SALARY >

E

(SELECT AVG(SALARY)FROM EMPLOYEEWHERE WORKDEPT =

WORKDEPT);

AVG(SALARY)

30156.66666666

C01AVG(SALARY)

41000.00000000

A00

Correlated subquery (2 of 2)

E.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-24 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 178: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

average salary for all employees in the work department that was passed from the outer row.

Then, the result of the subquery is compared to the individual salary of the employee whose row was read by the outer query. If the individual salary exceeds the result of the subquery, the outer table row is written to the result set; otherwise, it is ignored. This process is repeated for each row of the outer table.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-25

Page 179: Ce 1319 Stud

Student Notebook

.

Figure 5-15. Correlated subquery with EXISTS CE1319.4

Notes:

This visual shows use of the True/False indicator EXISTS in a correlated subquery. This is an alternate way to code an SQL query to get the same result as on Figure 5-5.

© Copyright IBM Corporation 2007, 2012

Which departments have no employees?

DEPARTMENT

DEPTNAMEDEPTNO MGRNO

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE DIV.

D01

A00C01 INFORMATION CENTER

000010000030

SELECT DEPTNO, DEPTNAMEFROM DEPARTMENT DWHERE NOT EXISTS

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY

000010000030000120000130000140000400

HAASKWANO'CONNELLQUINTANANICHOLLSWILSON

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.0025400.00NULL

(SELECT * FROM EMPLOYEEWHERE

WORKDEPT = D.DEPTNO);

D01 DEVELOPMENT CENTERDEPTNO DEPTNAME

RESULT

-

Correlated subquery with EXISTS

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-26 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 180: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-16. Scalar Fullselect CE1319.4

Notes:

A scalar fullselect is a fullselect, enclosed in parentheses, that returns a single value. The SQL syntax has now been enhanced to allow scalar fullselect. Subqueries, correlated subqueries, and expressions were all previously supported.

In all of the previous examples, the subquery was associated with the WHERE clause. Here we have an example of a subquery appearing in the SELECT clause of the query. The subselect calculating the average salary is executed each time a row is accessed that has WORKDEPT = ‘E11’.

© Copyright IBM Corporation 2007, 2012

Example: Display each employee in department E11, with their salary, and the average salary for their department SELECT lastname, salary,

(SELECT avg(salary)

FROM EMPLOYEE

WHERE WORKDEPT=E.WORKDEPT)

FROM EMPLOYEE E

WHERE WORKDEPT = 'E11' ;

Scalar Fullselect

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-27

Page 181: Ce 1319 Stud

Student Notebook

.

Figure 5-17. UPDATE including subquery CE1319.4

Notes:

Subqueries can also be used in UPDATE and DELETE statements. In the slide above DB2 finds the row with EMPNO = ‘000130’, and then updates the SALARY column value for that row to the AVG calculated by the subquery.

Here is an example of a DELETE statement with a subquery:

DELETE FROM EMPLOYEE WHERE LASTNAME NOT IN (SELECT SALES_PERSON FROM SALES WHERE YEAR(SALES_DATE)=2010) AND JOB IN ('SALESREP','FIELDREP') ;This would delete from the EMPLOYEE table all sales people and field representatives who did not make a sale in 2010.

© Copyright IBM Corporation 2007, 2012

UPDATE including subquery

Example:Update the salary for Quintana (EMPNO ‘000130’) to be the same as the average for her department.

UPDATE EMPLOYEE A

SET A.SALARY = (SELECT AVG(B.SALARY)

FROM EMPLOYEE B

WHERE A.WORKDEPT = B.WORKDEPT)

WHERE A.EMPNO = '000130‘ ;

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-28 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 182: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 5-18. Checkpoint CE1319.4

Notes:

Enter your answers below:

1.

2.

3.

© Copyright IBM Corporation 2007, 2012

Checkpoint

1. List three quantified predicates used with subqueries.

2. T/F. The EXISTS predicate can be negated.

3. Assume that you need to list the employees that have the highest salaries in their departments. What kind of subquery will you have to use?

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 5. Using Subqueries 5-29

Page 183: Ce 1319 Stud

Student Notebook

.

Figure 5-19. Unit summary CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2012

Unit summary

Having completed this unit, you should be able to:• Code subqueries using the ALL, ANY/SOME, and EXISTS

keywords• Code correlated subqueries• Choose the proper type of subquery to use in each case

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

5-30 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 184: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 6. Scalar Functions

What this unit is about

In this unit, we will discuss scalar functions and compare them with column functions.

What you should be able to do

After completing this unit, you should be able to:

• Extend your knowledge of scalar functions which:

- Manipulate arithmetic data

- Manipulate date values

- Manipulate character data

How you will check your progress

• Checkpoint

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-1

Page 185: Ce 1319 Stud

Student Notebook

.

Figure 6-1. Unit objectives CE1319.4

Notes:

A function is an operation denoted by a function name followed by one or more arguments which are enclosed in parentheses. Most functions have a single argument. The result of a function is a single value. Applying the function to its arguments derives this single value.

Functions are classified as scalar functions or column functions. The argument of a column function is a set of values from different rows. The argument of a scalar function is a single value.

In this unit, we will discuss scalar functions. Column functions were discussed in the SQL Basics course and were reviewed briefly at the beginning of this course. The next slide provides a basic comparison between column and scalar functions.

© Copyright IBM Corporation 2007, 2012

Unit objectives

After completing this unit, you should be able to:• Extend your knowledge of scalar functions which:

– Manipulate arithmetic data– Manipulate date values– Manipulate character data

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 186: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-2. Functions - Two types CE1319.4

Notes:

Column functions produce one summary or aggregate value for each set of rows.

Scalar functions produce one value for each row within the set of qualified rows.

The above column functions are the most commonly-used column functions. Each release of DB2 provides additional functions. Check the SQL manuals for your release of DB2 to see the supported functions.

© Copyright IBM Corporation 2007, 2012

FUNCTION_NAME(argument)

Column Functions(AKA SUMMARY functions)

SUMAVGMINMAXCOUNT(*)COUNT(DISTINCT column_name)

Scalar Functions

Functions – Two types

} valuevaluevaluevalue

function

value

function

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-3

Page 187: Ce 1319 Stud

Student Notebook

.

Figure 6-3. Scalar functions CE1319.4

Notes:

Scalar functions are fairly easy to use and enable the user to return the output in the format desired. Scalar functions also make it possible to do calculations involving dates, times, and timestamps. Depending upon the DB2 platform and version, the user has the capability of invoking almost 200 different scalar functions.

Some of the general categories of scalar functions include:

1. String manipulation

2. Date, time and timestamp comparisons and calculations

3. Mathematical functions

a. Algebraic

b. Statistical

c. Calculus

© Copyright IBM Corporation 2007, 2012

S U S A NS

Scalar functions

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 188: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-4. Scalar function - SUBSTR CE1319.4

Notes:

The SUBSTR() function is used to extract a portion of a string.

• string: Identifies the column or literal from which the result is derived. string must be a character string or a graphic string. If string is a character string, the result of the function is a character string. If it is a graphic string, the result of the function is a graphic string.

• start: Identifies the starting point at which the function will begin extracting data, that is, the position of the first character extracted. It must be a positive integer that is not beyond the end of string.

• length: Identifies the number of characters that the function must extract from string. The value of length cannot cause the extraction process to go beyond the end of string. If length is not specified, the number of characters returned is equal to the total length of the string minus the start value + 1 (in essence, all characters beginning at the start position, and continuing to the end of the string).

© Copyright IBM Corporation 2007, 2012

,length )SUBSTR(string, start

DEPARTMENT

DEPTNAMEDEPTNO MGRNO

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE DIV.

D01

A00C01 INFORMATION CENTER

000010000030

DEPTNAMEAREA

D0 DEVELOPMENT CENTERC0 INFORMATION CENTER

SELECT SUBSTR(DEPTNO,1,2) AS AREA, DEPTNAMEFROM DEPARTMENTWHERE DEPTNAME LIKE '%CENTER‘;

Scalar function – SUBSTR

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-5

Page 189: Ce 1319 Stud

Student Notebook

.

The example obtains information for departments called a center, that is, which name ends with the character string 'CENTER'. The desired information is the name of the department and the area to which the department belongs. The area is encoded in the first two characters of the department number. Therefore, the SUBSTR() function is used to extract the leftmost two characters of the department number (start=1, length=2). The appropriate column in the result, that is, the first column, is called AREA. The second column of the result is the name of the department.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 190: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-5. Position string CE1319.4

Notes:

The POSSTR scalar function returns the starting position of the first occurrence of the second string argument within the first string argument. POSSTR returns 0 (zero) if the string searched for cannot be found. It is therefore useful to check for the existence of one string within another, as well as at which position it occurs.

Notice the use of the UPPER (converts a character string to uppercase) scalar function in the WHERE clause. Without this conversion the POSSTR function would return 0 for any rows not containing the specific string ‘SON’ (capital S followed by capital O followed by capital N) in the LASTNAME column.

© Copyright IBM Corporation 2007, 2012

SELECT LASTNAME, POSSTR(LASTNAME, 'SON')FROM EMPLOYEE WHERE UPPER(LASTNAME) like '%SON%‘ ;

1 2 3 4 5 6 7 8 9

H E N D E R S O N

HENDERSON 7ADAMSON 5JEFFERSON 7JOHNSON 5

Position string

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-7

Page 191: Ce 1319 Stud

Student Notebook

.

Figure 6-6. Scalar function - COALESCE/VALUE (1 of 2) CE1319.4

Notes:

COALESCE() and VALUE() perform exactly the same function. They are synonymous with each other but COALESCE() is part of the ANSI/ISO standard and VALUE() is not. You should prefer to use the COALESCE() function and avoid any confusion with the VALUES keyword.

The function returns the first argument of its argument list that is not null. If all arguments are null, the function returns a null. For example, if an imaginary table contained three numeric columns named SALARY, COMMISSION, and BONUS, the expressions.

COALESCE(SALARY, COMMISSION, BONUS) or

VALUE(SALARY, COMMISSION, BONUS) would return:

• SALARY if salary is not null

• COMMISSION if salary is null and commission is not null

• BONUS if salary and commission are both null and bonus is not null

• Null if salary, commission and bonus are all null

© Copyright IBM Corporation 2007, 2012

DEPARTMENT

DEPTNAMEDEPTNO MGRNO

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE

D01

A00C01 INFORMATION CENTER

000010000030

)COALESCE(expression ,expression

DEPTNAMEDEPTNO MANAGER

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE

D01

A00C01 INFORMATION CENTER

000010000030NONE

SELECT DEPTNO, DEPTNAME,COALESCE(MGRNO,'NONE') AS MANAGERFROM DEPARTMENT ;

-

Scalar function – COALESCE/VALUE (1 of 2)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 192: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

The arguments must be compatible. Character string arguments are not compatible with datetime values. Thus, if any argument is a character string, all arguments must be character strings; if any argument is a date, all arguments must be dates; and so forth.

The example above displays the rows of the department table. If a department does not have a manager (MGRNO is null), the character string 'NONE' is displayed. The COALESCE function is used to achieve this. The appropriate column heading of the result set is named MANAGER.

COALESCE/VALUE is like a special kind of CASE expression and the same query can be written:

SELECT DEPTNO,DEPTNAME, CASE WHEN MGRNO IS NULL THEN 'NONE' ELSE MGRNO END AS MANAGER FROM DEPT ;

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-9

Page 193: Ce 1319 Stud

Student Notebook

.

Figure 6-7. Scalar function - COALESCE/VALUE (2 of 2) CE1319.4

Notes:

This visual shows how the COALESCE/VALUE function can be useful in an addition. The presence of the NULL value in a column can create difficulties when performing arithmetic or some column functions such as SUM or AVG.

In the visual above, observe that EMPNO 000130 has an unknown (NULL) salary and that EMPNO 000140 has an unknown (NULL) commission. To meet the requirement the query can also be written this way:

SELECT EMPNO, COALESCE(SALARY,0) + COALESCE(COMM,0)FROM EMPLOYEE ;The VALUE function can be specified in place of the COALESCE function. COALESCE should be used for conformance to SQL 2003 Core.

© Copyright IBM Corporation 2007, 2012

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY COMM

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.00

-28420.00

4220.003060.002340.001904.00

-

SELECT EMPNO,COALESCE(SALARY+COMM,SALARY,COMM,0) AS TOT_SALARYFROM EMPLOYEE ;

TOT_SALEMPNO

000010000030000120000130000140

56970.0041310.0031590.001904.00

28420.00

Prefer COALESCEover VALUE

Scalar function – COALESCE/VALUE (2 of 2)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 194: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-8. Scalar function - DECIMAL CE1319.4

Notes:

The DECIMAL() function returns a decimal representation of the value in its argument in the format DECIMAL(p,s), where p and s are the second and third argument. p is the precision of the decimal number and s the scale, that is, the number of decimal places.

The first argument must be a character string or a number. The second argument if specified, must range in value from 1 to 31. The third argument, if specified, must range in value from 1 to p, where p is the value of the second argument.

Default values for the second argument depend on the data type of the first argument.

The default value for the third argument is zero.

The example on the visual calculates the average salary of the employees in department C01. The first value displayed (AVERAGE_1) in the result is the average as displayed by DB2 when only the AVG column function is used. The displayed value has scale of 8, that is, 8 decimal places. The second result column (AVERAGE_2) also displays the average value, but after the DECIMAL() function has been used to truncate to two decimal places.

© Copyright IBM Corporation 2007, 2012

AVERAGE_2AVERAGE_1 AVERAGE_3

30156.6630156.66666666 30156.67

DECIMAL(expression ,integer

,integer

)

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

SELECT AVG(SALARY) AS AVERAGE_1,DECIMAL(AVG(SALARY),8,2) AS AVERAGE_2,DECIMAL(AVG(SALARY)+0.005,8,2) AS AVERAGE_3FROM EMPLOYEEWHERE WORKDEPT = 'C01’ ;

Scalar function – DECIMAL

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-11

Page 195: Ce 1319 Stud

Student Notebook

.

If you do not want to truncate the average to two decimal places, but want to round it to two decimal places, you must add 0.005 to the average before applying the DECIMAL() function (see AVERAGE_3) or use the ROUND() function, see next page.

There is also a scalar function called TRUNCATE() or TRUNC(). It returns its first argument to a precision determined by the second argument. When argument 2 is positive, argument 1 is truncated argument 2 places right of decimal point. When argument 2 is negative, argument 1 is truncated argument 2 places to the left of the decimal point.

The first argument can be any built-in numeric data type. The second argument has to be an INTEGER or SMALLINT. Example:

SELECT TRUNCATE(AVG(SALARY),+2) AS TRUN_1,TRUNC(AVG(SALARY),-1) AS TRUN_2 FROM EMPLOYEE WHERE WORKDEPT='C01' ;

TRUN_1 TRUN_2 ------------------------ ------------------------ +3,01566600000000E+004 +3,01500000000000E+004

TRUNCATE() or TRUNC() is available on DB2 for Linux, UNIX, and Windows and DB2 for z/OS. It returns expression 1 truncated to expression 2.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-12 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 196: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-9. Scalar function - ROUND CE1319.4

Notes:

The ROUND() function returns expression1 rounded to expression2 places right of the decimal point. If expression2 is negative, expression1 is rounded to the absolute value of expression2 places to the left of the decimal point.

The result data type depends on the data type of expression1. The result data type is:

• INTEGER if the first argument is INTEGER or SMALLINT.

• In DB2 for Linux, UNIX, and Windows: DOUBLE if the first argument is DOUBLE, DECIMAL or REAL.

• In DB2 for z/OS: DECIMAL if the first argument is DECIMAL.

The result is null if any argument is null.

The example on the visual calculates the average salary for the employees in department C01. The first value displayed (AVERAGE_1) in the result is the average as displayed by DB2 when only the AVG column function is used, as discussed on the previous page.

© Copyright IBM Corporation 2007, 2012

ROUND ( expression1, expression2 )

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

SELECT AVG(SALARY) AS AVERAGE_1,ROUND(AVG(SALARY),+2) AS AVERAGE_2,ROUND(AVG(SALARY),-1) AS AVERAGE_3,DECIMAL( ROUND(AVG(SALARY),-1) ,8,2) AS AVERAGE_4FROM EMPLOYEE

WHERE WORKDEPT = 'C01‘ ;

z/OS:

AVERAGE_2AVERAGE_1 AVERAGE_3 AVERAGE_4

+3.01566700000000E+004 +3.01600000000000E+004 30160.0030156.6700000030156.66666666 30160.00000000 30160.00

30156.66666666

Scalar function – ROUND

LUW:

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-13

Page 197: Ce 1319 Stud

Student Notebook

.

The second result column (AVERAGE_2) also displays the average value, but after the average has been rounded to two decimal places by means of the ROUND() function. The result is in DOUBLE.

The third result column (AVERAGE_3) also displays the average value, but after it has been rounded to -1 decimal places. The fourth result column (AVERAGE_4) also displays the average value after it has been rounded to -1 decimal places. This time the function DECIMAL has been used to limit the output from ROUND.

CEIL: Returns the smallest integer value greater than or equal to the argument.

FLOOR: Returns the largest integer value less than or equal to the argument.

SELECT CEIL(123.45678), FLOOR(123.45678) FROM SYSIBM.SYSDUMMY1;124. 123.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-14 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 198: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-10. Scalar function - DIGITS CE1319.4

Notes:

The DIGITS() function returns a character string representation of its argument. The argument must be an integer or a decimal number. If the argument is null, the function returns null.

The result of the function is a fixed-length character string representing the value of the argument without a sign or a decimal point. Leading zeros will be added if necessary to bring the value up to a minimum length. The length of the string is 5 for small integers, 10 for integers or p for decimal numbers with a precision of p.

The example displays the salary of employee NICHOLLS in two forms:

• The first column of the result (SALARY_1) shows the salary expressed as a character string that contains no decimal point.

• The second column of the result (SALARY_2) shows the integer portion of the salary by extracting the appropriate portion from the value for the first column by means of the SUBSTR() function. This demonstrates that the value returned by the DIGIT() function is indeed a character string as required by the SUBSTR() function.

© Copyright IBM Corporation 2007, 2012

SALARY_2SALARY_1

28420002842000

DIGITS(expression)

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

DECIMAL(9,2)

SELECT DIGITS(SALARY) AS SALARY_1,SUBSTR(DIGITS(SALARY),3,5) AS SALARY_2FROM EMPLOYEEWHERE EMPNO = '000140'

Datatype: char(9) char(5)

Scalar function – DIGITS

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-15

Page 199: Ce 1319 Stud

Student Notebook

.

Note

Since the SUBSTR() function only works on character strings, the above example demonstrates a technique frequently used to extract portions of numbers from numeric columns.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-16 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 200: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-11. Scalar functions - SQRT and POWER CE1319.4

Notes:

The SQRT function returns the square root of the numeric expression. Like the ROUND function, the DECIMAL function can be used to format the output to the required number of decimal points.

The POWER function returns the value of expression1 to the power of expression2. Again, the DECIMAL function can be used to format the output to the required number of decimal points. As you can see from the example, expression2 can be used for calculating roots as well as powers.

© Copyright IBM Corporation 2007, 2012

Scalar functions – SQRT and POWER

POWER(expression1, expression2)

SQRT(expression)

SELECT EDLEVEL,SQRT(EDLEVEL) AS SQRT1_EDLEVEL,DECIMAL(SQRT(EDLEVEL),8,2) AS SQRT2_EDLEVEL,POWER(EDLEVEL,0.5) AS SQRT3_EDLEVEL,DECIMAL(POWER(EDLEVEL,0.5),8,2) AS SQRT4_EDLEVEL,POWER(EDLEVEL,2) AS SQUARED_EDLEVEL

FROM EMPLOYEEWHERE WORKDEPT = ‘C01’ ;

EDLEVEL SQRT1_EDLEVEL SQRT2_EDLEVEL SQRT3_EDLEVEL SQRT4_EDLEVEL SQUARED_EDLEVEL

------- ----------------------- ------------- ----------------------- ------------- ---------------

20 +4.47213595499958E+000 4.47 +4.47213595499958E+000 4.47 400

16 +4.00000000000000E+000 4.00 +4.00000000000000E+000 4.00 256

18 +4.24264068711928E+000 4.24 +4.24264068711928E+000 4.24 324

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-17

Page 201: Ce 1319 Stud

Student Notebook

.

Figure 6-12. DB2 DATE, TIME and TIMESTAMP CE1319.4

Notes:

The visual shows the internal format, internal lengths and external lengths for DATE, TIME and TIMESTAMP data types. Internally, DB2 always stores dates, times and timestamps as unsigned, packed decimal numbers.

A timestamp is a seven-part value (year, month, day, hour, minute, second, and microsecond) that represents a date and a time including microseconds.

DB2 accepts any of the formats (USA, ISO, EUR, JIS) in an SQL statement (character string representation of dates, times, and timestamps). Thus, all three of the queries belowwould execute successfully:

© Copyright IBM Corporation 2007, 2012

Data TypeDATETIMETIMESTAMP

Internal Formatyyyymmddhhmmssyyyymmddhhmmssnnnnnn

Internal Length4 bytes3 bytes10 bytes

DB2 DATE, TIME and TIMESTAMP

• DATE, TIME, and TIMESTAMP data are internally stored in packed decimal format

• DATE, TIME and TIMESTAMP data may be externally displayed in a variety of formats … ISO, USA, etc.

External Length10 bytes8 bytes

26 bytes

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-18 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 202: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

SELECT EMPNO, LASTNAME, BIRTHDATEFROM EMPLOYEEWHERE BIRTHDATE = ‘1950-12-18’ ; SELECT EMPNO, LASTNAME, BIRTHDATEFROM EMPLOYEEWHERE BIRTHDATE = ’12/18/1950’ ; SELECT EMPNO, LASTNAME, BIRTHDATEFROM EMPLOYEEWHERE BIRTHDATE = ’18.12.1950’ ;

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-19

Page 203: Ce 1319 Stud

Student Notebook

.

Figure 6-13. Output formats for date and time CE1319.4

Notes:

The available date/time formats, and an example of a date and a time expressed in each format, are show in the visual above.

The pattern for dates is as follows, where Y is year, M is month, D is day:

• ISO (International Standards Organization) = YYYY-MM-DD

• USA = MM/DD/YY

• EUR (European Standard) = DD.MM.YYYY

• JIS (Japanese International Standard) = YYYY-MM-DD

The pattern for times is as follows, where H is hour, M is minute, S is second:

• ISO (International Standards Organization) = HH.MM.SS

• USA = HH:MM AM or HH:MM PM

• EUR (European Standard) = HH.MM.SS

• JIS (Japanese International Standard) = HH:MM:SS

© Copyright IBM Corporation 2007, 2012

Output formats for date and time

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-20 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 204: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

It is also possible to generate other formats for date and time, depending upon your needs (this is accommodated by the LOCAL format).

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-21

Page 205: Ce 1319 Stud

Student Notebook

.

Figure 6-14. Scalar function - CHAR (1 of 2) CE1319.4

Notes:

The CHAR() function returns a character-string representation of an expression.

The visual here shows how to convert a date/time value to a different format.

• When the first argument is a date, time or timestamp, the second argument is the name of a date/time format.

• The result of the function is a fixed-length character string.

• If the first argument is a date or a time, the result is the character string representation of that date or time in the format specified by the second argument.

• If the first argument is a timestamp, the second argument is not applicable and must be omitted.

• If the second argument is omitted, the representation of a date or time is in the default format defined during DB2 installation.

© Copyright IBM Corporation 2007, 2012

,LOCAL,JIS,EUR,USA,ISO

)CHAR(expression

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE000010

000030

000120

000130

000140

HAAS

KWAN

O'CONNELL

QUINTANA

NICHOLLS

A00

C01

A00

C01

C01

52750.00

38250.00

29250.00

23800.00

28420.00

1965-01-01

1975-04-05

1963-12-05

1971-07-28

1976-12-15

28.07.197107/28/19711971-07-28EUR_DATEUSA_DATEISO_DATE

SELECT CHAR(HIREDATE,ISO) AS ISO_DATE,CHAR(HIREDATE,USA) AS USA_DATE,CHAR(HIREDATE,EUR) AS EUR_DATEFROM EMPLOYEEWHERE EMPNO = '000130’ ;

Scalar function – CHAR (1 of 2)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-22 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 206: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

The example displays the hiring date of employee QUINTANA (employee number 000130) in three different formats: the ISO format (ISO_DATE), the USA format (USA_DATE), and the European format (EUR_DATE).

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-23

Page 207: Ce 1319 Stud

Student Notebook

.

Figure 6-15. Scalar function - CHAR (2 of 2) CE1319.4

Notes:

This visual shows the syntax for function CHAR() on different data types.

It returns a FIXED-LENGTH character-string representation of:

• Character string value if the first argument is any type of character string

• Integer number if the first argument is a SMALLINT or INTEGER

• Decimal number if the first argument is a decimal number

• Double-precision floating-point number if the first argument is a DOUBLE or REAL

• Datetime value if the first argument is a date, time or timestamp

If the first argument can be null, the result can be null. If the first argument is null, the result is null.

The example query shows how the decimal-character can be changed in a query to a ',' instead of the default '.'. (this “reversed” usage of the period and comma is very common in European nations). As shown on the visual, the result includes p digits, where p is the

© Copyright IBM Corporation 2007, 2012

EMPLOYEEEMPNO LASTNAME WORKDEPT SALARY HIREDATE000010

000030

000120

000130

000140

HAAS

KWAN

O'CONNELL

QUINTANA

NICHOLLS

A00

C01

A00

C01

C01

52750.00

38250.00

29250.00

23800.00

28420.00

1965-01-01

1975-04-05

1963-12-05

1971-07-28

1976-12-15

0023800,00

SELECT CHAR(SALARY,',') AS DEC_COMMAFROM EMPLOYEEWHERE EMPNO = '000130’ ;

CHAR(integer-expression )

CHAR(decimal-expression

DEC_COMMA

DECIMAL(9,2)

CHAR(floating-point-expression

,decimal-characterCHAR(decimal-expression

),integer

CHAR(character-expression

Scalar function – CHAR (2 of 2)

))

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-24 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 208: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

precision of the decimal-expression (with a preceding minus sign if the argument is negative).

Notice that the only valid punctuation allowed in a returned column or calculated value is the decimal point. There is no separation character to delineate thousand, millions, billions, etc. However, there are many query and report formatting software programs that do provide the user to further edit the contents of the result set.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-25

Page 209: Ce 1319 Stud

Student Notebook

.

Figure 6-16. Scalar function - LENGTH CE1319.4

Notes:

The LENGTH() function returns the length of its argument.

• The argument can be a value of any data type.

• The result of the function is an integer, unless the argument is a null, in which case the result is null.

• If the argument is a fixed-length character string, the result is the maximum width of the column as defined when the table was created.

• If the argument is a variable-length character string, the result is the actual length of the specific value being read from the column.

The example on the visual obtains the lengths of some columns for employee O'CONNELL (employee number 000120). The first column of the result (LENGTH_LASTNAME) displays the length of the employee's last name. The column LASTNAME of the EMPLOYEE table has been defined as VARCHAR. Therefore, the length returned specifies the actual number of characters stored for the last name. If the last name was stored with trailing

© Copyright IBM Corporation 2007, 2012

959

LENGTH(expression))

DIGITS_SALARYLENGTH_SALARYLENGTH_LASTNAME

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

DECIMAL(9,2)

SELECT LENGTH(LASTNAME) AS LENGTH_LASTNAME,LENGTH(SALARY) AS LENGTH_SALARY,LENGTH(DIGITS(SALARY)) AS DIGITS_SALARYFROM EMPLOYEEWHERE EMPNO = '000120'

Scalar function – LENGTH

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-26 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 210: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

blanks, these would be counted as well. Apparently, the name O'CONNELL has been stored without trailing blanks.

The second column of the result (LENGTH_SALARY) shows the internal length of the SALARY column which has been defined as DECIMAL(9,2) in the EMPLOYEE table. Internally, decimal numbers are stored in such a way that two digits occupy a byte. In addition, half a byte is needed for the sign of the decimal number. Of course, the decimal number must occupy a full number of bytes. If p is the precision of the decimal column, the number of bytes required is INTEGER(p/2)+1. For our example, it is INTEGER(9/2)+1 = 5, as shown in the result.

The third column of the result (DIGITS_SALARY) shows the number of digits for the SALARY column, that is, its precision. The DIGITS() function is first used to convert the salary of employee O'CONNELL into a character string without sign or decimal point, but with leading zeros to bring the length of the character string to the precision for the column. Thereafter, the LENGTH() function is applied to this character string and returns the precision for the column.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-27

Page 211: Ce 1319 Stud

Student Notebook

.

Figure 6-17. Scalar functions - LTRIM / RTRIM CE1319.4

Notes:

The LTRIM (Left TRIM) and RTRIM (Right TRIM) functions remove leading or trailing blanks in an expression. If the argument is null, the result is null.

The example can be done with the following SQL statement without changing the EMPLOYEE table:

WITH EMP (EMPNO,LASTNAME,WORKDEPT,SALARY,HIREDATE) AS (SELECT TRANSLATE(SUBSTR(EMPNO,1,5),' ','0')||'0' AS EMPNO, LASTNAME, WORKDEPT, SALARY, HIREDATE FROM EMPLOYEE WHERE EMPNO IN ('000010','000030','000120','000130','000140'))

SELECT CHAR(LTRIM(EMPNO),10) AS STRIPPED_EMPNO,LENGTH(RTRIM(LASTNAME)) AS LENGTH_NAMEFROM EMPWHERE EMPNO IN (' 30',' 120',' 140');

© Copyright IBM Corporation 2007, 2012

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE10

30

120

130

140

HAAS

KWAN

O'CONNELL

QUINTANA

NICHOLLS

A00

C01

A00

C01

C01

52750.00

38250.00

29250.00

23800.00

28420.00

1965-01-01

1975-04-05

1963-12-05

1971-07-28

1976-12-15

LENGTH_NAMESTRIPPED_EMPNO

430 91208140

SELECT CHAR(LTRIM(EMPNO),10) AS STRIPPED_EMPNO,LENGTH(RTRIM(LASTNAME)) AS LENGTH_NAMEFROM EMPLOYEEWHERE EMPNO IN(' 30',' 120',' 140')

LTRIM(expression )

RTRIM(expression )

Assume leading '0's havebeen translated to blanks

Scalar functions – LTRIM / RTRIM

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-28 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 212: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

This results in the following output:

STRIPPED_EMPNO LENGTH_NAME ---------+---------+--------30 4 120 9 140 8

The example displays information for the employees with the employee numbers 30, 120, and 140. The employee number, EMPNO, has been changed to leading blanks in the EMPLOYEE table.

The first column of the result (STRIPPED_EMPNO) contains the employee numbers of the employees where the leading blanks have been removed. The length of result column STRIPPED_EMPNO has been set to 10 in the CHAR() function. Remember that the EMPNO column has been defined as CHAR(6) in the EMPLOYEE table.

The second column contains the lengths of the last names when any trailing blanks have been stripped away.

There is also a very useful function, STRIP().

• The function STRIP() removes leading, trailing, or both leading and trailing occurrences of a specified strip-character from the value provided by the first argument. - The first argument must be a string expression.

• The second argument indicates whether characters are removed from the beginning, the end, or both the beginning and the end of the string provided by the first argument. The default for the second argument is to remove the characters from both ends of the string.

• The third argument indicates the character to be removed. The character must be enclosed in apostrophes. The default value of the third argument is the blank (' ') character.

The previous example is changed to use the STRIP function instead of the LTRIM function as shown below:

WITH EMP (EMPNO,LASTNAME,WORKDEPT,SALARY,HIREDATE) AS (SELECT TRANSLATE(SUBSTR(EMPNO,1,5),' ','0')||'0' AS EMPNO,LASTNAME, WORKDEPT, SALARY, HIREDATE FROM DSN8910.EMP WHERE EMPNO IN ('000010','000030','000120','000130','000140'))

SELECT CHAR(STRIP(EMPNO,LEADING,'0'),10) AS STRIPPED_EMPNO, LENGTH(RTRIM(LASTNAME)) AS LENGTH_NAME FROM EMP WHERE EMPNO IN (' 30',' 120',' 140');

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-29

Page 213: Ce 1319 Stud

Student Notebook

.

This results in the following output:

--------+---------+---------STRIPPED_EMPNO LENGTH_NAME ---------+---------+-------- 30 4 120 9 140 8

Notice the leading blanks in the first column in the output.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-30 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 214: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-18. Date-related scalar functions (1 of 4) CE1319.4

Notes:

The date-related functions include DATE(), YEAR(), MONTH(), DAY(), DAYS(), DAYOFYEAR(), DAYOFMONTH(), DAYOFWEEK and JULIAN_DAY. The first four functions extract a portion of their argument, namely, the full date, the year, the month, or the day, respectively. The DAYS() function also works with dates, but in a different way than the other date functions.

Note

The DAYS() function is NOT the same as the DAY() function, despite the similarity of the names.

The DAYS() function determines the difference between the date in the argument and the date of January 1, 0001 and expresses the difference in days.

© Copyright IBM Corporation 2007, 2012

DATE(expression))

YEAR(expression))

MONTH(expression))

DAY(expression))

DAYS(expression))

Date-related scalar functions (1 of 4)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-31

Page 215: Ce 1319 Stud

Student Notebook

.

We can determine the difference between two dates by subtracting one date from the other date. The difference, which is expressed as a decimal number with no decimal places, represents the number of years, months and days that separate the two dates. If the first date in the expression is later than the second date in the expression, the difference is preceded by a minus sign.

A date in a table can also be subtracted from a literal value representing a valid date or vice versa. The literal need not be the argument of a DATE function, but can be. However, a literal representing a date cannot be subtracted from another literal representing a date.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-32 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 216: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-19. Date-related scalar functions (2 of 4) CE1319.4

Notes:

The example on the visual uses the hiring date of employee KWAN to demonstrate the date-related scalar functions and perform some calculations with dates.

• The first two columns of the result (HIREDATE and EUR_DATE) show the hiring date when displaying column HIREDATE and when converting it to the European format by means of the CHAR() function. As you can see from the first column, the default format established for the system during DB2 installation is the ISO format.

• The third column of the result (DATE), applies the DATE() function to the character string, the European date, returned by the CHAR() function. It converts the character string again into a date which is displayed in the default date format for the system, that is, the ISO format.

• The fourth (YEAR), fifth (MONTH), and sixth (DAY) columns extract the year, the month, and the day from the hiring date of employee KWAN by means of the YEAR(), MONTH(), or DAY() function, respectively. You should realize that leading zeros are removed.

© Copyright IBM Corporation 2007, 2012

HIREDATE

1975-04-05EUR_DATE

05.04.1975DATE

1975-04-05YEAR

1975MONTH

4DAY

5DAYS

721083PERIOD

291129PERIOD_IN_DAYS

10957Y2K

1999-12-31

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

SELECT HIREDATE, CHAR(HIREDATE,EUR) AS EUR_DATE,DATE(CHAR(HIREDATE,EUR)) AS DATE,YEAR(HIREDATE) AS YEAR, MONTH(HIREDATE) AS MONTH,DAY(HIREDATE) AS DAY, DAYS(HIREDATE) AS DAYS,DATE('2005-04-04')-HIREDATE AS PERIOD,DAYS('2005-04-04')-DAYS(HIREDATE) AS PERIOD_IN_DAYS,HIREDATE+('01/01/2000'-HIREDATE) AS Y2K

FROM EMPLOYEEWHERE YEAR(DATE(‘2005-04-04’)- HIREDATE) BETWEEN 29 AND 30 ;

Date-related scalar functions (2 of 4)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-33

Page 217: Ce 1319 Stud

Student Notebook

.

• The seventh column of the result (DAYS) presents the hiring date of employee KWAN as one less than the number of days since January 1, 0001, by using the DAYS() function. The value returned by the DAYS() function is an integer.

• The eighth column (PERIOD) is derived by first converting the character string '2005-04-04' in a date by means of the DATE() function and, then, subtracting the hiring date of the employee. The result is a decimal date duration that specifies the number of years, month, and days between the two dates in this order. The data type of the result is DECIMAL(8,0). Because leading zeros are suppressed in the displayed result, the displayed value has to be interpreted from right to left. A value of '608.' means that the duration is 6 months and 8 days. No years are shown in this case.

The result is a little bit surprising. Because the hiring date of employee KWAN is '1975-04-05', you might have expected a value of '291130.'. April 4, 2004, is just one day less than 30 years from April 5, 1975. Since 30 years is equivalent to 29 years, 11 months, and 31 days, 30 years minus one day should be 29 years, 11 months, and 30 days, that is, a decimal date duration of '291130.'.

However, this is not the way DB2 calculates the difference between two dates. If DATE1 and DATE2 are two dates which you want to subtract, the following procedural description clarifies the steps involved in the operation RESULT = DATE1-DATE2:

Step 1

If DAY(DATE2) <= DAY(DATE1) then DAY(RESULT) = DAY(DATE1)-DAY(DATE2). else do DAY(RESULT) = N+DAY(DATE1)-DAY(DATE2) where N = the last day of MONTH(DATE2). MONTH(DATE2) = MONTH(DATE2)+1. end

Step 2

If MONTH(DATE2) <= MONTH(DATE1) then MONTH(RESULT) = MONTH(DATE1)-MONTH(DATE2). else do MONTH(RESULT) = 12+MONTH(DATE1)-MONTH(DATE2). YEAR(DATE2) = YEAR(DATE2)+1. end

Step 3

YEAR(RESULT) = YEAR(DATE1)-YEAR(DATE2).

For our example, DATE1 is '2005-04-04' and DATE2 is '1975-04-05'. Since DAY(DATE2) is 5 which is greater than DAY(DATE1), DAY(RESULT) is N+4-5 where N is the last day of MONTH(DATE2), that is, the last day of April which is 30. Accordingly, the day for the result is 29 as shown on the visual. This shows that dates should not be subtracted because normal mathematical rules do not apply for date calculations. Rather, we should calculate the difference in days as done for column PERIOD_IN_DAYS of the result.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-34 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 218: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

The ninth column PERIOD_IN_DAYS shows the number of days you get by first converting '2005-04-04' and the hiring date of employee KWAN into the respective number of days since January 1, 0001, and then subtracting the two integers obtained from the DAYS() function. For this calculation, the normal mathematical rules apply because integers are subtracted.

The tenth and final column of the result (Y2K) adds the decimal date duration, resulting from subtracting the hiring date of the employee from the date '2000-01-01', to the hiring date of employee KWAN. At the first glance, you would expect the result to be the date '2000-01-01' as simple mathematical rules for adding and subtracting numbers suggest. But beware of this trap. You are not performing an integer or decimal calculation, but a date calculation. By subtracting the hiring date of the employee from '2000-01-01', you obtain a decimal date duration for which the algorithm mentioned for the PERIOD column of the result applies. The appropriate decimal date duration is '240826.'. Adding this date duration to the hiring date (1975-04-05) results in date of '1999-12-31', and not '2000-01-01', because DB2 adds the years of the duration to the year of the date, the months of the duration to the month of the date, and the days of the duration to day of the date. (Of course, overflows are taken into account.)

This underlines again how careful you must be with date or date-duration calculations. They just do not follow the normal number arithmetic. To get the expected result, you have to calculate the difference between the two dates in days, by applying the DAYS() function to both dates and subtracting the results, and then add the difference to the hiring date. However, labeled durations are required for this. We will discuss labeled durations later in this unit.

In the WHERE statement, we selected KWAN based on how many years it is between her hiring date and the date 2005-04-04. This is done by subtracting the HIREDATE from the 2005-04-04 date in the same way as the PERIOD column above, and then applying the YEAR() function to extract the year part only. MONTH() and DAY() could also be applied to this form of date subtraction to extract the other constituent parts.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-35

Page 219: Ce 1319 Stud

Student Notebook

.

Figure 6-20. Date-related scalar functions (3 of 4) CE1319.4

Notes:

This visual lists some additional date-related scalar functions.

• The DAYOFYEAR() function returns the day of the year in argument as an integer value in the range 1-366.

• The WEEK() function returns the week of the year of the argument as an integer value in the range 1-54. The week starts with Sunday.

• The DAYOFWEEK() function returns the day of the week in the argument as an integer value in the range 1-7, where 1 represents Sunday.

• The WEEK_ISO() function returns the week of the year of the argument as an integer value in range 1-53. The week starts with Monday. Week 1 is the first week of the year to contain a Thursday, which is equivalent to the first week containing January 4.

• The DAYOFWEEK_ISO() function returns the day of the week in the argument as an integer value in the range 1-7, where 1 represents Monday.

© Copyright IBM Corporation 2007, 2012

DAYOFYEAR(expression))

DAYOFWEEK_ISO(expression))

WEEK(expression))

JULIAN_DAY(expression)

WEEK_ISO(expression))

DAYOFWEEK(expression))

Date-related scalar functions (3 of 4)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-36 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 220: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• The JULIAN_DAY() function returns an integer value representing a continuous count of days and fractions since noon Universal Time on January 1, 4712 B.C. to the date value specified in the argument.

For DAYOFYEAR, DAYOFWEEK, DAYOFWEEK_ISO, WEEK, WEEK_ISO and JULIAN_DAY, the argument must be a date, timestamp, or valid character string representation of a date or timestamp that is neither a CLOB nor a LONG VARCHAR. If the argument is null, the result is null.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-37

Page 221: Ce 1319 Stud

Student Notebook

.

Figure 6-21. Date-related scalar functions (4 of 4) CE1319.4

Notes:

The example on the visual uses the hiring date of employee KWAN (April 4, 1975) to demonstrate some more date-related scalar functions.

• The first column of the result (HIREDATE) shows the hiring date with default format as previously.

• The second column of the result (DAYOFYEAR), applies the function DAYOFYEAR() which returns the day off the year as an integer. HIREDATE 1965-01-01 for HAAS is the first day of the year.

• The third column of the result (DAYOFWEEK), applies the function DAYOFWEEK() which returns the day of the week in the argument as an integer where 1 represents Sunday. (HAAS was hired on a Friday.)

• The fourth column of the result (DAYOFWEEK_ISO), applies the function DAYOFWEEK_ISO() which returns the day of the week in the argument as an integer where 1 represents Monday. (HAAS was hired on a Friday.)

© Copyright IBM Corporation 2007, 2012

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

SELECT HIREDATE, DAYOFYEAR(HIREDATE) AS DAYOFYEAR,DAYOFWEEK(HIREDATE) AS DAYOFWEEK,DAYOFWEEK_ISO(HIREDATE) AS DAYOFWEEK_ISO,CHAR(DAYNAME(HIREDATE),9) AS DAYNAME,WEEK(HIREDATE) AS WEEK,WEEK_ISO(HIREDATE) AS WEEK_ISO,JULIAN_DAY(HIREDATE) AS JULIAN_DAY,MOD(JULIAN_DAY(HIREDATE),7)+1 AS JULIAN_WEEKDAYFROM EMPLOYEEWHERE EMPNO = '000010’ ;

HIREDATE DAYOFYEAR WEEKJULIAN_

WEEKDAYDAYOFWEEK DAYNAMEDAYOFWEEK

_ISOWEEK_ISO JULIAN_DAY

61965-01-01 51 531 5Friday 2438762

Date-related scalar functions (4 of 4)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-38 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 222: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• The fifth column of the result (DAYNAME), applies the function DAYNAME() which returns the name of the weekday. (It confirm that HAAS was hired on a Friday.) Note that the scalar function called DAYNAME() is only available on DB2 UDB for Linux, UNIX, and Windows. It returns a mixed case character string containing the name of the day (for example, Friday) for the day portion of the argument. There is another function, called MONTHNAME(), which works in exactly the same way for months.

• The sixth column of the result (WEEK), applies the function WEEK() which returns the week of the year as an integer. The week starts with Sunday. HIREDATE 1965-01-01 for HAAS is in the first week of the year.

• The seventh column of the result (WEEK_ISO), applies the function WEEK_ISO() which returns the week of the year as an integer. The week starts with Monday. HIREDATE 1965-01-01 for HAAS is week 53 of the previous year.

• The eighth column of the result (JULIAN_DAY), applies the function JULIAN_DAY() which returns an integer value representing the number of days from January 1, 4712 B.C. (start date of the Julian calendar).

• The ninth column of the result (JULIAN_WEEKDAY) is yet another way of finding the day of the week of the HIREDATE as an integer, this time using JULIAN_DAY. MOD is used to return the remainder of dividing the JULIAN_DAY by 7; it is then necessary to add 1 to return the day of the week in the same format as the DAYOFWEEK_ISO function, where 1 represents Monday.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-39

Page 223: Ce 1319 Stud

Student Notebook

.

Figure 6-22. Time/Timestamp-related scalar functions (1 of 2) CE1319.4

Notes:

The time and timestamp related scalar functions include TIME(), HOUR(), MINUTE(), SECOND(), MICROSECOND(), and TIMESTAMP(). The first five functions extract a portion of their argument, namely, the time, the hour, the minute, the second, or the microseconds, respectively.

The TIMESTAMP() function returns a timestamp derived from its arguments. The TIMESTAMP() function allows you to specify one or two arguments. If only one argument is specified, this argument must represent a timestamp. If two arguments are specified, the first argument must represent a date and the second a time. The returned timestamp then contains the date specified by the first argument and the time provided by the second argument.

You can determine the difference between two times or two timestamps by subtracting one from the other. When we subtract one time from another time, a decimal time duration results, which specifies the number of hours, minutes and seconds between the two times. The data type of the result is DECIMAL(6,0). When we subtract one timestamp from another timestamp, a decimal timestamp duration results, which specifies the number of

© Copyright IBM Corporation 2007, 2012

Time/Timestamp-related scalar functions (1 of 2)

TIME(expression))

HOUR(expression))

MINUTE(expression))

SECOND(expression))

,expression )TIMESTAMP(expression

MICROSECOND(expression))

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-40 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 224: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

years, months, days, hours, minutes, seconds and microseconds between the two timestamps. The data type of the result is DECIMAL(20.6). For the special rules that apply when subtracting times or timestamps, see the SQL Reference.

A time in a table can be subtracted from a literal value representing a valid time or vice versa. The literal need not be the argument of a TIME function, but can be. However, a literal representing a time cannot be subtracted from a literal representing a time. A similar rule applies to timestamps.

Conversion of timestamp (LUW)

Conversion of timestamp to char based on format string. Only supported format 'YYYY-MM-DD HH24:MI:SS' yet.

Example:

SELECT TO_CHAR ( CURRENT TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') FROM SYSIBM.SYSDUMMY1 ;Conversion of string expression into timestamp based on format string. Only supported format 'YYYY-MM-DD HH24:MI:SS' yet.

Example:

SELECT TO_DATE ('2003-09-11 14:53:50', 'YYYY-MM-DD HH24:MI:SS') FROM SYSIBM.SYSDUMMY1 WHERE TO_DATE ('2003-09-11 14:53:50', 'YYYY-MM-DD HH24:MI:SS') < CURRENT TIMESTAMP ;

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-41

Page 225: Ce 1319 Stud

Student Notebook

.

Figure 6-23. Time/Timestamp-related scalar functions (2 of 2) CE1319.4

Notes:

The first column of the result (TIMESTAMP) displays the timestamp created by combining the hiring date of employee KWAN with the time '08:15:35' to indicate that the employee was hired on April 5, 1975, at 08:15:35. When using the TIMESTAMP() function with two arguments, the first argument must represent a date and the second a time. Therefore, no microseconds can be provided and the displayed microsecond value is '000000'.

Columns 2 through 6 (TIME, HOUR, MINUTE, SECOND, and MICRO) extract portions of the timestamp displayed in the first column by using the functions TIME(), HOUR(), MINUTE(), SECOND(), and MICROSECOND(), respectively.

The last column of the result (UNTIL_Y2K) shows the subtraction of two timestamps. The result is a decimal timestamp duration. Its data type is DECIMAL(20,6). Its external format is yyyymmddhhmmss.nnnnnn expressing, from left to right, years, months, days, hours, minutes, seconds, and microseconds (decimal places). Leading zeros are suppressed. Because a decimal timestamp duration includes a decimal date duration, the same problems exist as described for decimal date durations.

© Copyright IBM Corporation 2007, 2012

Time/Timestamp-related scalar functions (2 of 2)

SELECT TIMESTAMP(HIREDATE,'08:15:35') AS TIMESTAMP,TIME(TIMESTAMP(HIREDATE,'08:15:35')) AS TIME,HOUR(TIMESTAMP(HIREDATE,'08:15:35')) AS HOUR,MINUTE(TIMESTAMP(HIREDATE,'08:15:35')) AS MINUTE,SECOND(TIMESTAMP(HIREDATE,'08:15:35')) AS SECOND,MICROSECOND(TIMESTAMP(HIREDATE,'08:15:35')) AS MICRO,

TIMESTAMP('20000101000000')-TIMESTAMP(HIREDATE,'08:15:35')

AS UNTIL_Y2K FROM EMPLOYEEWHERE EMPNO = '000030’ ;

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

TIMESTAMP

1975-04-05-08.15.35.000000TIME

08.15.35HOUR

8MINUTE

15SECOND

35MICRO

0UNTIL_Y2K

240825154425.000000

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-42 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 226: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-24. Labeled durations CE1319.4

Notes:

Datetime values can be added and subtracted. These operations involve decimal numbers called durations. A duration is a number representing an interval of time. • A duration may be expressed as a decimal or as a labeled duration. '2 YEARS' is an example of a labeled duration.

A labeled duration represents a specific unit of time as expressed by an integer followed by one of the keywords YEAR(S), MONTH(S), DAY(S), HOUR(S), MINUTE(S), SECOND(S), or MICROSECOND(S). The number specified is converted as if it were assigned to a DECIMAL(15,0) number.

The sequence of operations is important when adding or subtracting labeled durations. To ensure consistent, predictable results, it is important to follow these rules:

• When adding labeled durations, add years, then months, then days

• When subtracting labeled durations, subtract days, then months, then years

DB2 has some special registers which can be very useful when working with durations. These are CURRENT DATE, CURRENT TIME, CURRENT TIMESTAMP, and

© Copyright IBM Corporation 2007, 2012

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

SELECT HIREDATE + (2 YEARS + 3 MONTHS + 5 DAYS)FROM EMPLOYEEWHERE EMPNO = '000010' ;

1962-08-301988-12-051963-12-05 1998-11-16DATE_2DATE_1HIREDATE TODAY

SELECT HIREDATE,HIREDATE + 24 YEARS + 11 MONTHS + 30 DAYS AS DATE_1,HIREDATE - 5 DAYS - 3 MONTHS - 1 YEAR AS DATE_2,CURRENT DATE AS TODAY FROM EMPLOYEEWHERE EMPNO = '000120’ ;

Labeled durations

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-43

Page 227: Ce 1319 Stud

Student Notebook

.

CURRENT TIMEZONE. On both the LUW and z/OS platforms, these registers may be identified as CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, and CURRENT_TIMEZONE.

The first SELECT statement on the visual obtains four columns of date information for employee O'CONNELL. The first column of the result shows the hiring date of the employee. The second column (DATE_1) is derived by adding three labeled durations to the hiring date. The third column (DATE_2) is derived by subtracting three labeled durations from the hiring date. The fourth column displays the current date which is 1998-11-16 because the query was run on November 16, 1998.

The crossed out SELECT statement on the visual tries to add three labeled durations together, within parentheses, and then add the sum to the hiring date of an employee. A labeled duration can only be added or subtracted from a DATE, TIME, or TIMESTAMP: adding labeled durations together is an error. Therefore, the query fails.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-44 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 228: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 6-25. Checkpoint CE1319.4

Notes:

Enter your answers below:

1.

2.

3.

4.

© Copyright IBM Corporation 2007, 2012

Checkpoint

1. Which function can be used to extract the first three characters of an alphanumeric column?

2. Which function(s) can be used to extract the digits after the decimal point in a column defined as DECIMAL?

3. What is a timestamp?

4. What does the DAYS() function do?

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 6. Scalar Functions 6-45

Page 229: Ce 1319 Stud

Student Notebook

.

Figure 6-26. Unit summary CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2012

Unit summary

Having completed this unit, you should be able to:• Extend your knowledge of scalar functions which:

– Manipulate arithmetic data– Manipulate date values– Manipulate character data

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

6-46 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 230: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 7. Table Expressions and Recursive SQL

What this unit is about

Some business problems that once required complex application programming or the extensive use of views can now be solved with far less effort using DB2 features such as table expressions and recursive SQL.

What you should be able to do

After completing this unit, you should be able to:

• Identify reasons for table expressions and recursive SQL

• Use nested and common table expressions

• Identify the difference between views and table expressions

• Code recursive SQL

• Control the depth of recursion when coding recursive SQL

How you will check your progress

• Checkpoint

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-1

Page 231: Ce 1319 Stud

Student Notebook

.

Figure 7-1. Unit objectives CE1319.4

Notes:

Some business problems that once required complex application programming or the extensive use of views can now be solved with far less effort using DB2 features such as table expressions and recursive SQL.

© Copyright IBM Corporation 2007, 2012

Unit objectives

After completing this unit, you should be able to:• Identify reasons for table expressions and recursive SQL• Use nested and common table expressions• Identify the difference between views and table expressions• Code recursive SQL• Control the depth of recursion when coding recursive SQL

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 232: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-2. Nested Table Expressions CE1319.4

Notes:

Application programmers can use Table Expressions to define and use pseudo-views or inline-views. You do not have to create a normal DB2 view. DB2 views require you to CREATE them and to control who can use them, which can be seen as a security issue.

You do not create Table Expressions so they are not defined in the DB2 Catalog tables, and are therefore temporary. They simply return some intermediate results to the outer query, and do not persist after the query completes.

You may think of nested table expressions as being similar to subqueries, but they occur in the FROM clause rather than the WHERE clause of the outer query.

© Copyright IBM Corporation 2007, 2012

Nested Table Expressions

• Much like a subquery– Not defined in the DB2 catalog

– Temporary in nature

– In the FROM clause, rather than WHERE

– Sometimes called pseudo or inline views

SELECT ….

FROM (SELECT …. FROM

WHERE …. )

WHERE ….

ORDER BY ….

NestedTable

Expression

Outer

Query

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-3

Page 233: Ce 1319 Stud

Student Notebook

.

Figure 7-3. Nested Table Expressions CE1319.4

Notes:

Follow these rules when writing nested table expressions:

1. Enclose the table expression in parentheses.

2. Give a correlation name to the table expression. You can use the correlation name as the qualifier of a column name, just like any other table or view name. However, you cannot refer to the correlation name within the FROM clause that defines it.

3. Provide unique names for the columns of the result table that you reference. If you reference those columns, their names need to be unique.

© Copyright IBM Corporation 2007, 2012

Nested Table ExpressionsRules for writing nested table expressions:

1. Enclose the expression in parentheses

2. Provide a correlation name in the expression

3. Ensure unique names for the columns of the results table

DB2 table on DASD

Intermediate Results

Final Result Set

Outer

Query

NestedTable

Expression

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 234: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-4. Nested table expressions CE1319.4

Notes:

In the example above, everything in the parentheses following the first keyword FROM is a table expression. The name of the table expression is S. The table expression is obtaining data from the EMPLOYEE table. For each employee, it determines the decade in which the employee was hired and the employee's salary. The result columns for the table expression are called HIREDECADE and SALARY.

The column HIREDECADE contains the decade, during which the employee was hired. For example, 1970 - 9 means that the employee was hired between January 1, 1970 and December 31, 1979. The hiring decade is determined by extracting the first three digits from the ISO format of the hiring date and appending the character string '0 - 9'. The column SALARY is the salary for the employee.

For each hiring decade, the outer query determines the minimum salary paid to employees hired during this decade. The outer query obtains its data from the table expression by grouping the rows returned by the table expression by hiring decade and determining the minimum salary for each distinct hiring decade. The appropriate columns of the final result table are called HIREDECADE and MINIMUM_SALARY.

© Copyright IBM Corporation 2007, 2012

HIREDECADE MINIMUM_SALARY

1960 - 9 29250.0023800.001970 - 9

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

SELECT S.HIREDECADE, MIN(S.SALARY) AS MINIMUM_SALARYFROM(

) AS SGROUP BY S.HIREDECADE ;

SELECT SUBSTR(CHAR(HIREDATE,ISO),1,3)CONCAT '0 - 9' AS HIREDECADE,

SALARYFROM EMPLOYEE

Nested table expressions

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-5

Page 235: Ce 1319 Stud

Student Notebook

.

On DB2 for Linux, UNIX, and Windows, and in DB2 9 for z/OS, the query in the example here can be solved using an expression in GROUP BY, thus:

SELECT SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0-9' , MIN(SALARY) FROM EMPLOYEE GROUP BY SUBSTR(CHAR(HIREDATE,ISO),1,3) ;Nested table expressions are available on DB2 for Linux, UNIX, and Windows and DB2 for z/OS. • Common table expressions are available on DB2 for Linux, UNIX, and Windows, and on DB2 for z/OS beginning with version 8.

Note

The AS clause is used for two different purposes in this example in order to create temporary names that can be referenced elsewhere in the query: It is used to name the derived column HIREDECADE of the table expression and also to name the table expression itself. The columns of the table expression are referenced by the outer query. The outer query uses the AS clause as well to name the calculated column containing the minimum salary for the hiring decade.

Pull up ordering from nested query using ORDER BY ORDER OF:

SELECT * FROM (SELECT WORKDEPT, SUM(SALARY) FROM EMPLOYEE GROUP BY WORKDEPT ORDER BY 1 DESC) as T(DEPTNO,SUMSAL)ORDER BY ORDER OF T

Restrictions: Cannot use ORDER OF based on unordered derived table:

• Subquery without ORDER BY

• Table, view

• Common table expressions

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 236: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-5. Nested table expressions in joins (1 of 4) CE1319.4

Notes:

An operand of a join can be more complex than the name of a single table. You can use a fullselect in parentheses followed by a correlation name. This is called a nested table expression.

In the example on the visual, we want to list, for each employee in the EMPLOYEE table, his/her employee number, last name, hiring decade, salary, and the minimum salary being paid to any employee hired during the same decade. This is illustrated by the result table on the visual.

The result cannot be obtained by only using the EMPLOYEE table. It can be created by means of an inner join of two table expressions. As indicated on the visual, both table expressions must have a column HIREDECADE, and the inner join is performed by using the columns in the ON condition.

One of the table expressions (on the visual, the first one) is called E and must provide employee number, last name, salary, and hiring decade for all employees. The other table expression, called M, must provide the minimum salary for each hiring decade. Joining the

© Copyright IBM Corporation 2007, 2012

EMPLOYEE

EMPNO LASTNAME WORKDEPT SALARY HIREDATE000010

000030

000120

000130

000140

HAAS

KWAN

O'CONNELL

QUINTANA

NICHOLLS

A00

C01

A00

C01

C01

52750.00

38250.00

29250.00

23800.00

28420.00

1965-01-01

1975-04-05

1963-12-05

1971-07-28

1976-12-15

EMPNO LASTNAME HIREDECADE SALARY MINIMUM_SALARY000010

000030000120

000130

000140

HAAS

KWANO'CONNELL

QUINTANA

NICHOLLS

52750.00

38250.00

29250.00

23800.00

28420.00

1960 - 9

1970 - 91960 - 9

1970 - 9

1970 - 9 23800.00

29250.00

23800.00

29250.00

23800.00

SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE, E.SALARY, M.MINIMUM_SALARYFROM

( ) AS E

INNER JOIN (

) AS MON E.HIREDECADE = M.HIREDECADE ;

subselect

subselect

Nested table expressions in joins (1 of 4)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-7

Page 237: Ce 1319 Stud

Student Notebook

.

two table expressions by means of their HIREDECADE columns yields the desired result. The subsequent visual illustrates the result sets of the two table expressions.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 238: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-6. Nested table expressions in joins (2 of 4) CE1319.4

Notes:

This visual illustrates the temporary result tables to be created by the subselects of the table expressions:

• The first table expression, called E, should provide, for each employee, his/her employee number, last name, salary, and hiring decade (column HIREDECADE).

• The second table expression, called M, should supply, for each decade, the minimum salary being paid to employees hired during this decade. This is precisely the same result table as for the example on page.

• By joining the two tables, we will get the desired result.

© Copyright IBM Corporation 2007, 2012

SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE, E.SALARY, M.MINIMUM_SALARYFROM

(

) AS EINNER JOIN

(

) AS MON E.HIREDECADE = M.HIREDECADE ;

MINIMUM_SALARYHIREDECADE

29250.00

23800.00

1960 - 9

1970 - 9subselect

EMPNO LASTNAME SALARY HIREDECADE000010

000030

000120

000130

000140

HAAS

KWAN

O'CONNELL

QUINTANA

NICHOLLS

52750.00

38250.00

29250.00

23800.00

28420.00

1960 - 9

1970 - 9

1960 - 9

1970 - 9

1970 - 9

subselect

Nested table expressions in joins (2 of 4)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-9

Page 239: Ce 1319 Stud

Student Notebook

.

Figure 7-7. Nested table expressions in joins (3 of 4) CE1319.4

Notes:

The visual shows the SELECT statement for retrieving employee number, last name, salary, and hiring decade for all employees.

The hiring decade is determined in the same way as for the first example of the unit to ensure that the HIREDATE columns of the two table expressions can be used for the inner join operation of the outer query. The hiring decade is derived by appending the character string '0 - 9' to the first three digits of the year when the appropriate employee was hired.

An AS clause is used to give the derived column the name HIREDECADE.

© Copyright IBM Corporation 2007, 2012

EMPLOYEE(Base Table)

EMPNO LASTNAME WORKDEPT SALARY HIREDATE

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

A00C01A00C01C01

52750.0038250.0029250.0023800.0028420.00

1965-01-011975-04-051963-12-051971-07-281976-12-15

EMPNO LASTNAME HIREDECADESALARY

000010000030000120000130000140

HAASKWANO'CONNELLQUINTANANICHOLLS

1960 - 91970 - 91960 - 91970 - 91970 - 9

52750.0038250.0029250.0023800.0028420.00

SELECT EMPNO, LASTNAME, SALARY,SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0 - 9'AS HIREDECADE

FROM EMPLOYEE

Nested table expressions in joins (3 of 4)

Nested TableExpression

IntermediateResults

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 240: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-8. Nested table expressions in joins (4 of 4) CE1319.4

Notes:

Now, we can complete the original problem, that is, determine, for each employee, his/her employee number, last name, hiring decade, salary, and the minimum salary being paid to employees of his/her hiring decade.

To complete the query, we must replace the boxes labeled subselect in the preceding visuals by the SQL statements for the table expressions. The first box must be replaced by the SELECT statement for table expression E, shown on the previous visual. The second box must be replaced by the SQL statement for table expression M. For each hiring decade, this SQL statement determined the minimum salary paid to people hired during the respective decade.

© Copyright IBM Corporation 2007, 2012

SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE, E.SALARY, M.MINIMUM_SALARYFROM

(

) AS EINNER JOIN (

) AS MON E.HIREDECADE = M.HIREDECADE ;

SELECT EMPNO, LASTNAME,SALARY,SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0 - 9'AS HIREDECADE

FROM EMPLOYEE

SELECT S.HIREDECADE, MIN(S.SALARY) AS MINIMUM_SALARYFROM( SELECT SUBSTR(CHAR(HIREDATE,ISO),1,3)

CONCAT '0 - 9' AS HIREDECADE,SALARYFROM EMPLOYEE

) AS SGROUP BY S.HIREDECADE

Nested table expressions in joins (4 of 4)

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-11

Page 241: Ce 1319 Stud

Student Notebook

.

Figure 7-9. Common table expressions CE1319.4

Notes:

The table expressions we have seen so far created temporary result tables within the FROM clause of an outer query. These table expressions could not be referenced elsewhere in the query, although their result columns could.

Common table expressions can be referenced elsewhere in the query, even by other common table expressions within the same query. They are introduced by the keyword WITH and occur at the beginning of the query (before the SELECT), not within the FROM of the outer query. They are separated from each other by commas. Every reference to a specific common table expression within the same query uses the same result set.

The example on the visual reformulates the query that we used in the previous example, that is, the example for nested table expressions. It uses common table expressions which are named E and M as the nested table expressions were before.

The first common table expression is the SQL statement for table expression E which determines employee number, last name, salary, and hiring decade for all employees of the EMPLOYEE table. The common table expression is again called E. The columns of the associated result table are those named in the SELECT statement.

© Copyright IBM Corporation 2007, 2012

SELECT EMPNO, LASTNAME, SALARY,SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0 - 9'AS HIREDECADE

FROM EMPLOYEE

SELECT HIREDECADE, MIN(SALARY)FROM GROUP BY HIREDECADE

E

WITHE AS(

),M (HIREDECADE, MINIMUM_SALARY) AS(

)SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE,

E.SALARY, M.MINIMUM_SALARYFROM E INNER JOIN MON E.HIREDECADE = M.HIREDECADE

Common table expressions

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-12 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 242: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Although the second table expression looks different, it provides the same result as the SQL statement for table expression M of the previous query: for the various decades, it determines the minimum salary being paid to the employees hired during the appropriate decade. The basic differences are:

• The SELECT statement now uses common table expression E, defined in front of common table expression M, instead of table EMPLOYEE.

• The columns of the common table expression are named by specifying their names in parentheses following the name of the common table expression. This is the same technique as naming the columns of a view. No AS clause is needed for the calculated column in the SELECT statement.

As a main SELECT, the former outer query follows the common table expressions. Since it now can refer to the common table expressions, it becomes easier. It is not possible to take temporary views or common table expressions before Version 8 in z/OS.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-13

Page 243: Ce 1319 Stud

Student Notebook

.

Figure 7-10. Recursive SQL CE1319.4

Notes:

Here is a typical situation where the business requirement lends itself to the utilization of recursive SQL: Given the PARTLIST on the next page, what is the total number of each component and sub-component needed to make part 01?

Recursive SQL is used to work on tables that contain component breakdowns where each component is broken down into subcomponents and each subcomponent is broken down again into sub-subcomponents, etc. Problems involving these kinds of tables are often called “Bill of Materials” problems. A table that represented the parts in a computer would be an example of a Bill of Materials: the major components, the monitor, system unit, and printer, all contain subassemblies like the hard drive, the mother board, and the print head, each of which is composed of other subassemblies, and so on.

Recursive SQL involves defining a common table expression that references itself. The common table expression consists of two distinct components, an initialization select and an iterative select.

© Copyright IBM Corporation 2007, 2012

WITH RPL (PART, SUBPART, QUANTITY) AS(

SELECT ROOT.PART, ROOT.SUBPART, ROOT.QUANTITYFROM PARTLIST ROOT

WHERE ROOT.PART = '01'

SELECT CHILD.PART, CHILD.SUBPART, PARENT.QUANTITY*CHILD.QUANTITYFROM RPL PARENT, PARTLIST CHILD

WHERE PARENT.SUBPART = CHILD.PART)

SELECT PART, SUBPART, SUM(QUANTITY) AS QUANTITY FROM RPL

GROUP BY PART, SUBPARTORDER BY PART, SUBPART

UNION ALL

Initialization Select

Iterative Select

Main Select

Recursive SQL

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-14 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 244: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• The initialization select is the first SELECT in the table expression and the iterative select is the second SELECT in the table expression. The initialization SELECT is executed only once.

• The iterative select is combined with the initialization select by means of UNION ALL. The recursive common table expression in the example is named RPL. The iterative SELECT may be performed multiple times. The common table expression in a recursive SQL statement is followed by a main select.

• The main select identifies the columns which are obtained from the result set of the common table expression. The main SELECT builds a final result set that identifies all the parts and subparts needed to build Part 01 (WHERE clause of initialization select) in a parts table called PARTLIST. We will see the PARTLIST table when stepping through the various “phases” by means of the subsequent visuals.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-15

Page 245: Ce 1319 Stud

Student Notebook

.

Figure 7-11. Recursive SQL - Initialization SELECT CE1319.4

Notes:

The initialization select is executed only once. In the example, it reads the PARTLIST table. The WHERE clause of the initialization select controls the starting point of the recursion. In the example, the starting point is all rows with a part number of '01'.

The right-hand side of the visual displays the four rows placed in the temporary table RPL as the consequence of the initialization select. Parts 02, 03, 04, and 06 are the assemblies that directly make up Part 01. The first column (PART) of the interim result identifies the major part. The second column (SUBPART) identifies the subparts that make up the major part. The third column (QUANTITY) identifies the quantity of the subpart needed to construct one complete major part. For example, it takes three units of Part 06 to construct Part 01.

© Copyright IBM Corporation 2007, 2012

SELECT ROOT.PART, ROOT.SUBPART, ROOT.QUANTITYFROM PARTLIST ROOTWHERE ROOT.PART = '01'

PARTLIST Table

PART SUBPART QUANTITY

00 01 500 05 301 02 201 03 301 04 401 06 302 05 702 06 603 07 604 08 1004 09 1105 10 1005 11 1006 12 1006 13 1007 12 807 14 8

PART SUBPART QUANTITY

01 02 201 03 301 04 401 06 3

RPL

Recursive SQL – Initialization SELECT

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-16 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 246: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-12. Recursive SQL - First iteration CE1319.4

Notes:

Unless it is limited by control variables, the iterative select is executed until all subparts of all parts have been broken down into their subparts, no matter how many repetitions are required. In our example, there are no control variables so the iteration will continue until all parts are completely resolved.

The iterative select in the example was the part of the recursive SQL statement between the UNION ALL and the parenthesis that closed the common table expression named RPL. Only the iterative select is repeated on this visual.

During the first iteration, each row from the initialization select is joined to all rows in the PARTLIST table that meet the join criteria. The result rows are added to the temporary table RPL. The rows that are added to RPL indicate that Parts 05 through 09 and 12 through 13 make up the parts returned by the initialization SELECT:

Part 02 consists of Parts 05 and 06; Part 03 consists of Part 07; Part 04 consists of Parts 08 and 09; and Part 06 consists of Parts 12 and 13.

© Copyright IBM Corporation 2007, 2012

SELECT CHILD.PART, CHILD.SUBPART, PARENT.QUANTITY*CHILD.QUANTITYFROM RPL PARENT, PARTLIST CHILDWHERE PARENT.SUBPART = CHILD.PART

PARTLIST Table

00 01 500 05 301 02 201 03 301 04 401 06 302 05 702 06 603 07 604 08 1004 09 1105 10 1005 11 1006 12 1006 13 1007 12 807 14 8

PART SUBPART QUANTITY

PART SUBPART QUANTITY

01 02 201 03 301 04 401 06 302 05 1402 06 1203 07 1804 08 4004 09 4406 12 3006 13 30

RPL

Recursive SQL – First iteration

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-17

Page 247: Ce 1319 Stud

Student Notebook

.

Note

It is very easy to write a recursive SQL statement incorrectly and initiate an infinite loop. Control variables are very useful for limiting the number of iterations and are discussed later in this unit.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-18 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 248: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-13. Recursive SQL - Second iteration CE1319.4

Notes:

The second iteration joins the rows added by the first iteration to the PARTLIST table. The result rows of the second iteration are again added to RPL. The second iteration indicates that Part 05 consists of Parts 10 and 11, Part 06 consists of Parts 12 and 13, and that part 07 consists of parts 12 and 14.

Since there are no correspondences for the subparts of Parts 04 and 06, added by the first iteration, in the PARTLIST table, parts are not added for them to the RPL temporary table.

The iterative SELECT repeatedly executes until there is no further breakdown of components in the data structure, or halted by a user-controlled level of iterations. We will see how that is done in a couple more slides.

© Copyright IBM Corporation 2007, 2012

140140120120144144

RPL

SELECT CHILD.PART, CHILD.SUBPART, PARENT.QUANTITY*CHILD.QUANTITYFROM RPL PARENT, PARTLIST CHILDWHERE PARENT.SUBPART = CHILD.PART

PARTLIST Table

PART SUBPART QUANTITY

00 01 500 05 301 02 201 03 301 04 401 06 302 05 702 06 603 07 604 08 1004 09 1105 10 1005 11 1006 12 1006 13 1007 12 807 14 8

PART SUBPART QUANTITY

01 02 201 03 301 04 401 06 302 1402 06 1203 07 1804 08 4004 09 4406 12 3006 30050506060707

101112131214

No correspondencein PARTLIST table

13

05

Recursive SQL – Second iteration

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-19

Page 249: Ce 1319 Stud

Student Notebook

.

Note

RPL now contains two occurrences each of the rows that define the subparts of Part 06, namely, Parts 12 and 13. The first occurrence of these rows was contributed by the first iteration and the second occurrence of these rows came from the second iteration. The UNION ALL preceding the iterative select prevents the duplicate removal.

The recursion will not yield additional rows after the second iteration because there are no further subparts for the parts added by the second iteration. However, if the PARTLIST table contained additional levels of subparts, the recursion would continue since the current example does not limit the depth of the recursion.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-20 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 250: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-14. Recursive SQL - Main SELECT CE1319.4

Notes:

After the recursive common table expression has been evaluated completely, the main SELECT is evaluated. The main SELECT references the result of RPL, the common table expression.

The main SELECT summarizes the total quantity of all parts needed to build Part 01. The grouping and the SUM() function ensure that the quantities of the respective subparts of Part 06 are added together. In other words, the two rows for Part 06, Subpart 12, will be combined to make a single row. So will the two rows for Part 06, Subpart 13. A user who wishes to verify that the warehouse contains enough of each of the components needed to make Part 01 can execute this query and, then, check existing stocks against the result of the query.

© Copyright IBM Corporation 2007, 2012

SELECT PART, SUBPART, SUM(QUANTITY) AS QUANTITY FROM RPL GROUP BY PART, SUBPARTORDER BY PART, SUBPART

RPL

PART SUBPART QUANTITY

01 02 201 03 301 04 401 06 302 05 1402 06 1203 07 1804 08 4004 09 4406 12 3006 13 3005 10 14005 11 14006 12 12006 13 12007 12 14407 14 144

Final Result Table

PART SUBPART QUANTITY

01 04 401 06 302 05 1402 06 1203 07 1804 08 4004 09 4405 10 14005 11 14006 12 15006 13 15007 12 14407 14 144

Recursive SQL – Main SELECT

01 02 201 03 3

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-21

Page 251: Ce 1319 Stud

Student Notebook

.

Figure 7-15. Controlling depth of recursion (1 of 2) CE1319.4

Notes:

Recursion normally continues until all parts have been resolved into their components. However, the depth of the recursion can be controlled by simulating the addition of a control column to the table.

This control column is initialized arbitrarily to 0 in the initialization select and increased by one on every execution of the iterative select. A condition in the WHERE clause of the iterative SELECT is used to ensure that the iteration only continues for a fixed number of levels. This is also very important in enabling you to avoid running a piece of recursive SQL that results in an infinite loop.

In the example, the control column is named LEVEL. It is set to an integer value of 0 in the initialization SELECT. The WHERE clause of the initialization SELECT determines the value in the PART column with which the table expression begins. This time, we are interested in the breakdown of Part 01, but we could have started with any part number we were interested in. The initial value of LEVEL would still be 0, regardless of the starting part number.

The iterative SELECT increments the LEVEL value by adding 1 on each iteration.

© Copyright IBM Corporation 2007, 2012

WITH RPL (LEVEL, PART, SUBPART, QUANTITY) AS

(

SELECT 0, ROOT.PART, ROOT.SUBPART, ROOT.QUANTITYFROM PARTLIST ROOTWHERE ROOT.PART = '01'

SELECT PARENT.LEVEL+1, CHILD.PART, CHILD.SUBPART,PARENT.QUANTITY*CHILD.QUANTITY

FROM RPL PARENT, PARTLIST CHILDWHERE PARENT.SUBPART = CHILD.PART

AND PARENT.LEVEL < 2)

SELECT LEVEL, PART, SUBPART, SUM(QUANTITY) AS QUANTITYFROM RPLGROUP BY LEVEL, PART, SUBPART ORDER BY LEVEL, PART, SUBPART

UNION ALL

Initialization Select

Iterative Select

Main Select

Controlling depth of recursion (1 of 2)

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-22 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 252: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

The condition PARENT.LEVEL < 2 in the WHERE clause of the iterative select is used to limit the number of iterations: simply set the constant to the number of iterations which are desired.

The main SELECT displays the result of the table expression. The LEVEL column in the final result makes the origin of each result row clear: rows that came from the initialization SELECT have a level of 0, rows from the first iteration have a level of 1, rows from the second iteration have a level of 2, and so on. The ORDER BY puts the result in a convenient sequence. The actual result of the recursive SQL statement is illustrated on the next visual.

Note

LEVEL is not a column of table PARTLIST, and LEVEL is not a reserved word in SQL. LEVEL does not have to be added to table PARTLIST via an ALTER TABLE statement. It is a virtual column created by the SQL statement.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-23

Page 253: Ce 1319 Stud

Student Notebook

.

Figure 7-16. Controlling depth of recursion (2 of 2) CE1319.4

Notes:

This visual displays the result of the breakdown for Part 00 if the recursion is limited to two iterations. Subparts 05 and 06 of Part 02 and Subpart 07 of Part 03 could be further decomposed if the number of iterations was not limited to two.

© Copyright IBM Corporation 2007, 2012

PARTLIST Table

PART SUBPART QUANTITY

00 01 500 05 301 02 201 03 301 04 401 06 302 05 702 06 603 07 604 08 1004 09 1105 10 1005 11 1006 12 1006 13 1007 12 807 14 8

Final Result Table

Controlling depth of recursion (2 of 2)

0 01 02 20 01 03 3

0 01 06 31 02 05 141 02 06 121 03 07 181 04 08 401 04 09 441 06 12 301 06 13 302 05 10 1402 05 11 1402 06 12 1202 06 13 1202 07 12 144

PART SUBPART QUANTITYLEVEL

0 01 04 4

2 07 14 144

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-24 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 254: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-17. Recursive SQL - Recommendations CE1319.4

Notes:

Recursive SQL is cyclical by definition. This means that it is easy to cause loops if the SQL is coded incorrectly or if the data itself is cyclical.

• For example, if the join in the iterative select in the earlier examples was coded as: PARENT.SUBPART = CHILD.SUBPART there would be an infinite recursion if there was even one row where the Part and Subpart values were the same.

• By the same token, a loop can occur if the data was illogical. For example, if the PARTLIST table had a row where the Part was 05 and the Subpart was 01, a loop would occur.

To prevent this sort of problem, desk check all recursive SQL. Also, test it against small tables before implementing it in production.

Any recursive SQL statement that does not use a control variable will receive an SQL warning (SQL0347W) about the possibility of an infinite loop. This should not normally occur in 'Bill of Material' type examples such as those here, but it is a potential problem in

© Copyright IBM Corporation 2007, 2012

Desk-check recursive SQL statement

Test recursive SQL against small test tables

Use controls to limit recursion

Recursive SQL – Recommendations

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-25

Page 255: Ce 1319 Stud

Student Notebook

.

some other types of recursive SQL (such as air routes) so control variables are very valuable in these situations, both in test and in production.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-26 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 256: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 7-18. Checkpoint CE1319.4

Notes:

Enter your answers below:

1.

2.

3.

4.

© Copyright IBM Corporation 2007, 2012

Checkpoint

1. T/F. Table expressions have many similarities to views.

2. List three ways in which table expressions can be more convenient than views.

3. T/F. If your data is invalid and you code a recursive SQL statement, it is possible to start an infinite loop.

4. What key element in the syntax of an SQL statement makes it recursive?

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 7. Table Expressions and Recursive SQL 7-27

Page 257: Ce 1319 Stud

Student Notebook

.

Figure 7-19. Unit summary CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2012

Unit summary

Having completed this unit, you should be able to:• Identify reasons for table expressions and recursive SQL• Use nested and common table expressions• Identify the difference between views and table expressions• Code recursive SQL• Control the depth of recursion when coding recursive SQL

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

7-28 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 258: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 8. An Introduction to UDTs/UDFs and Stored Procedures

What this unit is about

DB2 User-Defined Distinct Data Types, User-Defined Functions and Stored Procedures can address business requirements that go beyond the functionality of built-in DB2 data types and functions.

In this unit we will be presenting the high-level concepts of UDT’s, UDF’s and SP’s.

What you should be able to do

After completing this unit, you should be able to:

• Describe the concepts behind User-Defined Distinct Types, User-Defined Functions and Stored Procedures

How you will check your progress

• Checkpoint

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 8. An Introduction to UDTs/UDFs and Stored Procedures 8-1

Page 259: Ce 1319 Stud

Student Notebook

.

Figure 8-1. Unit objectives CE1319.4

Notes:

If there are business requirements that go beyond the functionality of built-in DB2 data types and functions, they can be addressed using DB2 features such as User-Defined Distinct Data Types, User-Defined Functions and Stored Procedures.

© Copyright IBM Corporation 2007, 2012

Unit objectives

After completing this unit, you should be able to:• Describe the concepts behind User-Defined Distinct Types,

User-Defined Functions and Stored Procedures

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

8-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 260: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 8-2. User-defined Distinct Types - The need CE1319.4

Notes:

DB2 provides many “built-in” or standard data types, such as DECIMAL, CHAR, DATE, BLOB, etc. But these standard data types are not always adequate for every business need. A company sometimes requires data types that reflect a very specific use of data. Therefore DB2 allows users to create their own data types. These are called User-Defined Distinct Types, or UDTs for short.

UDTs allow DB2 itself, rather than applications, to ensure that only valid comparisons are made amongst the data types. This is especially useful for cases where users are writing ad-hoc queries and may not realize that one column is not logically comparable to another.

The diagram represents a situation where the statement “SPEED LIMIT 100” is ambiguous until the unit of measure is applied. 100 miles per hour is NOT the same as 100 kilometers per hour. The American motorist who has just crossed the border into Canada has seen a sign that says “SPEED LIMIT 100" and assumes that this means 100 miles per hour. The Canadian police officer pursuing him will ensure that his mistaken impression is soon corrected.

© Copyright IBM Corporation 2007, 2012

Need to establish context for valuesDB2 enforced typing

CANADA USA

SPEEDLIMIT

65

SPEEDLIMIT100

But... I wasonly doing ahundred!

User-defined Distinct Types – The need

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 8. An Introduction to UDTs/UDFs and Stored Procedures 8-3

Page 261: Ce 1319 Stud

Student Notebook

.

Figure 8-3. User-defined Distinct Types - Definition CE1319.4

Notes:

The CREATE DISTINCT TYPE statement allows users to create data types that can be enforced by the database manager. The name of the new UDT can be a qualified or an unqualified name. If the qualifier is different from the authorization ID of the statement, the authorization ID must have DBADM authority on the database.

UDTs are always based on existing built-in data types like CHAR and SMALLINT. UDTs cannot be used as source types for newer UDTs.

The WITH COMPARISONS clause tells DB2 to generate =, <>, <, <=, >, and >= operators for comparing different instances of the UDT. This clause is required if the source data type supports these comparisons and is prohibited if the source data type does not support comparisons. The source data types which do not support comparisons are BLOB, CLOB, DBCLOB, LONG VARCHAR, and LONG VARGRAPHIC.

DB2 always generates cast functions to:

• CAST from the UDT to the source type, using the standard name of the source type.

© Copyright IBM Corporation 2007, 2012

MPH

CREATE DISTINCT TYPE AS INTEGERWITH COMPARISONS

CREATE DISTINCT TYPE AS INTEGERWITH COMPARISONS

CREATE TABLE SPEED_LIMITS( ROUTE_NUM SMALLINT NOT NULL,CANADA_SL NOT NULL,US_SL NOT NULL )

KPHMPH

KPH

SELECT ROUTE_NUMFROM SPEED_LIMITSWHERE CANADA_SL > KPH(80)

SELECT ROUTE_NUM FROM SPEED_LIMITSWHERE CANADA_SL > US_SL

FAILS

User-defined Distinct Types – Definition

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

8-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 262: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

• CAST from the source type to the UDT. These functions are important for the manipulation of the UDTs in queries.

The first example in the upper box creates a UDT called KPH, which is based on the INTEGER built-in data type. The second example in the upper box creates a UDT called MPH, which is also based on the INTEGER data type. The third example in the upper box creates a table named SPEED_LIMITS that contains three columns. The first column, ROUTE_NUM, contains a highway number like 401 or I95. The second column, CANADA_SL, is defined with the new data type KPH. The third column, US_SL, is defined with the new data type MPH. Presumably, each highway in the table, whether Canadian or American, contains the speed limit expressed in both kilometers per hour and miles per hour.

The first example in the lower box asks for all the highways in the SPEED_LIMITS table that have a CANADA_SL value that is greater than 80. However, the query cannot simply say: 'WHERE CANADA_SL > 80'. The number 80 is an integer, but CANADA_SL is not an integer, it is a KPH. Therefore, the 80 has to be cast to a KPH value via the 'KPH(80)' expression. This CAST function was created automatically by DB2 when the KPH UDT was created.

The second example in the lower box attempts to get all the rows of the SPEED_LIMITS table where the CANADA_SL value is greater than the US_SL value. KPH and MPH values cannot be compared directly so the statement fails. The WHERE clause in this query would need to be changed to: WHERE INTEGER(CANADA_SL) > INTEGER(US_SL) In other words, both columns would need to be explicitly cast back into their source types in order to compare them.

Note

The CAST functions cannot be used to convert a speed limit that is in KPH to its equivalent speed in miles per hour. The CAST functions will not, for example, convert 80 kilometers per hour to 50 miles per hour.

Notexclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 8. An Introduction to UDTs/UDFs and Stored Procedures 8-5

Page 263: Ce 1319 Stud

Student Notebook

.

Figure 8-4. User-Defined Functions - The need CE1319.4

Notes:

Although many functions are already built into DB2 and more may emerge over time, many users want more functions. DB2 supports User-Defined Functions, UDFs for short, as a way for users to develop their own functions and have DB2 manage them.

A UDF can perform many tasks. For example, UDFs can be written to:

• Convert between different measuring systems, like the metric and US systems

• Do complex calculations like multivariate analyses

• Act as methods for UDTs by providing behavior and encapsulation for the distinct types

At CREATE time UDFs are stored in the DB2 catalog. This ensures that they always give consistent answers. UDFs can return a single value or a whole table. UDFs can be based on other UDFs that already exist.

UDFs can be written in many languages such as Assembler, COBOL, PL/I, C, Java, and OLE. and are normally developed by programmers. The IBM DB2 programming classes describe the development process fully and let students develop a UDF.

© Copyright IBM Corporation 2007, 2012

SCALARFUNCTIONSREQUESTED

BYDB2 CUSTOMERS

UDTs?

cubicyards

etc.

mph

rating

horsepower

watts

cups

feet

etc.etc.

User-Defined Functions – The need

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

8-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 264: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 8-5. User-Defined Functions - Sourced CE1319.4

Notes:

The CREATE FUNCTION statement is used to define UDFs.

Strong typing defines that data types can only be used as defined by the installation. Therefore, a UDT cannot be used in conjunction with functions provided by the database manager unless usage is defined. To assist the installation in defining common functions, DB2 allows functions to be sourced on previously defined functions. These previously defined functions could be provided by the database manager or user-written.

It might be advantageous NOT to define a function sourced on every function provided by the database manager for certain UDFs. For example, assume the employee number is defined as a distinct type based on the INTEGER type. The installation would probably not define the functions SUM, AVG, +, -, *, and / on this data type since such manipulations would be invalid.

A function name can also be overloaded, so that the same name can be used several times. For example, consider the function AVG_US_SPEED defined in the visual. This function could have been defined as AVG, even though such a function name already

© Copyright IBM Corporation 2007, 2012

CREATE FUNCTION "+" (MPH,MPH)RETURNS MPHSOURCE "+" (INTEGER,INTEGER)

CREATE FUNCTION AVG_US_SPEED (MPH)RETURNS MPHSOURCE AVG(INTEGER)

Existing Built-in Functions(or UDFs)SUM + - / * AVG MIN, etc.

User-Defined Functions – Sourced

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 8. An Introduction to UDTs/UDFs and Stored Procedures 8-7

Page 265: Ce 1319 Stud

Student Notebook

.

exists. The database manager will invoke the function that matches the input parameters passed in an overloading situation.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

8-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 266: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 8-6. User-Defined Functions - External CE1319.4

Notes:

A UDF can accept one or more input parameters, but a single value must be returned. Furthermore, the UDF code cannot contain SQL.

The UDF is coded in a programming language such as C. This enables the developer to take advantage of any and all functions available in the language. The developer can also take advantage of library management of such functions as well. For example, the functions defined in the visual are conversion functions. They reside in a library called convert.

A UDF can be invoked anywhere in an SQL statement where an expression is currently permitted. For an example of usage of the UDF in the visual, consider the following query: SELECT ROUTE_NUM FROM SPEED_LIMITS WHERE CANADA_SL > MPH_TO_KPH(US_SL)

Normally, programmers will develop UDFs. The IBM programming classes describe the development process fully and let students develop a UDF.

© Copyright IBM Corporation 2007, 2012

CREATE FUNCTION KPH_TO_MPH(KPH)RETURNS MPHSPECIFIC kph_convEXTERNAL NAME 'convert!kph2mph'LANGUAGE CPARAMETER STYLE DB2SQLDETERMINISTICNO EXTERNAL ACTIONFENCEDNOT NULL CALLNO SQLNO SCRATCHPADNO FINAL CALL

CREATE FUNCTIONMPH_TO_KPH(MPH)RETURNS KPH ....

User-Defined Functions – External

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 8. An Introduction to UDTs/UDFs and Stored Procedures 8-9

Page 267: Ce 1319 Stud

Student Notebook

.

Figure 8-7. User-Defined SQL Functions CE1319.4

Notes:

In the more recent versions of DB2, there is now another option available. Rather than use a programming environment and a language like C to code a simple requirement, we can code the function in the CREATE statement, using SQL and the extensions of SQL/PL.

SQL/PL (Structured Query Language/Procedural Language) is a superset of SQL. SQL/PL offers the programmer the full functionality of SQL, plus additional features typical of a procedural language, such as comparisons, branching, and error handling.

In the example above you can see how converting KPH to MPH and vice versa can be written easily using SQL. These simple functions only need one SQL statement in the RETURN clause. If more logic is needed, DB2 SQL/PL can be used to provide the other needed programming constructs.

Beginning with DB2 for Linux, UNIX and Windows Version 8.2 and DB2 for z/OS Version 9, they are considered “native” SQL functions and are run as a script in the usual way. We will see examples in the coming pages with stored procedures, which can also be coded with SQL/PL.

© Copyright IBM Corporation 2007, 2012

User-Defined SQL Functions

CREATE FUNCTION KPH_TO_MPH (KPH SMALLINT)

RETURNS SMALLINT

LANGUAGE SQL

RETURN KPH*1.60934

CREATE FUNCTION MPH_TO_KPH (MPH SMALLINT)

RETURNS SMALLINT

LANGUAGE SQL

RETURN MPH*0.62137

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

8-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 268: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 8-8. Stored Procedures - Example 1 CE1319.4

Notes:

A stored procedure is effectively an external program which can be called using input and output parameters. Here, you would call the procedures in the following way:

CALL KPH_TO_MPH(CANADA_SL,?)

CALL MPH_TO_KPH(US_SL,?)

The first parameter is the speed limit to be converted. The second parameter,? represents the output parameter (the output parameter has no input value when calling the procedure, but you have to recognize its existence).

© Copyright IBM Corporation 2007, 2012

Stored Procedures – Example 1CREATE PROCEDURE KPH_TO_MPH (IN KPH SMALLINT, OUT MPHSMALLINT)

LANGUAGE SQL

P1: BEGIN

SET MPH = KPH * 1.60934;

END P1@

CREATE PROCEDURE MPH_TO_KPH (IN MPH SMALLINT, OUT KPHSMALLINT)

LANGUAGE SQL

P1: BEGIN

SET KPH = MPH * 0.62137;

END P1@

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 8. An Introduction to UDTs/UDFs and Stored Procedures 8-11

Page 269: Ce 1319 Stud

Student Notebook

.

Figure 8-9. Stored Procedures - Example 2 CE1319.4

Notes:

This example is meant to show a little more of the capabilities of SQL/PL. It is designed to select the median (the one after the middle) salary from a list of salaries.

A brief explanation of the code:

• Two variables are declared. • A cursor C1 is declared. This will enable the program to read down the EMPLOYEE

table rows, one by one. • If no rows are found in EMPLOYEE, the program will return 6666. • We count how many rows are in the EMPLOYEE table. • We open the cursor to prepare to read the rows. • The WHILE will then read the rows until it reaches the row after the number of rows

divided by 2 (this is the median). The counter enables us to leave the repetitive WHILE statement when we have found the median row.

• It then FETCHes the value of this row into a variable. • We then finish everything, and the variable is the output parameter.

© Copyright IBM Corporation 2007, 2012

Stored Procedures – Example 2CREATE PROCEDURE MEDIAN (OUT medianSalary DEC(7,2))LANGUAGE SQL

P1: BEGINDECLARE v_numRecords INT DEFAULT 1;DECLARE v_counter INT DEFAULT 0;

DECLARE c1 CURSOR FORSELECT salary FROM EMPLOYEEORDER BY salary;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET medianSalary = 6666;

SET medianSalary = 0;

SELECT COUNT(*) INTO v_numRecords FROM EMPLOYEE;OPEN c1; WHILE v_counter < (v_numRecords / 2 + 1) DO FETCH c1 INTO medianSalary; SET v_counter = v_counter + 1; END WHILE; CLOSE c1;

END P1@

ProcedureBody

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

8-12 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 270: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 8-10. Checkpoint CE1319.4

Notes:

Write your answers below:

1.

2.

3.

© Copyright IBM Corporation 2007, 2012

Checkpoint

1. T/F. User-Defined Distinct Data Types can be compared without restrictions.

2. T/F. UDF’s and Stored Procedures may only be written in the C language.

3. Briefly describe the functionality that SQL/PL provides.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 8. An Introduction to UDTs/UDFs and Stored Procedures 8-13

Page 271: Ce 1319 Stud

Student Notebook

.

Figure 8-11. Unit summary CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2012

Unit summary

Having completed this unit, you should be able to:• Describe the concepts behind User-Defined Distinct Types,

User-Defined Functions and Stored Procedures

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

8-14 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 272: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Unit 9. SQL and DB2 Performance

What this unit is about

It is helpful for those who write SQL to have a basic understanding of DB2 performance. In this unit the students will learn general guidelines for writing SQL statements that will yield the best performance and minimize the use of system resources when executed.

What you should be able to do

After completing this unit, you should be able to:

• After completing this unit, you should be able to:

• Explain some basic principles of DB2 performance

• Code SQL statements to obtain better performance

How you will check your progress

• Checkpoint

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-1

Page 273: Ce 1319 Stud

Student Notebook

.

Figure 9-1. Unit objectives CE1319.4

Notes:

It is helpful for those who write SQL to have a basic understanding of DB2 performance. In this unit the students will learn general guidelines for writing SQL statements that will yield the best performance and minimize the use of system resources when executed.

© Copyright IBM Corporation 2007, 2013

Unit objectives

After completing this unit, you should be able to:• After completing this unit, you should be able to:• Explain some basic principles of DB2 performance• Code SQL statements to obtain better performance

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 274: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 9-2. The Optimizer CE1319.4

Notes:

The Optimizer is used to calculate access paths for SQL regardless of its source: SPUFI, QMF, the Command Center, the command line, or an application.

The Optimizer will not use an index if the amount of data is very small: in this case, the Optimizer may calculate that it is cheaper to read all the rows with a table scan and discard rows that do not satisfy the predicates in the query.

Linux, UNIX, and Windows versions of DB2 all share the same code base so they also share the same Optimizer. Mainframe versions of the Optimizer have also changed with virtually every release.

The Optimizer can rewrite queries in a variety of different ways. For example, the Optimizer can:

• Rewrite subqueries as joins (when the Optimizer rewrites a query, it is sometimes referred to as AQR – Automatic Query Rewrite)

• Merge views

• Eliminate redundant DISTINCTs

© Copyright IBM Corporation 2007, 2013

• The Optimizer chooses the access path for all SQL statements• The Optimizer calculates the cost of each possible access path

for a given query and uses the path that is the cheapest– As a result, the Optimizer may choose not to use a given index as an

access path if its calculations show that some other approach is cheaper

• Each code version and operating system for DB2 has its own distinct Optimizer

– The Optimizers have been evolving steadily since DB2 was first released

• Each Optimizer is capable of rewriting some queries to improve performance

– The rewrite techniques vary for each Optimizer

• Poorly written SQL can dramatically hurt the performance of a given query

– The Optimizer cannot always rewrite these queries

• Changes that help a query run better under one Optimizer may not help the same query run better under another Optimizer

The Optimizer

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-3

Page 275: Ce 1319 Stud

Student Notebook

.

• Convert correlated subqueries to non correlated subqueries

• Convert multiple OR predicates into an IN predicate

• Convert a 'NOT <' predicate to a '>=' predicate

Some queries tend to have poor performance and cannot always be rewritten by the Optimizer. For example, a '<>'-predicate cannot be turned into a positive predicate.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 276: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 9-3. Index concepts CE1319.4

Notes:

Indexes are used by DB2 in much the same way as indexes in books are used by people. The index contains two major parts - a desired key value and a pointer that identifies where the corresponding row can be found in the table. This identifier is known as the RID, which is an acronym for Record IDentifier.

The index is structured in strict ascending or descending sequence on the columns in the index. The desired value can be located quickly in the index because of this ascending or descending sequence. This again is similar to searching the index in the back of a book. For example, assume you need to look in the index of a book for any information concerning “configuration”. Because the index is ordered, you need not scan through the entire index. You can quickly find any entries that match your search criteria by finding the C part of the index and applying your knowledge of the alphabet.

An index is not essential for finding data: it is essential for finding the data quickly though. It is still possible to find data in a table that has no indexes, but, in this case, every row of the table must be read. Indexes simply reduce the time needed to get the data.

© Copyright IBM Corporation 2007, 2013

VALUE LOCATION (RID)

INDEX

A

B

C

D

Index concepts

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-5

Page 277: Ce 1319 Stud

Student Notebook

.

Figure 9-4. Clustered and Non-clustered indexes CE1319.4

Notes:

An index is structured as a binary tree. This structure enables the database manager to find values in the index rapidly.

The root node is at the top of this structure. In the graphic, the root node contains an entry for each non-leaf node on the next level down. The entry in the root node consists of the high value contained on the next level and a pointer to this node. Entries are represented by integers in the graphic and pointers are represented by arrows. For example, the '45' entry in the root node in the graphic points down to the second non-leaf node on the next level. There is always just one root node.

The non-leaf nodes are similar in structure to the root node, except that the range of values addressed is narrower. A non-leaf node contains an entry for each of the nodes on the next level down. (In this case, there is only one non-leaf level, but it is possible to have several non-leaf levels.) The entries in a non-leaf node consist of the high values contained on a node on the next level down and a pointer to this node. The '13' in the first non-leaf node points to the third node on the next level.

© Copyright IBM Corporation 2007, 2013

8 13 33 45 75 86

45 86

8 13 33 45 75 86

45 86

Root Node Root Node

Non-leafNode

Data Page Data Page

Non-leafNode

LeafNode

LeafNode

ClusteringIndex

Non-ClusteringIndex

13 13

4 420 20

Clustered and Non-clustered indexes

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 278: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

The leaf nodes contain the value/RID pairs themselves. The leaf nodes collectively address the entire table. Each leaf node contains actual key values and the RIDs which identify the location in the data pages of the rows that have those key values.

The data pages contain the full rows of the table. The RIDs on the leaf nodes point to the location of the row on the data page.

A clustered index means that the sequence of the data in the table is in the same sequence as the index, or almost in the same sequence. For a clustered index, DB2 tries to maintain that sequence. An non clustered index is an index, where the data in the table is in a relatively random sequence as compared to values in the index.

The cluster ratio is the percentage of data rows that is in sequence by a given clustering index. A value of 100% means that all of the data rows are in the same sequence as the index.

A clustered index can be very advantageous if you are processing the data sequentially, because once you have read one data page into memory, it is highly likely that the next row you need is on the same data page. You might immediately think of applications of this in batch-type programs that will be processing many rows of data in sequence. For example, if you want to get all orders for a given week and the rows have been stored in ORDER_DATE sequence because ORDER_DATE was the clustering index, you should have no problems at all getting the rows for a given week (if the cluster ratio is high) since the rows for the same week would be effectively contiguous on a few pages.

Clustering indexes help range processing (for example, getting all the rows whose order date is in a given range), but do not do much for random accesses (for example, getting specific individuals from the employee table).

In DB2, clustering is requested by defining a specific index with the keyword CLUSTER. There can only be one clustering index per table because DB2 can only store the data in one sequence. You can change the choice of the clustering index only by dropping and recreating the index. DB2 cannot always maintain clustering as it inserts and updates data so the cluster ratio can get smaller over time. A low value for CLUSTERRATIO should prompt the DBA to do a REORG of the table.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-7

Page 279: Ce 1319 Stud

Student Notebook

.

Figure 9-5. Index utilization CE1319.4

Notes:

The graphic illustrates an index and various predicates that DB2 might encounter. The index is on the combination of LASTNAME, FIRSTNAME, and AGE in an imaginary table. Assume that there are thousands or millions of rows in the imaginary table.

Given the SQL statements on the right of the graphic, this is the reasoning that DB2 would go through to decide whether it was going to use the index.

1. This statement requests all the columns for all rows for people named 'Free'. Since the predicate applies to the first (leftmost) column in the index, the predicate is range-delimiting. The index only needs to be examined for those entries that have 'Free' in the first part of the index. The RIDs for those entries could then be used to access the data rows of the people named 'Free'.

2. This statement requests only the last name of people that have a first name of 'David'. The predicate is not range-delimiting, because the binary structure of the index cannot be exploited to limit the search. (Someone with the first name of 'David' could have any last name, and, therefore, appear in any leaf node.) However, the predicate is index-sargable: the column to be evaluated, the first name, does appear in the index.

© Copyright IBM Corporation 2007, 2013

LASTNAME FIRSTNAME AGE RIDAbell Jim 40...Campbell Debbie 32...Cheng David 45

Free Ann 64Free Bernie 66Free David 35Free Debbie 39Free Dennis 37Free Diane 41Free Donald 33Free Donna 42Free Doreen 36Free Doug 34

Klonne Dennis 42...

SELECT *WHERE LASTNAME = 'Free'

1

SELECT LASTNAMEWHERE FIRSTNAME = 'David'

2

SELECT *WHERE LASTNAME = 'Free'AND FIRSTNAME > 'Diane'

3

SELECT *WHERE LASTNAME = 'Free'AND AGE > 36

4

SELECT ...WHERE ...GROUP BY LASTNAME

-or-ORDER BY LASTNAME

5

Index utilization

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 280: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

DB2 will have to read the entire index, but it will be able to find the people named 'David' without having to go outside of the index. Also, since the last names of the Davids are also in the index, DB2 only needs to find the Davids in the FIRSTNAME column and then look to the left to get the corresponding LASTNAME. The table will not have to be accessed at all. This is called index-only access and is very desirable since it performs very well. Index-sargable predicates can be more efficient than relational scans in cases where: - The number of data pages is large compared to the number of leaf-node pages - Other predicates have been evaluated as range-delimiting predicates in the same composite index - The index itself provides all the necessary information to resolve the SQL statement. This is the case in Example 2.

3. Both of the predicates in this example are range-delimiting predicates. Even though the second predicate is not an equal predicate, it can assist in narrowing the number of leaf pages that have to be scanned in the index. It is not necessary for the index manager to read all entries that have a LASTNAME = 'Free', because the second predicate (FIRSTNAME > 'Diane') provides information that can be exploited to position the search. Certainly, a predicate such as FIRSTNAME = 'Diane' would limit the search even further, but the given predicate does provide useful information.

Note

When determining the number of range-delimiting predicates for a multiple-column index, the order of columns in the index is significant. Also, any predicates beyond the first non equal predicate cannot be exploited as range-delimiting, although they would be index-sargable. For example, assume a third predicate (AGE = 37) was added to this example. The Age predicate could not be exploited to reduce the amount of reading that had to be done in the index, since the prior column in the index (FIRSTNAME) is not in an equal predicate.

4. This statement has a range-delimiting predicate and an index-sargable predicate. LASTNAME = 'Free' is a range-delimiting predicate for the reasons discussed in earlier examples. AGE > 36 cannot be used to limit the search since there is an intervening column in the index for which DB2 has not been given a value. However, the AGE could be used for comparison. Therefore, the second predicate is index-sargable. DB2 would find all index entries that satisfy both predicates and then read only the data rows for those index entries.

5. The last statement is provided to highlight a function of the database manager. Data may be accessed via an index to avoid a sort if it is determined to be less costly than other access strategies followed by a sort. The GROUP BY clause and ORDER BY clause are just two SQL constructs that require ordered data; others are documented in the Administration Guide. In these cases, DB2 calculates the cost of accessing all of the qualifying rows and then sorting them and compares this with the cost of reading the

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-9

Page 281: Ce 1319 Stud

Student Notebook

.

qualifying rows in sequence via the index. DB2 will use whichever approach is the cheapest.

The process of determining the least expensive access path to satisfy an SQL statement is called optimization. This occurs during the BIND of the DB2 application.

Note

When multiple predicates were provided in the examples above, the logical operator between the predicates was the 'AND' operator. This is necessary for the predicate LASTNAME = 'Free' to be considered range-delimiting. If the logical operator was 'OR', isolating individuals with LASTNAME = 'Free' would not be considered range-delimiting.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 282: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 9-6. Predicates CE1319.4

Notes:

A predicate is a single search condition. The first and second predicates in the graphic,'A = B' and 'C = D', are often referred to as “equal predicates”. The third search criteria,'J > K', is a “range predicate”.

© Copyright IBM Corporation 2007, 2013

WHERE AND C = D OR J > KA = B

Predicate

Predicate

Predicate

Predicate = ONE Condition in a WHERE Clause

Predicates

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-11

Page 283: Ce 1319 Stud

Student Notebook

.

Figure 9-7. Predicate processing CE1319.4

Notes:

There are three main components of DB2 that execute predicates: the Index Manager, Data Management Services, and Relational Data Services. Each is responsible for different types of predicates.

Predicates that can be evaluated through the use of an index are evaluated by the Index Manager. These predicates include range-delimiting and index-sargable predicates.

• A range-delimiting predicate allows the index manager to narrow the search of index pages. If DEPTNO was the leftmost column of an index and the query contains the predicate 'DEPTNO = 10', this predicate is considered range-delimiting. DB2 can use this index to limit the number of index pages it examines so that only pages with a DEPTNO value of 10 are examined. This will most likely save considerable I/O with respect to reading the entire index or table.

• An index-sargable predicate does not limit the range of pages examined, but it can help find the data faster. If SALARY was a column of an index, but was not one of the leading columns and the values in the leading columns was unknown, the predicate'SALARY > 50000' would be considered index-sargable. The predicate could not be

© Copyright IBM Corporation 2007, 2013

best

PERFORMANCE

worst

Data-sargable predicates

Data Management Services

DEPTNO = 10 whenDEPTNO not in index

DEPTNO = 10Index Manager

Index-sargable predicates

Range-delimiting predicatesSALARY > 50000when SALARY onleading index column

Residual (stage 2) predicates

Relational Data ServicesCorrelated subqueriesQuantified subqueries(ANY, ALL, SOME, IN)

LONG VARCHAR

(stage 1)

Predicate processing

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-12 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 284: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

used to reduce the number of index pages read, but it could still be used to reduce the number of data rows which the query would have to read.

Predicates that cannot be evaluated by the index manager, but do not require the more complex algorithms of relational data services, are called data-sargable predicates. An example of a data-sargable predicate is DEPTNO = 'D10' when DEPTNO is not in an index. These predicates do not reduce the number of index rows read nor the number of data rows read, but they do reduce the number of data rows returned to the result set.

Predicates that require more complex evaluation require relational data services and are called residual (remaining) predicates. In DB2 for z/OS, they are also referred to as Stage 2 or non-sargable predicates. Correlated subqueries, quantified subqueries, and predicates that use LONG VARCHAR columns are all examples of residual predicates. The term residual also infers that these predicates are evaluated after the other predicates have been applied. The presence of a residual predicate is not in itself cause for concern. However, limiting the number of residual predicates can enhance performance.

Although there are always exceptional cases, the best performance is obtained from range-delimiting predicates and the worst from residual predicates. The user or programmer does not need to analyze each predicate manually in order to predict performance. The EXPLAIN facility provided with DB2 will generate an explanation of the access paths that DB2 will use to resolve each part of the SQL statement.

Let us look at some general guidelines that could help you write better-performing SQL statements.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-13

Page 285: Ce 1319 Stud

Student Notebook

.

Figure 9-8. General guidelines - Correlated subqueries CE1319.4

Notes:

Correlated subqueries tend to perform quite poorly. The first example illustrates why.

When it encounters an SQL statement that uses a non correlated subquery, DB2 executes the subquery first and passes the result set to the outer query to control the rows which are returned by the outer query. The subquery is performed only once so it tends to be fairly inexpensive, even if it has to read many rows.

By contrast, a correlated subquery involves repeated executions of the subquery. In the case of the first example, a single row of the EMPLOYEE table is read by the outer query and EMPNO, LASTNAME, WORKDEPT, and SALARY are determined from this row. Then, the WORKDEPT value is passed to the subquery, and the subquery is used to calculate the average salary for all people in that department. A single value is passed back to the outer query, which then compares the salary of the individual it read earlier with the average salary determined by the subquery. If the individual salary is higher than the average, the row found by the subquery is written to the result set; otherwise it is omitted. This process is followed for each row that satisfies the outer query. In a worst-case

© Copyright IBM Corporation 2007, 2013

SELECT EMPNO, LASTNAME, WORKDEPT, SALARYFROM EMPLOYEE EWHERE

SALARY >(SELECT AVG(SALARY)

FROM EMPLOYEEWHERE WORKDEPT = E.WORKDEPT)

SELECT EMPNO, LASTNAME, E.WORKDEPT, SALARYFROM EMPLOYEE E INNER JOIN

(SELECT WORKDEPT, AVG(SALARY) AS AVG_SALARYFROM EMPLOYEEGROUP BY WORKDEPT

) AS DON E.WORKDEPT = D.WORKDEPT

WHERE SALARY > AVG_SALARY

General guidelines – Correlated subqueries

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-14 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 286: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

situation, if the EMPLOYEE table contains a million rows, the subquery will be executed a million times.

Because of the possibility of poor performance, many people avoid correlated subqueries whenever possible. However, this may be too drastic: the Optimizers have been improved so their performance is considerably better than the worst-case scenario discussed above. These optimizers use a scratchpad area to record the subquery results for given values of the correlation value.

For example, the average salary for each department is written to the scratchpad when the average for the department is calculated the first time. When the outer query finds a row with the same correlation value as one that has been saved on the scratchpad, DB2 simply goes to the scratchpad to find the value. That eliminates the need to recalculate the average salary for a given department after it has been done once. This technique improves the performance of the correlated subquery considerably. Given the same million-row EMPLOYEE table and only 100 departments for which the employees work, an Optimizer that uses a scratchpad will only have to perform the subquery 100 times, which is much better than the million times that would have been required in the worst-case scenario.

The second example produces the same result as the first example, but is not correlated and performs considerably better, even on a platform where scratch pads are used to handle correlated subqueries. In this case, it is cheaper to do the same work as a non-correlated subquery.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-15

Page 287: Ce 1319 Stud

Student Notebook

.

Figure 9-9. General guidelines - Minimize DB2 sorts CE1319.4

Notes:

All of the following may require sorts: ORDER BY, GROUP BY, DISTINCT, UNION, EXCEPT, INTERSECT, and some joins. In some cases, DB2 will read the data in the correct order via the index and avoid the sort, but, in all other cases, DB2 will have to sort. Sorts add to the processing done by the query: They increase CPU requirements and can require additional I/O activity in DB2's sort work area. Sorts are handled by the Relational Data Services component of DB2, the part that handles all the residual predicates. Therefore, performance can suffer if sorts are needed.

Do not request sorts unless you need sorted data.

When it makes sense, create indexes on the columns that are used in the ORDER BY, GROUP BY, and other sort-provoking statements. This will enable DB2 to consider using indexes to read the rows via the index and eliminate the sort.

© Copyright IBM Corporation 2007, 2013

AFEW

KEYSTROKES

I/OCPU>

EXEC SQL

ORDER BY

DB2 MAY USE ANINDEX TO AVOID

AN INTERNAL SORT

TABLE

WORKDEPT SALARY . . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

INDEX ON WORKDEPT

VALUE RIDEXEC SQL

GROUP BY WORKDEPT

General guidelines – Minimize DB2 sorts

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-16 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 288: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 9-10. General guidelines - Use views CE1319.4

Notes:

The view in the example could be easily created by using the two base tables. If users needed both of the columns in the view, the view could save effort among end users. The DBA who created the view could ensure that the join was done correctly so that it avoided Cartesian Products and could even create extra indexes, if needed, to ensure the best possible join performance. In this case, the view could be considered a performance aid.

However, if the users really only needed one of the columns in the view, a view that was based on a join would cause unnecessary processing to join the two tables. In this case, the user should either have a view that was based on a single table to avoid the unnecessary join or, possibly, have access to the base table that contains the needed data.

© Copyright IBM Corporation 2007, 2013

SUBSET

JOIN

BASE TABLES

VIEW

EMPLOYEE Table

EMPNO LASTNAME WORKDEPT SALARY000010

000030

000120000130000140

HAASKWANO'CONNELLQUINTANA

NICHOLLS

A00C01A00C01C01

52750.0038250.00

29250.0023800.0028420.00

DEPARTMENT Table

DEPTNAMEDEPTNO MGRNO

DEVELOPMENT CENTER

SPIFFY COMPUTER SERVICE DIV.

D01

A00C01 INFORMATION CENTER

000010000030

NAME DEPARTMENTHAASKWAN

SPIFFY COMPUTER SERVICE DIV.

INFORMATION CENTER

EMP_DEPT View

General guidelines – Use views

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-17

Page 289: Ce 1319 Stud

Student Notebook

.

Figure 9-11. General guidelines - Expressions CE1319.4

Notes:

Scalar functions are considered expressions. DB2 will never use an index to satisfy a predicate that contains an expression, unless the expression evaluates to a constant.

The first example will almost never use an index because its predicate, “SUBSTR(WORKDEPT,1,2) = 'A0'”, uses the SUBSTR() function, which is considered an expression by DB2.

The second example does not contain an expression. Therefore, DB2 will consider using an index to satisfy this query.

Other SQL constructs including arithmetic, the concatenation operator, and casting, are also considered expressions. Refer to the SQL Reference manual for your platform to get a complete list of expressions. People writing SQL should investigate alternatives to coding expressions in predicates.

© Copyright IBM Corporation 2007, 2013

Given:WORKDEPT is a column of table EMPLOYEEThere is a nonunique index on column WORKDEPT

Avoid expressions in predicates whenever possible

Example 1:SELECT EMPNO, LASTNAME

FROM EMPLOYEEWHERE SUBSTR (WORKDEPT, 1, 2) = 'A0'

Example 2:SELECT EMPNO, LASTNAME

FROM EMPLOYEEWHERE WORKDEPT LIKE 'A0%'

General guidelines – Expressions

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-18 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 290: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 9-12. General guidelines - NOT EQUAL Predicates CE1319.4

Notes:

DB2 for Linux, UNIX, and Windows can use the index on WORKDEPT to build the result sets for both examples. However, the total cost of the first example is much higher than the total cost of the second example.

DB2 for z/OS does not use the index on WORKDEPT to build the result set for the first example, but it can use that index for the second example. The fact that the predicate in the first example uses a not-equal ('<>') operator is the factor that causes the index to be bypassed.

© Copyright IBM Corporation 2007, 2013

Given:WORKDEPT is a column of table EMPLOYEEAll employees belong to departments A00 or C01 There is a nonunique index on column WORKDEPT

Example 1:SELECT EMPNO, LASTNAME, SALARY

FROM EMPLOYEEWHERE WORKDEPT <> 'A00'

Example 2:SELECT EMPNO, LASTNAME, SALARY

FROM EMPLOYEEWHERE WORKDEPT = 'C01'

NOT predicates may not be very efficient

General guidelines – NOT EQUAL Predicates

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-19

Page 291: Ce 1319 Stud

Student Notebook

.

Figure 9-13. General guidelines - Arithmetic CE1319.4

Notes:

DB2 for Linux, UNIX, and Windows do not use the index on SALARY for the first example, but can use it for the second example. The total cost of the first example is almost three times as high as that for the second example.

DB2 for z/OS does not use the index on SALARY for the first example, but can use it for the second. The fact that the predicate in the first example does arithmetic is the factor that causes the index to be bypassed.

© Copyright IBM Corporation 2007, 2013

Given:SALARY is a column of table EMPLOYEEThere is a nonunique index on column SALARY

Avoid arithmetic in predicates whenever possible

Example 1:SELECT EMPNO, LASTNAME

FROM EMPLOYEEWHERE SALARY+2000 > 40000

Example 2:SELECT EMPNO, LASTNAME

FROM EMPLOYEEWHERE SALARY > 38000

General guidelines – Arithmetic

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-20 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 292: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 9-14. General guidelines - Search value length CE1319.4

Notes:

DB2 for Linux, UNIX, and Windows uses the index on WORKDEPT to build the result sets for both examples. The costs of the two examples are identical in this case.

In DB2 for z/OS, the index on WORKDEPT is used to build the result set in both cases. Prior to V8, however, the first example did not use the index, but the second did, the fact that the search value is longer than the width of the WORKDEPT column being significant.

© Copyright IBM Corporation 2007, 2013

Given:WORKDEPT is a column of table EMPLOYEEWORKDEPT is defined as CHAR(3)There is a nonunique index on column WORKDEPT

Avoid making search values longer than the searchedcolumn whenever possible

Example 2:SELECT EMPNO, LASTNAME

FROM EMPLOYEEWHERE WORKDEPT = 'A00'

Example 1:SELECT EMPNO, LASTNAME

FROM EMPLOYEEWHERE WORKDEPT = 'A00 '

General guidelines – Search value length

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-21

Page 293: Ce 1319 Stud

Student Notebook

.

Figure 9-15. General guidelines - Conversions CE1319.4

Notes:

DB2 for Linux, UNIX, and Windows uses the index on EDLEVEL to build the result sets for both examples. The costs of the two examples are very similar in this case.

In DB2 for z/OS, both queries use the same optimization methods (a scan is preferred for greater than 15 or than 15.00, but an index on EDLEVEL is used for equal to 15 or 15.00). Prior to V8, however, the first example did not use the index, but the second did, the fact that the first search value is in a different format to that of EDLEVEL being significant.

© Copyright IBM Corporation 2007, 2013

Given:EDLEVEL is a column of table EMPLOYEEEDLEVEL is defined as SMALLINTThere is a nonunique index on EDLEVEL

Avoid using search values that have different data typesfrom searched column whenever possible

Example 1:SELECT EMPNO, LASTNAME

FROM EMPLOYEEWHERE EDLEVEL > 15.00

Example 2:SELECT EMPNO, LASTNAME

FROM EMPLOYEEWHERE EDLEVEL > 15

General guidelines – Conversions

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-22 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 294: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 9-16. General guidelines - Retrieve only necessary data CE1319.4

Notes:

Only select the columns that are needed to satisfy the SQL query, which is written in a manner to meet the requirements of the application. SELECT * in applications programs are usually not necessary, and predictably undesirable from a performance standpoint. Each column requested from DB2 will require additional processing.

Likewise, only retrieve the rows that qualify according to the requirements of your application (i.e. use local predicates to filter rows to an absolute minimum). In a nutshell, MINIMIZE the columns in the SELECT clause, and MAXIMIZE the predicates in the WHERE clause, so that you return only the data that is needed to meet the business requirements.

© Copyright IBM Corporation 2007, 2013

General guidelines – Retrieve only necessary data

SELECT *

WHERE condition-1

AND condition-2

OR condition-3

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-23

Page 295: Ce 1319 Stud

Student Notebook

.

Figure 9-17. General guidelines - Statements CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

General guidelines - Statements• Return only the columns that are actually needed• Use local predicates• Write joins if equivalent to subquery• UNION ALL performs better than UNION• Use Evaluate at Open subqueries when possible

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-24 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 296: Ce 1319 Stud

Student NotebookV7.0

Uempty

E

Figure 9-18. Checkpoint CE1319.4

Notes:

Please write your answers here:

1.

2.

3.

© Copyright IBM Corporation 2007, 2013

Checkpoint

1. How much data must DB2 read to satisfy a WHERE condition if no index is available?

2. What kinds of predicates can be evaluated very efficiently?

3. Why will a correlated subquery sometimes perform poorly?

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Unit 9. SQL and DB2 Performance 9-25

Page 297: Ce 1319 Stud

Student Notebook

.

Figure 9-19. Unit summary CE1319.4

Notes:

© Copyright IBM Corporation 2007, 2013

Unit summaryHaving completed this unit, you should be able to:• After completing this unit, you should be able to:• Explain some basic principles of DB2 performance• Code SQL statements to obtain better performance

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

9-26 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 298: Ce 1319 Stud

Student NotebookV7.0.1

AP

E

Appendix A. Checkpoint Solutions

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Appendix A. Checkpoint Solutions A-1

Page 299: Ce 1319 Stud

Student Notebook

.

Unit 1. Introduction

© Copyright IBM Corporation 2007, 2013

Checkpoint Solutions

1. z/OS, VM, VSE, Unix (AIX, Solaris, or HP-UX), Windows,OS/400

2. True

3. Column functions MIN, MAX and AVG

4. The GROUP BY clause

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

A-2 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 300: Ce 1319 Stud

Student NotebookV7.0.1

AP

E

Unit 2. Create Objects

© Copyright IBM Corporation 2007, 2013

Checkpoint Solutions

1. Create a unique index.

2. True

3. A primary key in the parent table and a foreign key in the dependent table.

4. Create a trigger.

5. False. Views can be based on single views, joins of tables, joins of views, or joins of tables and views.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Appendix A. Checkpoint Solutions A-3

Page 301: Ce 1319 Stud

Student Notebook

.

Unit 3. Join

© Copyright IBM Corporation 2007, 2012

Checkpoint Solutions

1. A left or right outer join

2. False. Each row from the first table will be combined with every row of the second table. The result is know as a Cartesian Product.

3. No. But there are a number of ways to code an “anti-JOIN”query in order to obtain the desired result.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

A-4 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 302: Ce 1319 Stud

Student NotebookV7.0.1

AP

E

Unit 4. CASE, CAST, Summary Tables and Temporary tables

© Copyright IBM Corporation 2007, 2013

Checkpoint Solutions

1. True

2. True

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Appendix A. Checkpoint Solutions A-5

Page 303: Ce 1319 Stud

Student Notebook

.

Unit 5. Using Subqueries

© Copyright IBM Corporation 2007, 2012

Checkpoint Solutions

1. ALL, ANY and SOME

2. True

3. A correlated subquery

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

A-6 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 304: Ce 1319 Stud

Student NotebookV7.0.1

AP

E

Unit 6. Scalar Functions

© Copyright IBM Corporation 2007, 2012

Checkpoint Solutions

1. The SUBSTR function

2. Use the DIGITS function to convert the value into an alphanumeric format. Then, use the SUBSTR function to extract the digits to the right of the decimal point.

3. A Timestamp consists of a date, a time, and a number of microseconds. It is used to identify exactly when something happened, to one millionth of a second.

4. The DAYS function determines the difference in days between the date in the argument of the function and January 1, 0001.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Appendix A. Checkpoint Solutions A-7

Page 305: Ce 1319 Stud

Student Notebook

.

Unit 7. Table Expressions and Recursive SQL

© Copyright IBM Corporation 2007, 2012

Checkpoint Solutions

1. True 2. Ways in which a table expression is more convenient:

a. Table expression does not need to be CREATEd b. Access to the table expression does not need to be GRANTed c. Table expressions can use host variables

3. True

4. It contains a common table expression that refers to itself within its definition.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

A-8 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 306: Ce 1319 Stud

Student NotebookV7.0.1

AP

E

Unit 8. An Introduction to UDTs/UDFs and Stored Procedures

© Copyright IBM Corporation 2007, 2012

Checkpoint Solutions

1. False. They must be compared to “compatible” data types.

2. False. UDF’s and SP’s may now be written in SQL.

3. SQL/PL adds procedural language constructs to the SQL query language, making it possible to write SQL applications that have both program logic and query capability.

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

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

© Copyright IBM Corp. 2007, 2013 Appendix A. Checkpoint Solutions A-9

Page 307: Ce 1319 Stud

Student Notebook

.

Unit 9. SQL and DB2 Performance

© Copyright IBM Corporation 2007, 2013

Checkpoint Solutions

1. If there is no index, DB2 must read all the rows in the table.

2. Range-delimiting predicates, and indexable-sargable predicates perform very efficiently.

3. The correlated subquery may need to perform the subquery once for each row that satisfies the WHERE clause, resulting in heavy I/O and CPU consumption.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

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

A-10 DB2 SQL Workshop for Experienced Users © Copyright IBM Corp. 2007, 2013

Page 308: Ce 1319 Stud

V6.0

backpg

E

Back page

xclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C.

Page 309: Ce 1319 Stud

.

Exclus

ivo fo

rmac

ión

proy

ecto

C.F.T.I.C

Page 310: Ce 1319 Stud

CONTACTO

Teléfono91 761 21 78Póngase en contacto con nuestro equipo y le informaremos de cualquier duda o cuestión que pueda surgirle.

[email protected]ándenos un email y le atenderemos enseguida.

Online@Arrow_Edu_ESO bien puede contactarnos a través de nuestro perfil en Twitter.

VisítenosArrow ECS Education ServicesAvenida de Europa 21, Parque Empresarial La Moraleja28108 Alcobendas, Madrid

EDUCATIONS E R V I C E S