Você está na página 1de 310

Business

Intelligence

Data
Centre

Cloud

Mobility

Enterprise Computing Solutions

Student Manual

Direccin General de Formacin


CONSEJERA DE EMPLEO,
TURISMO Y CULTURA

Comunidad de Madrid

UNIN EUROPEA
FONDO SOCIAL EUROPEO
El Fondo Social Europeo invierte en tu futuro

EDUCATION
S

V6.0

cover

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Front cover

DB2 SQL Workshop for


Experienced Users
(Course code CE131)

Student Notebook

pr

Ex

cl

ERC 9.4

IBM certified course material

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The following are trademarks of International Business Machines Corporation, registered in


many jurisdictions worldwide:
AIX
DB2 Connect
iSeries
OS/390
WebSphere
400

AS/400
DB2
MVS
OS/400
z/OS

Command Center
IMS
Notes
QMF
zSeries

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.

Ex

cl

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

pr

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 customers ability to evaluate and integrate them into the customers 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.

V6.0
Student Notebook

Contents
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Course description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

0-1
0-2
0-3
0-4
0-5
0-6

cl

Unit 1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1


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

Ex

TOC

pr

Unit 2. Create Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1


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

Copyright IBM Corp. 2007, 2013


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

Contents

iii

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Create index and primary key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-16


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

pr

Ex

cl

Unit 3. Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-1


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

iv

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

cl

Unit 5. Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-1


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

Ex

TOC

pr

Unit 6. Scalar Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
Functions - Two types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3
Scalar functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
Scalar function - SUBSTR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Position string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7
Scalar function - COALESCE/VALUE (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8
Scalar function - COALESCE/VALUE (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10
Scalar function - DECIMAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11
Scalar function - ROUND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-13
Copyright IBM Corp. 2007, 2013
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Contents

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Scalar function - DIGITS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-15


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

Ex

cl

Unit 7. Table Expressions and Recursive SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-1


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

pr

Unit 8. An Introduction to UDTs/UDFs and Stored Procedures . . . . . . . . . . . . . . . .8-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-2
User-defined Distinct Types - The need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-3
User-defined Distinct Types - Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-4
User-Defined Functions - The need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-6
User-Defined Functions - Sourced . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-7
User-Defined Functions - External . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-9
User-Defined SQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-10
Stored Procedures - Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-11
vi

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Stored Procedures - Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12


Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Unit 9. SQL and DB2 Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-1


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

cl

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

pr

Ex

TOC

Copyright IBM Corp. 2007, 2013


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

Contents

vii

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

viii

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
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:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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:
AIX
DB2 Connect
iSeries
OS/390
WebSphere
400

AS/400
DB2
MVS
OS/400
z/OS

Command Center
IMS
Notes
QMF
zSeries

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.

cl

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.

pr

Ex

TMK

Copyright IBM Corp. 2007, 2013


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

Trademarks

ix

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Course description
DB2 SQL Workshop for Experienced Users

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

cl

Objectives

After completing this course, you should be able to:

pr

Ex

pref

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

Copyright IBM Corp. 2007, 2013


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

Course description

xi

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

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

Performance Considerations

xii

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Agenda
Day 1

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

cl

Unit 7 - Table Expressions and Recursive SQL


Exercise 7
Unit 8 - UDTs/UDFs and Performance
Exercise 8
Unit 9 - SQL and DB2 Performance
Exercise 9

pr

Ex

pref

Copyright IBM Corp. 2007, 2013


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

Agenda

xiii

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

xiv

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 0. DB2 SQL Workshop for Experienced Users


What this unit is about
This course provides information about the functions of IBM's

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Ex

cl

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

pr

Checkpoints

Copyright IBM Corp. 2007, 2013

Unit 0. DB2 SQL Workshop for Experienced Users

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

0-1

Student Notebook

Course objectives
After completing this course, you should be able to:

Discuss basic relational database concepts

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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


Copyright IBM Corporation 2007, 2013

Figure 0-1. Course objectives

CE1319.4

pr

Ex

cl

Notes:

0-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Prerequisites

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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


Copyright IBM Corporation 2007, 2013

Figure 0-2. Prerequisites

CE1319.4

pr

Ex

cl

Notes:

Copyright IBM Corp. 2007, 2013

Unit 0. DB2 SQL Workshop for Experienced Users

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

0-3

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Agenda Day 1

Welcome

Unit 1 : Introduction
Lab Exercises

Unit 2 : Create Objects


Lab Exercises

Unit 3 : Join

Lab Exercises

Copyright IBM Corporation 2007, 2013

Figure 0-3. Agenda - Day 1

CE1319.4

pr

Ex

cl

Notes:

0-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Agenda Day 2

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Unit 4 : CASE, CAST, Summary


(Materialized Query) Tables
and Temporary Tables
Lab Exercises

Unit 5 : Using Subqueries


Lab Exercises

Unit 6 : Scalar Functions


Lab Exercises

Copyright IBM Corporation 2007, 2013

Figure 0-4. Agenda - Day 2

CE1319.4

pr

Ex

cl

Notes:

Copyright IBM Corp. 2007, 2013

Unit 0. DB2 SQL Workshop for Experienced Users

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

0-5

Student Notebook

Agenda Day 3

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Unit 7 : Table Expressions and


Recursive 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

Copyright IBM Corporation 2007, 2013

Figure 0-5. Agenda - Day 3

CE1319.4

pr

Ex

cl

Notes:

0-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 1. Introduction
What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

Checkpoint

Copyright IBM Corp. 2007, 2013


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

Unit 1. Introduction

1-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2013

Figure 1-1. Unit objectives

CE1319.4

pr

Ex

cl

Notes:

1-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

DB2 products
Intel Systems

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

UNIX Systems

DB2

Mainframe Systems

DB2

Midrange Systems

DB2

z/OS

DB2

DB2

OS/400

VSE
and VM

Copyright IBM Corporation 2007, 2013

Figure 1-2. DB2 products

CE1319.4

Notes:

Ex

cl

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 32and 64-bit operating systems); and UNIX systems such as AIX, HP-UX, and Solaris, and
Linux (including Linux for zSeries).

pr

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


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

Unit 1. Introduction

1-3

Student Notebook

DB2 system catalog tables

A set of System Catalog Tables is created and

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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


Copyright IBM Corporation 2007, 2013

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.

cl

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.

Ex

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.

pr

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.

1-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

- 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

pr

Ex

cl

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

Copyright IBM Corp. 2007, 2013


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

Unit 1. Introduction

1-5

Student Notebook

DB2 system catalog - Maintenance


To change the contents of the catalog, use the

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2013

Figure 1-4. DB2 system catalog - Maintenance

CE1319.4

Notes:

cl

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.

Ex

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

pr

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.

1-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Databases in DB2 for Linux, UNIX, and Windows


An instance of DB2

Database 1
Table_2

Table_n

Catalog

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Table_1

- Tables
- Columns

---------

.
.
.
.
.

---------

Requires federation Join

Database 2
Table_a

Table_b

Table_x

Catalog

- Tables
- Columns

---------

.
.
.
.
.

---------

Copyright IBM Corporation 2007, 2013

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.

cl

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

Ex

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.

pr

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


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

Unit 1. Introduction

1-7

Student Notebook

Subsystem in DB2 for z/OS


DB2 Subsystem
Catalog

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Database 1
Table_1

Table_2

- Systables
- Syscolumns

DB2 Log

.
.
.
.
.

Join

Database 2
Table_3

Table_4

Bufferpools

Table_n

Copyright IBM Corporation 2007, 2013

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

CE1319.4

Notes:

cl

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.

pr

Ex

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

1-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Selecting rows and columns


EMPNO

LASTNAME

SALARY

JOB

000120
000100
000130
000280
000250

O'CONNELL
SPENSER
QUINTANA
SCHNEIDER
SMITH

29250.00
26150.00
23800.00
26250.00
19180.00

CLERK
MANAGER
ANALYST
OPERATOR
CLERK
MANAGER

EDLEVEL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

14
14
16
17
15

000060 STERN
EMPLOYEE Table

32250.00

16

SELECT LASTNAME, SALARY, EMPNO


FROM EMPLOYEE
WHERE EDLEVEL <= 16
AND SALARY BETWEEN 19000 AND 30000
AND LASTNAME LIKE 'S%'
AND JOB IN ('MANAGER', 'OPERATOR', 'ANALYST')
ORDER BY LASTNAME

LASTNAME

Result SPENSER

SALARY

EMPNO

26150.00

000100

Copyright IBM Corporation 2007, 2013

Figure 1-7. Selecting rows and columns

CE1319.4

Notes:

Ex

cl

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.

pr

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


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

Unit 1. Introduction

1-9

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.SYSTABLES
WHERE 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.

pr

Ex

cl

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.

1-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Column functions
EMPNO

LASTNAME

SALARY

JOB

000120
000100
000130
000280
000250

O'CONNELL
SPENSER
QUINTANA
SCHNEIDER
SMITH

29250.00
26150.00
23800.00
26250.00
19180.00

CLERK
MANAGER
ANALYST
OPERATOR
CLERK
MANAGER

EDLEVEL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

14
14
16
17
15

000060 STERN
EMPLOYEE Table

32250.00

16

SELECT 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

TOTAL

MINIMUM

MAXIMUM

AVERAGE

156880.00

19180.00

32250.00

26146.66

#EMP #LVL
6

Copyright IBM Corporation 2007, 2013

Figure 1-8. Column functions

CE1319.4

Notes:

cl

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.

Ex

The main column functions are:

SUM() Returns the total value.

MIN() Returns the minimum value.

pr

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


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

Unit 1. Introduction

1-11

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

1-12 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Summarizing group values


EMPNO

LASTNAME

000120

O'CONNELL

29250.00

CLERK

14

000100

SPENSER

26150.00

MANAGER

14

000130

QUINTANA

23800.00

ANALYST

16

000280

SCHNEIDER

26250.00

OPERATOR

17

000250

SMITH

19180.00

CLERK

15

000060

STERN

32250.00

MANAGER

16

JOB

EDLEVEL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SALARY

EMPLOYEE Table

SELECT EDLEVEL, SUM(SALARY) AS GROUP_TOTAL


FROM EMPLOYEE
GROUP BY EDLEVEL
HAVING COUNT(*) > 1

Result

EDLEVEL

GROUP_TOTAL

14

55400.00

16

56050.00

Copyright IBM Corporation 2007, 2013

Figure 1-9. Summarizing group values

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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


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

Unit 1. Introduction

1-13

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

1-14 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

GROUP BY ROLLUP (DB2 LUW only)


LASTNAME

000120

O'CONNELL

SALARY
29250.00

JOB

EDLEVEL

CLERK

14

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO
000100

SPENSER

26150.00

MANAGER

14

000130

QUINTANA

23800.00

ANALYST

16

000280

SCHNEIDER

26250.00

OPERATOR

17

000250

SMITH

19180.00

CLERK

15

000060

STERN

32250.00

MANAGER

16

EMPLOYEE Table

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


AS AVG_SALARY
EDLEVEL
FROM EMPLOYEE
GROUP BY ROLLUP(EDLEVEL)
- (null)

Result

14
EDLEVEL
15
16

17

AVG_SALARY
26146.66
27700.00

19180.00
28025.00
26250.00

Copyright IBM Corporation 2007, 2013

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

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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


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

Unit 1. Introduction

1-15

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT EDLEVEL, JOB, AVG(SALARY) AS AVG_SALARY


FROM EMPLOYEE WHERE EMPNO IN
('000060','000100','000120','000130','000250','000280')
GROUP BY ROLLUP (EDLEVEL, JOB)
------- -------- --------------------------------EDLEVEL JOB
AVG_SALARY
------- -------- --------------------------------26146,666666666666666666666666
14
27700,000000000000000000000000
15
19180,000000000000000000000000
16
28025,000000000000000000000000
17
26250,000000000000000000000000
14
CLERK
29250,000000000000000000000000
14
MANAGER 26150,000000000000000000000000
15
CLERK
19180,000000000000000000000000
16
ANALYST 23800,000000000000000000000000
16
MANAGER 32250,000000000000000000000000
17
OPERATOR 26250,000000000000000000000000
11 record(s) selected.

pr

Ex

cl

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.

1-16 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

GROUP BY CUBE (DB2 LUW only)


EMPNO

LASTNAME

SALARY

JOB

EDLEVEL

O'CONNELL

29250.00

CLERK

14

000100

SPENSER

26150.00

14

000130

QUINTANA

23800.00

MANAGER
ANALYST

000280

26250.00

000250

SCHNEIDER
SMITH

000060

STERN

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000120

16

17

19180.00

OPERATOR
CLERK

15

32250.00

MANAGER

16

EMPLOYEE Table

EDLEVEL JOB

SELECT EDLEVEL,JOB,
DECIMAL(AVG(SALARY),8,2) AS
AVG_SALARY
FROM EMPLOYEE
WHERE EDLEVEL IN(14,15)
GROUP BY CUBE(EDLEVEL,JOB)

Result

14
15

CLERK
MANAGER

14
15
14

EDLEVEL

CLERK

CLERK
MANAGER

AVG_SALARY
27700.00
19180.00
24860.00
24215.00

26150.00
29250.00
19180.00
26150.00

Copyright IBM Corporation 2007, 2013

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

CE1319.4

Notes:

cl

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.

Ex

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

pr

Average salary by JOB

Average salary by the combination of EDLEVEL and JOB

Copyright IBM Corp. 2007, 2013


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

Unit 1. Introduction

1-17

Student Notebook

Grouping function Why?


LASTNAME

000120

O'CONNELL

SALARY
29250.00

JOB

EDLEVEL

CLERK

14

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO
000100

SPENSER

26150.00

MANAGER

14

000130

QUINTANA

23800.00

ANALYST

16

000280

SCHNEIDER

26250.00

OPERATOR

17

000250

SMITH

19180.00

CLERK

000060

STERN

32250.00

MANAGER

16

EMPLOYEE Table

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


AS AVG_SALARY
EDLEVEL
FROM EMPLOYEE
GROUP BY ROLLUP(EDLEVEL)

14
EDLEVEL

Result

16
17

AVG_SALARY
26146.66
27700.00
19180.00
28025.00
26250.00

Copyright IBM Corporation 2007, 2013

Figure 1-12. Grouping function - Why?

CE1319.4

Notes:

pr

Ex

cl

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.

1-18 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Grouping function Example


LASTNAME

000120

O'CONNELL

SALARY
29250.00

JOB

EDLEVEL

CLERK

14

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO
000100

SPENSER

26150.00

MANAGER

14

000130

QUINTANA

23800.00

ANALYST

16

000280

SCHNEIDER

26250.00

OPERATOR

17

000250

SMITH

19180.00

CLERK

000060

STERN

32250.00

MANAGER

16

EMPLOYEE Table

SELECT EDLEVEL, GROUPING(EDLEVEL),


DECIMAL(AVG(SALARY),8,2)
AS AVG_SALARY
FROM EMPLOYEE
GROUP BY ROLLUP(EDLEVEL)

EDLEVEL

Result

14
16
17

AVG_SALARY

1
0
0
0
0

26146.66
27700.00

19180.00
28025.00
26250.00

Copyright IBM Corporation 2007, 2013

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.

cl

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.

pr

Ex

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


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

Unit 1. Introduction

1-19

Student Notebook

GROUP BY grouping sets


EMPNO

LASTNAME

000120

O'CONNELL

29250.00

CLERK

1942-10-18

000100

SPENSER

26150.00

MANAGER

1956-12-18

000130

QUINTANA

23800.00

ANALYST

1925-09-15

000280

26250.00

OPERATOR

1936-03-28

000250

SCHNEIDER
SMITH

19180.00

CLERK

000060

STERN

32250.00

MANAGER

1939-11-12
1945-07-07

JOB

BIRTHDATE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SALARY

EMPLOYEE Table
SELECT JOB, DAY(BIRTHDATE),
DECIMAL(AVG(SALARY),8,2) AS
AVG_SALARY
FROM EMPLOYEE
WHERE YEAR(BIRTHDATE) > 1938
GROUP BY
GROUPING SETS (JOB,DAY(BIRTHDATE))

Result

JOB

DAY AVG_SALARY

CLERK
MANAGER

24215.00
29200.00

7
12
18

32250.00

19180.00

27700.00

Copyright IBM Corporation 2007, 2013

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

cl

GROUP BY JOB

Ex

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 ().

pr

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.

1-20 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Super groups Syntax

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

GROUP BY

expr

ROLLUP
CUBE

GROUPING SETS

expr
,
expr

( )

Copyright IBM Corporation 2007, 2013

Figure 1-15. Super groups - Syntax

CE1319.4

Notes:

pr

Ex

cl

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


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

Unit 1. Introduction

1-21

Student Notebook

OLAP function for ranking (for DB2 z/OS and


LUW)
SELECT EMPNO, LASTNAME, EDLEVEL,
ROW_NUMBER() OVER(ORDER BY EDLEVEL DESC)
FROM EMPLOYEE
ORDER BY EMPNO

EMPNO
-----000060
000100
000120
000130
000250
000280

EMPNO
-----000060
000100
000120
000130
000250
000280

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT EMPNO, LASTNAME, EDLEVEL,


RANK() OVER(ORDER BY EDLEVEL DESC)
FROM EMPLOYEE
ORDER BY EMPNO

LASTNAME
EDLEVEL RANKING
--------------- ------- -------------------STERN
16
2
SPENSER
14
5
O'CONNELL
14
5
QUINTANA
16
2
SMITH
15
4
SCHNEIDER
17
1

LASTNAME
EDLEVEL ROW_NUMBER
--------------- ------- -------------------STERN
16
2
SPENSER
14
5
O'CONNELL
14
6
QUINTANA
16
3
SMITH
15
4
SCHNEIDER
17
1

SELECT EMPNO, LASTNAME, EDLEVEL,


DENSE_RANK() OVER(ORDER BY EDLEVEL DESC)
FROM EMPLOYEE
ORDER BY EMPNO
EMPNO
-----000060
000100
000120
000130
000250
000280

LASTNAME
EDLEVEL DENSE_RANKING
--------------- ------- -------------------STERN
16
2
SPENSER
14
4
O'CONNELL
14
4
QUINTANA
16
2
SMITH
15
3
SCHNEIDER
17
1

Copyright IBM Corporation 2007, 2013

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

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.
1-22 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 2007, 2013


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

Unit 1. Introduction

1-23

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Additional SQL in this course

CREATE OBJECTS

TRIGGER

SUBQUERY

OUTER JOIN

VIEWS

PERFORMANCE

CHECK CONSTRAINTS

SCALAR FUNCTIONS

RECURSIVE SQL
CASE
EXPRESSIONS TABLE
EXPRESSIONS

Copyright IBM Corporation 2007, 2013

Figure 1-17. Additional SQL in this course

CE1319.4

Notes:

pr

Ex

cl

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

1-24 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Checkpoint
List three operating systems that can run DB2.
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.
Assume that you need to list the minimum, the maximum, and
the average salary in your company. Which functions do you
use?
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?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.
2.

3.

4.

Copyright IBM Corporation 2007, 2013

Figure 1-18. Checkpoint

CE1319.4

Notes:

Write your answers here:


1.

cl

2.

Ex

3.

pr

4.

Copyright IBM Corp. 2007, 2013


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

Unit 1. Introduction

1-25

Student Notebook

Unit summary
Having completed this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2013

Figure 1-19. Unit summary

CE1319.4

pr

Ex

cl

Notes:

1-26 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 2. Create Objects


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

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Checkpoint

pr

Ex

cl

How you will check your progress

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2013

Figure 2-1. Unit objectives

CE1319.4

Notes:

cl

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.

pr

Ex

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.

2-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Scenario (Part 1)
We create tables:
ORDER,
ORDER_ITEM,
ARTICLE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

How can we create a


data model to
administer my orders?

Bill

Susan

Copyright IBM Corporation 2007, 2013

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:

Ex

cl

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).

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-3

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

2-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Creating tables and adding columns (1 of 2)


INTEGER
DATE
SMALLINT

NOT NULL,
NOT NULL,
NOT NULL)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TABLE ORDER


(ORDER_NO
ORDER_DATE
CUST_NO

ALTER TABLE ORDER


ADD ORDER_REF INTEGER

CREATE TABLE ARTICLE


(ART_NO
INTEGER
NOT NULL,
ART_NAME
CHAR(20) NOT NULL,
STOCK_QTY
INTEGER
NOT NULL)
IN DATABASE PRODDB
-- z/OS only
Copyright IBM Corporation 2007, 2013

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

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-5

Student Notebook

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

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Ex

cl

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.

pr

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

2-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Creating tables and adding columns (2 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TABLE NEW_ORDER


LIKE OLD_ORDER;

CREATE TABLE NEW_STAFF


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

Copyright IBM Corporation 2007, 2013

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.

cl

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

Ex

- 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.

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-7

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

2-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Scenario (Part 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

How can we limit the


value of the ordered
quantity for each item
not to exceed 100 ?

We will implement
Check Constraints!

Bill

Susan

Copyright IBM Corporation 2007, 2013

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.

Ex

cl

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.

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-9

Student Notebook

Check constraints The need

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

BUSINESS RULE: No value in the ORDER_QTY column in


the table ORDER_ITEM may exceed 100.

PGM n

PGM n

RULE

DB2

DB2

ENFORCEMENT
APPLICATION

RULE

DATABASE

PGM 1

PGM 2

RULE

RULE

PGM 1

PGM 2

Copyright IBM Corporation 2007, 2013

Figure 2-6. Check constraints - The need

CE1319.4

Notes:

cl

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 manager
will then enforce these rules.
Constraints will be checked during INSERT and UPDATE operations.

Ex

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.

pr

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

2-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

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

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-11

Student Notebook

Check constraints

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TABLE ORDER_ITEM


(ORDER_NO
INTEGER
NOT NULL,
ART_NO
INTEGER
NOT NULL,
ORDER_QTY
SMALLINT NOT NULL
CONSTRAINT order_limit
CHECK(ORDER_QTY <= 100),
CONSTRAINT special_order
CHECK(ART_NO <= 50000 OR
ORDER_QTY <= 10) )

ALTER TABLE ORDER_ITEM


DROP CONSTRAINT special_order

ALTER TABLE ORDER_ITEM


ADD CONSTRAINT special_order
CHECK(ART_NO < 50000 OR
ORDER_QTY <= 10)
Copyright IBM Corporation 2007, 2013

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.

cl

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.

Ex

Constraints can include the basic WHERE clause constructs:


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

pr

Constraints can NOT include:


-

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

2-12 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-13

Student Notebook

Scenario (Part 3)

We will implement
Referential Integrity!

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

How can we avoid


order_items for nonexisting orders?

Bill

Susan

Copyright IBM Corporation 2007, 2013

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?

Ex

cl

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.

pr

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.

2-14 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Susan: OK, no problem.

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-15

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Create index and primary key

OR

DE R

: 01
_ NO

910

CREATE UNIQUE INDEX ORDER_IND


ON ORDER (ORDER_NO)
ALTER TABLE ORDER
ADD PRIMARY KEY (ORDER_NO)
Copyright IBM Corporation 2007, 2013

Figure 2-9. Create index and primary key

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.

2-16 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-17

Student Notebook

Referential integrity (Part 1)


ORDER_NO

ORDER_DATE

ORDER_QTY

ORDER_REF

ART_NO

40000

22444

42500

STOCK_QTY

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

22333

ART_NAME

ORDER

ARTICLE

Parent
Table

Parent
Table

35555

75500

Primary Key

Primary Key

Foreign Key

ORDER_ITEM

Foreign Key

ORDER_NO

ART_NO

ORDER_QTY

22333

40000

22333

42500

35555

40000

Dependent
Table

Primary Key

Copyright IBM Corporation 2007, 2013

Figure 2-10. Referential integrity (Part 1)

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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
2-18 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-19

Student Notebook

Referential integrity Keys

A PRIMARY KEY value must be UNIQUE and cannot be


NULL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

By definition, a FOREIGN KEY matches a PRIMARY KEY


or 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

Copyright IBM Corporation 2007, 2013

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.

cl

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

Ex

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.

pr

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.

2-20 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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


2. Update the FKs to match this new PK value.
3. Delete the row with the original PK value.

pr

Ex

cl

Note

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-21

Student Notebook

Referential integrity DELETE rules

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Problem:
When deleting a primary/unique key, what should be done to
rows 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

Copyright IBM Corporation 2007, 2013

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.

pr

Ex

cl

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.

2-22 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

ALTER TABLE Adding referential constraints

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

ALTER TABLE ORDER


ADD PRIMARY KEY (ORDER_NO)
ALTER TABLE ARTICLE
ADD PRIMARY KEY (ART_NO)

ALTER TABLE ORDER_ITEM


ADD PRIMARY KEY (ORDER_NO, ART_NO)

ALTER TABLE ORDER_ITEM


ADD CONSTRAINT IN_ORDER
FOREIGN KEY(ORDER_NO)
REFERENCES ORDER
ON DELETE CASCADE
ALTER TABLE ORDER_ITEM
ADD CONSTRAINT OF_ARTICLE
FOREIGN KEY(ART_NO)
REFERENCES ARTICLE
ON DELETE RESTRICT

Copyright IBM Corporation 2007, 2013

Figure 2-13. ALTER TABLE - Adding referential constraints

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-23

Student Notebook

Referential integrity (Part 2)


ORDER_NO ORDER_DATE ORDER_QTY ORDER_REF

22333
22444

22333

35555

22444

Primary Key

Foreign Key

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

ORDER

Parent and
Dependent
Table

Delete rule for self-referencing constraint can only be


CASCADE or NO ACTION

ALTER TABLE ORDER


ADD FOREIGN KEY(ORDER_REF)
REFERENCES ORDER
ON DELETE NO ACTION
Copyright IBM Corporation 2007, 2013

Figure 2-14. Referential integrity (Part 2)

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.

2-24 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

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

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-25

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Information integrity constraints

Referential Integrity
Constraints

a performance option

Copyright IBM Corporation 2007, 2013

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.

cl

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

Ex

It is possible to switch a constraint on and off by modifying the attributes of the


referential constraint.

Constraint attributes:

pr

- 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.

2-26 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

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


optimization.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-27

Student Notebook

Scenario (Part 4)
Can DB2
automatically
create reorder
information?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Sure, we can
use triggers!

Susan

Bill

Copyright IBM Corporation 2007, 2013

Figure 2-16. Scenario (Part 4)

CE1319.4

Notes:

cl

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?

Ex

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?

pr

Susan: Sure, we can use a trigger.

Bill: That is great.

2-28 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Triggers The need

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

BUSINESS RULE: When value in the ORDER_QTY column gets


below 50, then insert reorder information into
table SUP_ORDERS.

BUSINESS RULES:
INSERT, UPDATE,
and DELETE
actions against a
table may require
another action or
set of actions
to occur to meet
the needs of the
business.

PGM n

ENFORCEMENT

DB2

APPLICATION
DATABASE

Copyright IBM Corporation 2007, 2013

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:

cl

INSERT

UPDATE

Ex

DELETE

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-29

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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

2-30 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

CREATE TRIGGER (1 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TRIGGER re_order


AFTER UPDATE OF STOCK_QTY ON ARTICLE
REFERENCING NEW AS N OLD AS O
FOR EACH ROW
MODE DB2SQL
WHEN (N.STOCK_QTY < 50 AND
O.STOCK_QTY >= 50)
INSERT INTO SUP_ORDERS
VALUES(N.ART_NO, N.STOCK_QTY)

Triggered action can contain INSERT, searched


UPDATE or DELETE, and single row SELECT

SQL extensions, such as CASE expressions, can


be exploited
DROP TRIGGER re_order
Copyright IBM Corporation 2007, 2013

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

CE1319.4

Notes:

Triggers can be defined with the CREATE TRIGGER statement.


In the example above:

Ex

cl

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.

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-31

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

2-32 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

CREATE TRIGGER (2 of 2)

Unit of Work

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TRIGGER newproj


AFTER INSERT ON PROJECT
REFERENCING NEW AS N_ROW
FOR EACH ROW MODE DB2SQL
BEGIN 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+1
WHERE EMPNO=N_ROW.RESP_EMP;
END
Copyright IBM Corporation 2007, 2013

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

CE1319.4

Notes:

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

Ex

cl

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.

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-33

Student Notebook

Here is an example of using a transitional table:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TRIGGER LARG_ORD


AFTER INSERT ON INVOICE
REFERENCING NEW_TABLE AS N_TABLE
FOR EACH STATEMENT MODE DB2SQL
SELECT 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.

pr

Ex

cl

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.

2-34 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

MERGE data into tables


EMP_COPY before MERGE:

EMP_UPDATES:

EMPNO
-----000010
000020
000030

EMPNO
-----000030
000050
000060

WORKDEPT
-------A00
B01
C01

LASTNAME
--------------KWAN
GEYER
STERN

WORKDEPT
-------Z99
E01
D11

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

LASTNAME
--------------HAAS
THOMPSON
KWAN

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_COPY after MERGE:
EMPNO
-----000010
000020
000030
000050
000060

LASTNAME
--------------HAAS
THOMPSON
KWAN
GEYER
STERN

WORKDEPT
-------A00
B01
Z99
E01
D11

Copyright IBM Corporation 2007, 2013

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.

cl

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).

Ex

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

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-35

Student Notebook

View considerations
A00 000130
.00
T
23800
LYS
A
INFORMA
N
A
TION CEN
TER
IV.
RVICE D
E
S
39-06-21
R
9
E
1
T
U
D
P
01
C OM
SPIFFY
R
0000
NAGE
HAAS
A
3
M
0
0.00
3825
9.00 5275
C01
KWAN

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

IN G
PLANN
B01

1949-02-21

QU

ANA
T
N
I

00

00

View

10

HAAS

A00

SPIFFY COMPUTER SERVICE DIV.

QUINTANA

C01

INFORMATION CENTER

A View Definition cannot include: ORDER BY


Copyright IBM Corporation 2007, 2013

Figure 2-21. View considerations

CE1319.4

Notes:

cl

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.

Ex

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:

pr

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.

2-36 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-37

Student Notebook

Sample view
EMPLOYEE Table
LASTNAME

WORKDEPT

SALARY

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

000010 HAAS
000030 KWAN
000120 O'CONNELL
000130 QUINTANA
000140 NICHOLLS

A00
C01
A00
C01
C01

52750.00
38250.00

DEPTNO

29250.00
23800.00
28420.00

A00
C01
D01

DEPARTMENT Table

DEPTNAME

MGRNO

SPIFFY COMPUTER SERVICE DIV. 000010


INFORMATION CENTER
000030
DEVELOPMENT CENTER

CREATE VIEW EMP_DEPT (NAME, DEPARTMENT)


AS SELECT LASTNAME, DEPTNAME
FROM EMPLOYEE, DEPARTMENT
WHERE WORKDEPT = DEPTNO
SELECT * FROM EMP_DEPT;
DEPARTMENT

NAME

EMP_DEPT View

HAAS
O'CONNELL
KWAN
NICHOLLS
QUINTANA

SPIFFY COMPUTER SERVICE DIV.


SPIFFY COMPUTER SERVICE DIV.
INFORMATION CENTER
INFORMATION CENTER
INFORMATION CENTER

Copyright IBM Corporation 2007, 2013

Figure 2-22. Sample view

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.

2-38 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-39

Student Notebook

View hierarchy
PROJNAME

DEPTNO

RESPEMP

DEPTNO

AD3100 ADMIN SERVICES

D01

000010

A00

SPIFFY COMPUTER SERVICE DIV.

000010

IF1000 QUERY SERVICES

C01

000030

C01

INFORMATION CENTER

000030

IF2000 USER EDUCATION

C01

000030

D01

DEVELOPMENT CENTER

DEPTNAME

MGRNO

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

PROJNO

PROJECT Table

DEPARTMENT Table

PROJNAME

DEPTNAME

WORKDEPT

SALARY

USER EDUCATION

INFORMATION CENTER

000030

000010 HAAS

A00

52750.00

QUERY SERVICES

INFORMATION CENTER

000030

000030 KWAN

C01

38250.00

ADMIN SERVICES

SPIFFY COMPUTER SERVICE DIV.

000120 O'CONNELL

A00

29250.00

000130 QUINTANA

C01

23800.00

000140 NICHOLLS

C01

28420.00

MGRNO

PROJ_DEPT View

EMPNO

LASTNAME

EMPLOYEE Table

PROJECT

DEPARTMENT

MANAGER

USER EDUCATION

INFORMATION CENTER

KWAN

QUERY SERVICES

INFORMATION CENTER

KWAN

ADMIN SERVICES

SPIFFY COMPUTER SERVICE DIV.

PROJ_DEPT_MGR View

Copyright IBM Corporation 2007, 2013

Figure 2-23. View hierarchy

CE1319.4

Notes:

cl

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.

Ex

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:

pr

CREATE VIEW PROJ_DEPT


AS SELECT PROJNAME, DEPTNAME, MGRNO
FROM PROJECT P INNER JOIN DEPARTMENT D
ON P.DEPTNO = D.DEPTNO

2-40 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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, LASTNAME
FROM PROJ_DEPT LEFT OUTER JOIN EMPLOYEES
ON MGRNO = EMPNO

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-41

Student Notebook

Read-only views
EMPLOYEE Table
EMPNO

HAAS
KWAN
O'CONNELL
QUINTANA
NICHOLLS

WORKDEPT

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00
23800.00
28420.00

DEPARTMENT Table

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130
000140

LASTNAME

EMP_DEPT View

DEPTNO

A00
C01
D01

DEPTNAME

MGRNO

SPIFFY COMPUTER SERVICE DIV. 000010


INFORMATION CENTER
000030
DEVELOPMENT CENTER

NAME

DEPARTMENT

HAAS
O'CONNELL
KWAN
NICHOLLS
QUINTANA

SPIFFY COMPUTER SERVICE DIV.


SPIFFY COMPUTER SERVICE DIV.
INFORMATION CENTER
INFORMATION CENTER
INFORMATION CENTER

UPDATE EMP_DEPT
SET DEPARTMENT = 'DEVELOPMENT CENTER'
WHERE NAME = 'KWAN'

Copyright IBM Corporation 2007, 2013

Figure 2-24. Read-only views

CE1319.4

Notes:

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

cl

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

Ex

- 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.

pr

- 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.

2-42 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

- 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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-43

Student Notebook

Changing data through views


Table

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

View

UPDATE

INSERT

DELETE

Copyright IBM Corporation 2007, 2013

Figure 2-25. Changing data through views

CE1319.4

Notes:

cl

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:

Ex

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.

pr

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.
2-44 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-45

Student Notebook

INSTEAD OF triggers (1 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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!

Copyright IBM Corporation 2007, 2013

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.

Ex

cl

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.

pr

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.

2-46 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

INSTEAD OF triggers (2 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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 V1


REFERENCING 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!

Copyright IBM Corporation 2007, 2013

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

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-47

Student Notebook

CHECK OPTION
EMPNO

LASTNAME

HAAS
KWAN
O'CONNELL
QUINTANA

WORKDEPT

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00
23800.00

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130

EMPLOYEE Table

000140 NICHOLLS

CREATE VIEW SALARIES_1


AS
SELECT EMPNO, LASTNAME, SALARY
FROM EMPLOYEE
WHERE SALARY < 40000

UPDATE SALARIES_1
SET SALARY = 42000
WHERE EMPNO = '000030'

28420.00

CREATE VIEW SALARIES_2


AS
SELECT EMPNO, LASTNAME, SALARY
FROM EMPLOYEE
WHERE SALARY < 40000
WITH CHECK OPTION
UPDATE SALARIES_2
SET SALARY = 42000
WHERE EMPNO = '000030'

WORKS!

FAILS!

Copyright IBM Corporation 2007, 2013

Figure 2-28. CHECK OPTION

CE1319.4

Notes:

cl

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.

pr

Ex

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.

2-48 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-49

Student Notebook

View Merge

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE VIEW EMP_SALARY_DEPT (NAME, SALARY, DEPARTMENT)


AS SELECT LASTNAME, SALARY, DEPTNAME
FROM EMPLOYEE INNER JOIN DEPARTMENT
ON WORKDEPT = DEPTNO
WHERE DEPTNO = 'C01'
SELECT NAME, SALARY
FROM EMP_SALARY_DEPT
WHERE SALARY > 25000
FUNCTIONS AS

SELECT LASTNAME AS NAME, SALARY


FROM EMPLOYEE INNER JOIN DEPARTMENT
ON WORKDEPT = DEPTNO
WHERE DEPTNO = 'C01' AND
SALARY > 25000
Copyright IBM Corporation 2007, 2013

Figure 2-29. View Merge

CE1319.4

Notes:

cl

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:

Ex

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

pr

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.

2-50 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

View materialization
EMPNO

HAAS
KWAN
O'CONNELL
QUINTANA

WORKDEPT

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00
23800.00

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130

LASTNAME

EMPLOYEE Table

000140 NICHOLLS

28420.00

CREATE VIEW DEPT_SALARIES (DEPARTMENT, TOTAL_SALARY)


AS SELECT WORKDEPT, SUM(SALARY)
FROM EMPLOYEE
GROUP BY WORKDEPT

DEPT_SALARIES
View

DEPARTMENT

TOTAL_SALARY

A00

82000.00

C01

90470.00

STEP 1

SELECT MAX(TOTAL_SALARY) AS MAXIMUM_SALARY


FROM DEPT_SALARIES
MAXIMUM_SALARY

90470.0
0

STEP 2

Copyright IBM Corporation 2007, 2013

Figure 2-30. View materialization

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-51

Student Notebook

Checkpoint

2.
3.

4.

5.

Assume that you have created a new table. How could you make
sure that you will not have any duplicate rows in the table?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.

T/F. Check constraints can be used to enforce business rules


requiring evaluation of one or more columns in a table row.

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?
Assume that you need to insert a record in a table any time an
employee receives a salary increase of more than 10 percent. How
could you ensure that this happens without writing any application
code?
T/F. Views can be based on one or more tables, but not on other
views.
Copyright IBM Corporation 2007, 2013

Figure 2-31. Checkpoint

CE1319.4

Notes:

Write your answers here:


1.

cl

2.

Ex

3.
4.

pr

5.

2-52 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit summary
Having completed this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2013

Figure 2-32. Unit summary

CE1319.4

pr

Ex

cl

Notes:

Copyright IBM Corp. 2007, 2013

Unit 2. Create Objects

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-53

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

2-54 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 3. Join
What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

Checkpoints

Copyright IBM Corp. 2007, 2013


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

Unit 3. Join

3-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Use inner and outer joins to retrieve data from more than
one table
Use joins of tables to themselves

Copyright IBM Corporation 2007, 2012

Figure 3-1. Unit objectives

CE1319.4

Notes:

cl

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.

pr

Ex

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.

3-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Sample tables for unit


EMPNO

LASTNAME

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00
23800.00
28420.00
25400.00

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010 HAAS
000030 KWAN
000120 O'CONNELL
000130 QUINTANA
000140 NICHOLLS
000400 WILSON

WORKDEPT

EMPLOYEE

DEPTNO DEPTNAME

DEPARTMENT

A00

SPIFFY COMPUTER SERVICE DIV.

000010

C01

INFORMATION CENTER

000030

D01

DEVELOPMENT CENTER

PROJNO PROJNAME

PROJECT

MGRNO

AD3100 ADMIN SERVICES


IF1000 QUERY SERVICES
IF2000 USER EDUCATION

DEPTNO RESPEMP

D01
C01
C01

000010
000030
000030

Copyright IBM Corporation 2007, 2012

Figure 3-2. Sample tables for unit

CE1319.4

Notes:

pr

Ex

cl

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


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

Unit 3. Join

3-3

Student Notebook

The tables are derived from the DB2 sample tables by:

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

3-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Join (original syntax)


EMPLOYEE.
WORKDEPT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DEPARTMENT.
DEPTNO

NULL

A00
C01

D01

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAME


FROM EMPLOYEE, DEPARTMENT
WHERE WORKDEPT = DEPTNO
EMPNO

LASTNAME

000010
000120
000030
000140

HAAS
O'CONNELL
KWAN
NICHOLLS

000130 QUINTANA

RESULT

DEPTNO DEPTNAME

A00
A00
C01
C01

SPIFFY COMPUTER SERVICE DIV.

C01

INFORMATION CENTER

SPIFFY COMPUTER SERVICE DIV.


INFORMATION CENTER
INFORMATION CENTER

Copyright IBM Corporation 2007, 2012

Figure 3-3. Join (original syntax)

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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


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

Unit 3. Join

3-5

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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

3-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

INNER JOIN (newer syntax)


EMPLOYEE.
WORKDEPT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DEPARTMENT.
DEPTNO

NULL

A00
C01

D01

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAME


FROM EMPLOYEE INNER JOIN DEPARTMENT
ON WORKDEPT = DEPTNO
EMPNO LASTNAME

DEPTNO DEPTNAME

000010 HAAS

A00

SPIFFY COMPUTER SERVICE DIV.

000120 O'CONNELL

A00

SPIFFY COMPUTER SERVICE DIV.

000030 KWAN

C01

INFORMATION CENTER

000140 NICHOLLS

C01

INFORMATION CENTER

000130 QUINTANA

C01

INFORMATION CENTER

RESULT

Copyright IBM Corporation 2007, 2012

Figure 3-4. INNER JOIN (newer syntax)

CE1319.4

Notes:

cl

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.

pr

Ex

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


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

Unit 3. Join

3-7

Student Notebook

LEFT OUTER JOIN


EMPLOYEE.
WORKDEPT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DEPARTMENT.
DEPTNO

NULL

A00
C01

D01

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAME


FROM EMPLOYEE
LEFT OUTER JOIN DEPARTMENT
ON WORKDEPT = DEPTNO
EMPNO

LASTNAME

DEPTNO DEPTNAME

000010 HAAS

A00

SPIFFY COMPUTER SERVICE DIV.

000030 KWAN

C01

INFORMATION CENTER

000120 O'CONNELL

A00

SPIFFY COMPUTER SERVICE DIV.

000130 QUINTANA

C01

INFORMATION CENTER

000140 NICHOLLS

C01

INFORMATION CENTER

000400 WILSON

RESULT

Copyright IBM Corporation 2007, 2012

Figure 3-5. LEFT OUTER JOIN

CE1319.4

Notes:

cl

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).

pr

Ex

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.

3-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

RIGHT OUTER JOIN


EMPLOYEE.
WORKDEPT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DEPARTMENT.
DEPTNO

A00
C01

NULL

D01

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAME


FROM EMPLOYEE
RIGHT OUTER JOIN DEPARTMENT
ON WORKDEPT = DEPTNO
EMPNO

LASTNAME

DEPTNO DEPTNAME

000010 HAAS

A00

SPIFFY COMPUTER SERVICE DIV.

000120 O'CONNELL

A00

SPIFFY COMPUTER SERVICE DIV.

000030 KWAN

C01

INFORMATION CENTER

000130 QUINTANA

C01

INFORMATION CENTER

000140 NICHOLLS

C01

INFORMATION CENTER

D01

DEVELOPMENT CENTER

RESULT

Copyright IBM Corporation 2007, 2012

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.

cl

There are three types of Outer Joins:


RIGHT OUTER JOIN

Ex

LEFT OUTER JOIN


FULL OUTER JOIN

pr

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


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

Unit 3. Join

3-9

Student Notebook

FULL OUTER JOIN


EMPLOYEE.
WORKDEPT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DEPARTMENT.
DEPTNO
A00
C01

NULL

D01

SELECT EMPNO, LASTNAME, DEPTNO, DEPTNAME


FROM EMPLOYEE
FULL OUTER JOIN DEPARTMENT
ON WORKDEPT = DEPTNO
EMPNO LASTNAM
E
000120 O'CONNELL

DEPTNO DEPTNAME
SPIFFY COMPUTER SERVICE DIV.
A00

000010 HAAS

A00

SPIFFY COMPUTER SERVICE DIV.

000030 KWAN

C01

INFORMATION CENTER

000130 QUINTANA

C01

INFORMATION CENTER

000140 NICHOLLS

C01

INFORMATION CENTER

D01

DEVELOPMENT CENTER

000400 WILSON

RESULT

Copyright IBM Corporation 2007, 2012

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.

pr

Ex

cl

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.

3-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Anti JOIN (1 of 3)
EMPLOYEE.
WORKDEPT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

NULL

DEPARTMENT.
DEPTNO

D01

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 ;

RESULT

DEPTNO

DEPTNAME

D01

DEVELOPMENT CENTER

Copyright IBM Corporation 2007, 2012

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

CE1319.4

Notes:

Ex

cl

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.

pr

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 Corp. 2007, 2013
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Unit 3. Join

3-11

Student Notebook

Anti JOIN (2 of 3)
EMPLOYEE.
WORKDEPT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

NULL

DEPARTMENT.
DEPTNO

D01

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 ;

RESULT

DEPTNO

DEPTNAME

D01

DEVELOPMENT CENTER

Copyright IBM Corporation 2007, 2012

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

CE1319.4

Notes:

pr

Ex

cl

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.

3-12 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Anti JOIN (3 of 3)
EMPLOYEE.
WORKDEPT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

NULL

DEPARTMENT.
DEPTNO

D01

METHOD 3

SELECT DEPTNO, DEPTNAME


FROM DEPARTMENT

WHERE DEPTNO NOT IN

(SELECT WORKDEPT
FROM EMPLOYEE) ;

RESULT

DEPTNO

DEPTNAME

D01

DEVELOPMENT CENTER

Copyright IBM Corporation 2007, 2012

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

CE1319.4

Notes:

pr

Ex

cl

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


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

Unit 3. Join

3-13

Student Notebook

Joins of more than two tables


DEPARTMENT D
Inner Join on
P.DEPTNO = D.DEPTNO

RESULT

NULL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Left Outer Join


on MGRNO=EMPNO

D01
C01

000030

PROJECT P

000120
000130
000140
000400

EMPLOYEE

SELECT P.PROJNO,P.PROJNAME, D.DEPTNO, D.MGRNO,


E.LASTNAME AS MGRNAME
FROM PROJECT P
INNER JOIN DEPARTMENT D ON P.DEPTNO = D.DEPTNO
LEFT OUTER JOIN EMPLOYEE E ON D.MGRNO = E.EMPNO
RESULT

PROJNO PROJNAME

DEPTNO

MGRNO MGRNAME

IF2000 USER EDUCATION

C01

000030 KWAN

IF1000 QUERY SERVICES

C01

000030 KWAN

AD3100 ADMIN SERVICES

D01

Left and right joins are not symmetrical.

Copyright IBM Corporation 2007, 2012

Figure 3-11. Joins of more than two tables

CE1319.4

Notes:

cl

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:

Ex

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.

pr

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.

3-14 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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:

Ex

cl

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;

pr

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
Copyright IBM Corp. 2007, 2013
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Unit 3. Join

3-15

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

3-16 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Joins and local predicates (1 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT EMPNO, LASTNAME, SALARY,


DEPTNO, DEPTNAME
FROM EMPLOYEE
FULL OUTER JOIN DEPARTMENT
ON WORKDEPT = DEPTNO

WHERE SALARY > 30000 AND


DEPTNAME LIKE '%CENTER%'

EMPNO

LASTNAME

WORKDEPT

SALARY

DEPTNO

DEPTNAME

MGRNO

000120

O'CONNELL

A00

29250.00

A00

SPIFFY COMPUTER SERVICE DIV.

000010

000010

HAAS

A00

52750.00

A00

SPIFFY COMPUTER SERVICE DIV.

000010

000030

KWAN

C01

38250.00

C01

INFORMATION CENTER

000030

000130

QUINTANA

C01

23800.00

C01

INFORMATION CENTER

000030

000140

NICHOLLS

C01

28420.00

C01

INFORMATION CENTER

000030

D01

DEVELOPMENT CENTER

WILSON

25400.00

000400

INTERMEDIATE RESULT TABLE

Copyright IBM Corporation 2007, 2012

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:

cl

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.

Ex

Local predicates are those specified via the WHERE clause.

pr

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


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

Unit 3. Join

3-17

Student Notebook

Joins and local predicates (2 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT EMPNO, LASTNAME, SALARY,


DEPTNO, DEPTNAME
FROM EMPLOYEE
FULL OUTER JOIN DEPARTMENT
ON WORKDEPT = DEPTNO

EMPNO

LASTNAME

WORKDEPT

SALARY

DEPTNO

DEPTNAME

MGRNO

000120

O'CONNELL

A00

29250.00

A00

SPIFFY COMPUTER SERVICE DIV.

000010

000010

HAAS

A00

52750.00

A00

SPIFFY COMPUTER SERVICE DIV.

000010

INFORMATION CENTER

000030

000030

KWAN

C01

38250.00

C01

000130

QUINTANA

C01

23800.00

C01

INFORMATION CENTER

000030

000140

NICHOLLS

C01

28420.00

C01

INFORMATION CENTER

000030

DEVELOPMENT CENTER

000400

D01

WILSON

25400.00

WHERE SALARY > 30000


AND
DEPTNAME LIKE '%CENTER%'

EMPNO LASTNAME
000030 KWAN

SALARY
38250.00

DEPTNO DEPTNAME
INFORMATION CENTER
C01

Copyright IBM Corporation 2007, 2012

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

CE1319.4

Notes:

cl

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.

pr

Ex

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
3-18 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

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

Copyright IBM Corp. 2007, 2013


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

Unit 3. Join

3-19

Student Notebook

Joining a table with itself (1 of 3)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Display the name of department B01 and the name of the


...
department it reports to
DEPARTMENT

DEPTNO DEPTNAME

MGRNO

ADMRDEPT

A00

SPIFFY COMPUTER SERVICE DIV.

000010

A00

B01

PLANNING

000020

A00

Copyright IBM Corporation 2007, 2012

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

CE1319.4

Notes:

pr

Ex

cl

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.

3-20 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Joining a table with itself (2 of 3)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Display the name of department B01 and the name of the


...
department it reports to
DEPARTMENT (DEP)

DEPTNO

DEPTNAME

MGRNO

ADMRDEPT

A00

SPIFFY COMPUTING SERVICE DIV.

000010

A00

B01

PLANNING

000020

A00

DEPTNO

DEPTNAME

MGRNO

ADMRDEPT

A00

SPIFFY COMPUTING SERVICE DIV.

000010

A00

B01

PLANNING

000020

A00

...

DEPARTMENT (SUP)

Copyright IBM Corporation 2007, 2012

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

CE1319.4

Notes:

cl

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.

Ex

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).

pr

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


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

Unit 3. Join

3-21

Student Notebook

Joining a table with itself (3 of 3)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Display the name of department B01 and the name of the


...
department it reports to
SELECT

FROM
WHERE

AND

DEP.DEPTNAME,
SUP.DEPTNAME AS SUP_DEPTNAME
DEPARTMENT DEP, DEPARTMENT SUP
DEP.ADMRDEPT = SUP.DEPTNO
DEP.DEPTNO = B01

DEPTNAME

SUP_DEPTNAME

PLANNING

SPIFFY COMPUTER SERVICE DIV.

Copyright IBM Corporation 2007, 2012

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.

pr

Ex

cl

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.

3-22 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Joining a table with itself


Another example (1 of 2)
1. Retrieve employee's row from EMPLOYEE (E)
EMPNO

...

LASTNAME

WORKDEPT

...

BIRTHDATE

...

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Which
employees
are older
than their
manager?

000100

SPENSER

E21

1956-12-18

000330

LEE

E21

1941-07-18

2. Obtain department number from DEPARTMENT (D)


DEPTNO

DEPTNAME

.
.

E21

MGRNO

ADMRDEPT

.
.

.
.

000100

E21

.
.

SOFTWARE SUPPORT

3. Retrieve row for manager from EMPLOYEE (M)


EMPNO

...

LASTNAME

WORKDEPT

...

BIRTHDATE

000100

SPENSER

E21

1956-12-18

000330

LEE

E21

1941-07-18

...

Copyright IBM Corporation 2007, 2012

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.

pr

Ex

cl

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


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

Unit 3. Join

3-23

Student Notebook

Joining a table with itself


Another example (2 of 2)
SELECT

E.EMPNO, E.LASTNAME,
E.BIRTHDATE,
M.BIRTHDATE AS M_BIRTHDATE,
M.EMPNO AS MGRNO
EMPLOYEE E,
DEPARTMENT D,
EMPLOYEE M
E.WORKDEPT = D.DEPTNO
D.MGRNO = M.EMPNO
E.BIRTHDATE < M.BIRTHDATE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Which employees
are older than
their manager?

FROM

WHERE
AND
AND

EMPNO

LASTNAME

BIRTHDATE

M_BIRTHDATE

MGRNO

000110
000130
000200
000230
000250
000260
000280
000300
000310
000320

LUCCHESI
QUINTANA
BROWN
JEFFERSON
SMITH
JOHNSON
SCHNEIDER
SMITH
SETRIGHT
MEHTA

1929-11-05
1925-09-15
1941-05-29
1935-05-30
1939-11-12
1936-10-05
1936-03-28
1936-10-27
1931-04-21
1932-08-11

1933-08-14
1941-05-11
1945-07-07
1953-05-26
1953-05-26
1953-05-26
1941-05-15
1941-05-15
1941-05-15
1956-12-18

000010
000030
000060
000070
000070
000070
000090
000090
000090
000100

Copyright IBM Corporation 2007, 2012

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

CE1319.4

Notes:

pr

Ex

cl

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 employees
number is labeled EMPNO and the managers number is labeled MGRNO. The managers
birth date is labeled as M_BIRTHDATE to distinguish it from the birth date of the employee
E_BIRTHDATE.

3-24 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

UNION and UNION ALL


EDLEVEL
16
18

SELECT
FROM
WHERE

LASTNAME, EDLEVEL
EMPLOYEE
JOB = 'ANALYST'

SELECT
FROM
WHERE

LASTNAME, EDLEVEL
EMPLOYEE
EDLEVEL = 18

LASTNAME
HAAS
THOMPSON
NICHOLLS
LUTZ

EDLEVEL
18
18
18
18

SELECT
FROM
WHERE
UNION ALL
SELECT
FROM
WHERE

LASTNAME, EDLEVEL
EMPLOYEE
JOB = 'ANALYST'

LASTNAME
QUINTANA
NICHOLLS
HAAS
THOMPSON
NICHOLLS
LUTZ

EDLEVEL
16
18
18
18
18
18

SELECT
FROM
WHERE
UNION
SELECT
FROM
WHERE

LASTNAME, EDLEVEL
EMPLOYEE
JOB = 'ANALYST'

LASTNAME
HAAS
LUTZ
NICHOLLS
QUINTANA
THOMPSON

EDLEVEL
18
18
18
16
18

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

LASTNAME
QUINTANA
NICHOLLS

LASTNAME, EDLEVEL
EMPLOYEE
EDLEVEL = 18

LASTNAME, EDLEVEL
EMPLOYEE
EDLEVEL = 18

Copyright IBM Corporation 2007, 2012

Figure 3-19. UNION and UNION ALL

CE1319.4

Notes:

Here is a brief review of the UNION set operator.

pr

Ex

cl

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


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

Unit 3. Join

3-25

Student Notebook

EXCEPT and INTERSECT


LASTNAME, EDLEVEL
EMPLOYEE
JOB = 'ANALYST'

LASTNAME

EDLEVEL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT
FROM
WHERE
EXCEPT
SELECT
FROM
WHERE

QUINTANA

16

LASTNAME

EDLEVEL

NICHOLLS

18

LASTNAME, EDLEVEL
EMPLOYEE
EDLEVEL = 18

SELECT
LASTNAME, EDLEVEL
FROM
EMPLOYEE
WHERE
JOB = 'ANALYST'
INTERSECT
SELECT
LASTNAME, EDLEVEL
FROM
EMPLOYEE
WHERE
EDLEVEL = 18

Copyright IBM Corporation 2007, 2012

Figure 3-20. EXCEPT and INTERSECT

CE1319.4

Notes:

Here are two other keywords that can be used for merging data, but work differently.

cl

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.

Ex

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.

pr

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, EDLEVEL
FROM EMPLOYEE
WHERE JOB = ANALYST
AND NOT EDLEVEL = 18 ;

3-26 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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:

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT LASTNAME, EDLEVEL


FROM EMPLOYEE
WHERE JOB = ANALYST
AND EDLEVEL = 18;

Copyright IBM Corp. 2007, 2013


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

Unit 3. Join

3-27

Student Notebook

Checkpoint

2.
3.

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?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.

T/F. If you specify two or more tables in the FROM clause,


but omit all join conditions, the result set will be empty.

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?

Copyright IBM Corporation 2007, 2012

Figure 3-21. Checkpoint

CE1319.4

Notes:

Enter your answers here:


1.

cl

2.

pr

Ex

3.

3-28 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit summary
Having completed this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Use inner and outer joins to retrieve data from more than
one table
Use joins of tables to themselves

Copyright IBM Corporation 2007, 2012

Figure 3-22. Unit summary

CE1319.4

pr

Ex

cl

Notes:

Copyright IBM Corp. 2007, 2013


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

Unit 3. Join

3-29

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

3-30 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 4. CASE, CAST, Summary (Materialized


Query) Tables and Temporary Tables
What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

Checkpoint

Copyright IBM Corp. 2007, 2013

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2013

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.

pr

Ex

cl

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.

4-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

CASE expressions in SELECT


EMPNO

WORKDEPT

LASTNAME

SALARY

HIREDATE

A00

000030 KWAN

C01

52750.00 1965-01-01
38250.00 1975-04-05

000120 O'CONNELL

A00

29250.00 1963-12-05

000130 QUINTANA

C01

23800.00 1971-07-28

000140 NICHOLLS

C01

28420.00 1976-12-15

EMPLOYEE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010 HAAS

SELECT EMPNO,
CASE
WHEN
WHEN
ELSE
END AS

LASTNAME,

SALARY < 25000 THEN 'LOW'


SALARY >= 25000 AND SALARY < 40000 THEN 'AVERAGE'
'HIGH'
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

FROM EMPLOYEE

SALARY_CLASS

AREA_TYPE

000010 HAAS

HIGH

000030 KWAN

AVERAGE

ADMINISTRATION
CUSTOMER SERVICE

EMPNO

LASTNAME

000120 O'CONNELL AVERAGE

ADMINISTRATION

000130 QUINTANA
000140 NICHOLLS

CUSTOMER SERVICE
CUSTOMER SERVICE

LOW
AVERAGE

Copyright IBM Corporation 2007, 2013

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.

cl

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.

Ex

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.

pr

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

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-3

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

4-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

CASE expression in WHERE clause


WORKDEPT

SALARY

COMM

000010 HAAS

A00

52750.00

4220.00

000030 KWAN

C01

38250.00

3060.00

LASTNAME

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

000120 O'CONNELL

A00

29250.00

2340.00

000130 QUINTANA

C01

23800.00

1904.00

000140 NICHOLLS

C01

28420.00

2274.00

EMPLOYEE

SELECT EMPNO,LASTNAME,SALARY+COMM AS TOT_SAL


FROM EMPLOYEE
WHERE (CASE WHEN SALARY = 0 THEN NULL
ELSE COMM / SALARY
END ) > 0.08

EMPNO

LASTNAME

000140 NICHOLLS

TOT_SAL

31194.00

Copyright IBM Corporation 2007, 2013

Figure 4-3. CASE expression in WHERE clause

CE1319.4

Notes:

cl

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).

Ex

Of course, an alternate approach to fulfilling this particular business requirement is to code


the SELECT with two local predicates:

pr

SELECT EMPNO, LASTNAME, SALARY+COMM AS TOT_SAL


FROM EMPLOYEE
WHERE SALARY > 0
AND COMM/SALARY > 0.08 ;

Copyright IBM Corp. 2007, 2013

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-5

Student Notebook

CASE expression in a function


WORKDEPT

SALARY

COMM

000010 HAAS

A00

52750.00

4220.00

000030 KWAN

C01

0.00

3060.00

000120 O'CONNELL

A00

29250.00

2340.00

000130 QUINTANA

C01

23800.00

1904.00

000140 NICHOLLS

C01

28420.00

2274.00

LASTNAME

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

EMPLOYEE

SELECT MIN ( CASE


WHEN SALARY = 0 THEN NULL
ELSE SALARY END) AS MIN_SAL
FROM EMPLOYEE

MIN_SAL

23800.00

Copyright IBM Corporation 2007, 2013

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.

cl

The same results would also be returned by the following query:

Ex

SELECT MIN(SALARY)
FROM EMPLOYEE
WHERE SALARY <> 0 ;

pr

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.

4-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Nested CASE expression


WORKDEPT

SALARY

COMM

000010 HAAS

A00

52750.00

4220.00

000030 KWAN

C01

0.00

3060.00

000120 O'CONNELL

A00

29250.00

2340.00

000130 QUINTANA

C01

23800.00

1904.00

000140 NICHOLLS

C01

28420.00

2274.00

LASTNAME

EMPLOYEE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

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 SALARY
END AS NEW_SAL
FROM EMPLOYEE
WHERE SALARY < 50000

EMPNO

NEW_SAL

000030
000130

50.00
29350.00
24000.00

000140

28470.00

000120

Copyright IBM Corporation 2007, 2013

Figure 4-5. Nested CASE expression

CE1319.4

Notes:

pr

Ex

cl

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

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-7

Student Notebook

CAST specifications
WORKDEPT

SALARY

COMM

000010 HAAS

A00

52750.00

4220.00

000030 KWAN

C01

38250.00

3060.00

000120 O'CONNELL

A00

29250.00

2340.00

000130 QUINTANA

C01

23800.00

1904.00

000140 NICHOLLS

C01

28420.00

2274.00

LASTNAME

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

EMPLOYEE

SELECT EMPNO,COMM/SALARY AS COL2,


CAST(COMM/SALARY AS DEC(9,2))
AS COL3
FROM EMPLOYEE
WHERE EMPNO='000140'

EMPNO

000140

COL2

0.0976073187895847994370

COL3

0.09

Copyright IBM Corporation 2007, 2013

Figure 4-6. CAST specifications

CE1319.4

Notes:

cl

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.

pr

Ex

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.

4-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-9

Student Notebook

Summary tables and


Materialized Query Tables (MQTs)
Materialized views (Materialized Query Table, MQT)

Aggregate Aware Optimization

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Contains pre-computed results

Queries can reuse Summary Tables without accessing base


tables
Improved performance

Copyright IBM Corporation 2007, 2013

Figure 4-7. Summary tables and Materialized Query Tables (MQTs)

CE1319.4

Notes:

cl

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. MQTs 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.

Ex

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.

pr

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

4-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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 MQTs 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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-11

Student Notebook

Summary tables or MQTs Example

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TABLE DEPT_GROUP


AS ( SELECT WORKDEPT,
SUM(SALARY) AS SALARY,
SUM(BONUS) AS BONUS
FROM EMPLOYEE
GROUP BY WORKDEPT )
DATA INITIALLY DEFERRED
REFRESH DEFERRED

DATA INITIALLY DEFERRED

REFRESH DEFERRED

Data is not inserted into table as part of the CREATE TABLE statement

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

Copyright IBM Corporation 2007, 2013

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.

Ex

cl

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).

pr

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)
4-12 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

MAINTAINED BY USER allows INSERT/UPDATE/DELETE on the MQT but only allows


REFRESH DEFERRED.

pr

Ex

cl

To exploit Summary table and ensure that we are not using old data from the Summary
table: SET CURRENT REFRESH AGE

Copyright IBM Corp. 2007, 2013

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-13

Student Notebook

Materialized Query Tables Considerations

If optimizer decides to use a summary table, access to base


tables is eliminated.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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 if


that 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.

Copyright IBM Corporation 2007, 2013

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.

cl

Ideally, each Summary Table should serve many queries.

Ex

It is recommended that the DBA create indexes on summary tables and update statistics
on summary tables.

pr

Consider physical storage requirements in DASD-constrained environments.

4-14 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Temporary tables - Usage


CREATE

Intermediate Results

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

TEMPORARY

Conversion to/from other relational data

TABLE

Accessing non-relational data

Returning result set multiple times

Work table

DECLARE TEMPORARY TABLE

Copyright IBM Corporation 2007, 2013

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.

Ex

cl

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.

pr

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

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-15

Student Notebook

the course of that program, the temporary table storing the non-relational data can be
accessed by SQL.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

4-16 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Temporary tables Types in DB2 z/OS


CREATED
temp tables

DECLARED
temp tables

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Feature/Function
Defined in
DB2 catalog?

YES

NO

Shareable by
other applications

YES

NO

NO

YES

NO

YES

Indexes

Check Constraints

Copyright IBM Corporation 2007, 2013

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.

Ex

cl

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.

pr

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

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-17

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

4-18 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Temporary tables
DECLARE GLOBAL TEMPORARY TABLE T1

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2013

Figure 4-12. Temporary tables

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-19

Student Notebook

Checkpoint

2.

T/F. CASE can be coded in both the SELECT-list and in the


WHERE clause.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.

T/F. CAST specifications can be used to cast the value


NULL to a parameter.

Copyright IBM Corporation 2007, 2013

Figure 4-13. Checkpoint

CE1319.4

Notes:

Write your answers here:


1.

pr

Ex

cl

2.

4-20 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit summary
Having completed this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2013

Figure 4-14. Unit summary

CE1319.4

pr

Ex

cl

Notes:

Copyright IBM Corp. 2007, 2013

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-21

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

4-22 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 5. Using Subqueries


What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

Choose the proper type of subquery to use in each case

Copyright IBM Corp. 2007, 2013

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Code subqueries using the ALL, ANY/SOME, and EXISTS


keywords
Code correlated subqueries

Choose the proper type of subquery to use in each case

Copyright IBM Corporation 2007, 2012

Figure 5-1. Unit objectives

CE1319.4

Notes:

pr

Ex

cl

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.

5-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Subquery with basic predicate


EMPNO

LASTNAME

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00

Who has the


highest yearly
salary?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010 HAAS
000030 KWAN
000120 O'CONNELL
000130 QUINTANA

WORKDEPT

000140 NICHOLLS

23800.00
28420.00

EMPLOYEE

SELECT EMPNO, LASTNAME, SALARY


FROM EMPLOYEE
WHERE SALARY =
(SELECT MAX(SALARY)
FROM EMPLOYEE);
EMPNO

000010

LASTNAME

HAAS

MAX(SALARY)

52750.0
0

SALARY

52750.00

Copyright IBM Corporation 2007, 2012

Figure 5-2. Subquery with basic predicate

CE1319.4

Notes:

cl

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:

Ex

Step 1 - The subquery will determine the highest salary and it will be compared with the
salaries of all employees.

pr

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-3

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Note

5-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Subquery with IN predicate


WORKDEPT

SALARY

000010 HAAS

A00

52750.00

000030 KWAN

38250.00

000120 O'CONNELL

C01
A00

000130 QUINTANA

C01

23800.00

000140 NICHOLLS

C01

28420.00

LASTNAME

Which employees are


responsible for projects
within their department?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

29250.00

EMPLOYEE
PROJNO

PROJNAME

AD3100 ADMIN SERVICES


IF1000 QUERY SERVICES

IF2000 USER EDUCATION

DEPTNO

RESPEMP

D01

000010

C01
C01

000030
000030

PROJECT

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE (EMPNO,WORKDEPT) IN
(SELECT RESPEMP, DEPTNO FROM PROJECT);

RESPEMP DEPTNO

000010 D01
000030 C01

Copyright IBM Corporation 2007, 2012

Figure 5-3. Subquery with IN predicate

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-5

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Check the reference manuals for your environment and release to determine if the syntax
is supported.

5-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Subquery with NOT IN predicate


EMPNO

LASTNAME

WORKDEPT

SALARY

A00

52750.00

Which employees
are not responsible for
projects?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010 HAAS
000030 KWAN

C01

38250.00

000120 O'CONNELL

A00

29250.00

000130 QUINTANA

C01

23800.00

000140 NICHOLLS

C01

28420.00

EMPLOYEE
PROJNO

PROJNAME

AD3100 ADMIN SERVICES


IF1000 QUERY SERVICES
IF2000 USER EDUCATION

DEPTNO

RESPEMP

D01

000010

C01
C01

000030
000030

PROJECT

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE EMPNO
(SELECT RESPEMP
NOT IN
FROM PROJECT);

RESPEMP

000010
000030

Copyright IBM Corporation 2007, 2012

Figure 5-4. Subquery with NOT IN predicate

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-7

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

3. Using the COALESCE function

5-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

NOT IN predicate for nullable column


EMPNO

LASTNAME

HAAS
KWAN
O'CONNELL
QUINTANA
NICHOLLS
WILSON

WORKDEPT

A00
C01
A00
C01
C01
NULL

SALARY

52750.00
38250.00
29250.00
EMPLOYEE
23800.00
28420.00
25400.00

Which departments
have no employees?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130
000140
000400
DEPTNO

A00
C01
D01

DEPTNAME

MGRNO

SPIFFY COMPUTER SERVICE DIV.000010


000030 DEPARTMENT
INFORMATION CENTER
DEVELOPMENT CENTER

WORKDEPT

SELECT DEPTNO, DEPTNAME


FROM DEPARTMENT
WHERE DEPTNO
NOT IN (SELECT WORKDEPT

A00
A00
C01
C01
NULL

FROM EMPLOYEE);

DEPTNO DEPTNAME

EMPTY!!!
Wrong result!

You must avoid


the NULL value!

Copyright IBM Corporation 2007, 2012

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.

Ex

cl

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:

pr

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-9

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:

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT DEPTNO, DEPTNAME


FROM DEPARTMENT
WHERE DEPTNO NOT IN
(SELECT WORKDEPT
FROM EMPLOYEE
WHERE WORKDEPT IS NOT NULL) ;

5-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Subquery with ORDER BY

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Example: Show only employees that are in the last three


departments (alphabetically) in the department table
SELECT EMPNO, LASTNAME, WORKDEPT
FROM EMPLOYEE
WHERE WORKDEPT IN
( SELECT DEPTNO
FROM DEPARTMENT
ORDER BY DEPTNO DESC
FETCH FIRST 3 ROWS ONLY );

Copyright IBM Corporation 2007, 2012

Figure 5-6. Subquery with ORDER BY

CE1319.4

Notes:

pr

Ex

cl

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-11

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:
LASTNAME
-----------GEYER
HENDERSON
SPENSER
SCHNEIDER
PARKER
SMITH
SETRIGHT
MEHTA
LEE
GOUNOT

WORKDEPT
-----------E01
E11
E21
E11
E11
E11
E11
E21
E21
E21

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO
-----000050
000090
000100
000280
000290
000300
000310
000320
000330
000340

pr

Ex

cl

10 record(s) selected.

5-12 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Subquery with ALL predicate (1 of 2)


EMPNO

HAAS

WORKDEPT

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00
23800.00
28420.00

Which
department
has the highest
salary cost?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130
000140

LASTNAME

KWAN
O'CONNELL
QUINTANA
NICHOLLS

EMPLOYEE

SELECT WORKDEPT AS DEPTNO,


SUM(SALARY) AS TOTAL_SALARY
FROM EMPLOYEE
GROUP BY WORKDEPT
HAVING
Sum of salaries >=
Sum of salaries for
ALL departments

A00 SUM

SUM(SALARY)

82000.00

90470.00

C01 SUM

Copyright IBM Corporation 2007, 2012

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.

Ex

cl

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.

pr

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-13

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

5-14 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Subquery with ALL predicate (2 of 2)


EMPNO

WORKDEPT

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00
23800.00
28420.00

HAAS
KWAN
O'CONNELL
QUINTANA

Which
department
has the highest
salary cost?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130
000140

LASTNAME

NICHOLLS

EMPLOYEE

SELECT WORKDEPT AS DEPTNO,


SUM(SALARY) AS TOTAL_SALARY
FROM EMPLOYEE
GROUP BY WORKDEPT
HAVING SUM(SALARY)>=

ALL (SELECT SUM(SALARY)

FROM EMPLOYEE
GROUP BY WORKDEPT);

DEPTNO

TOTAL_SALARY

C01

90470.00

SUM(SALARY)

82000.00
90470.00

Copyright IBM Corporation 2007, 2012

Figure 5-8. Subquery with ALL predicate (2 of 2)

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-15

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

5-16 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Subquery with ANY or SOME predicate (1 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Which employees have


a salary that is higher than
the average of at least one
department?

EMPNO

000010
000030
000120
000130
000140

LASTNAME

WORKDEPT

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00
23800.00
28420.00

HAAS

KWAN
O'CONNELL
QUINTANA
NICHOLLS

EMPLOYEE

SELECT EMPNO, LASTNAME, SALARY


FROM EMPLOYEE
WHERE SALARY >
Average salary of
ANY department

AVG(SALARY)

41000.00000000
30156.66666666

Copyright IBM Corporation 2007, 2012

Figure 5-9. Subquery with ANY or SOME predicate (1 of 2)

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-17

Student Notebook

Subquery with ANY or SOME predicate (2 of 2)


WORKDEPT

SALARY

000010 HAAS

A00

52750.00

000030 KWAN

C01

38250.00

000120 O'CONNELL

A00

29250.00

000130 QUINTANA

C01

23800.00

000140 NICHOLLS

C01

28420.00

LASTNAME

Which employees have


a salary that is higher than
the average of at least one
department?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

EMPLOYEE

SELECT EMPNO, LASTNAME, SALARY


FROM EMPLOYEE
WHERE SALARY >
ANY (SELECT AVG(SALARY)
FROM EMPLOYEE
GROUP BY WORKDEPT);
EMPNO

LASTNAME

AVG(SALARY)

41000.00000000
30156.66666666

SALARY

000010 HAAS

52750.00

000030 KWAN

38250.00

Copyright IBM Corporation 2007, 2012

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:

cl

True if the specified relationship is true for at least one value returned by the subquery.

Ex

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.

pr

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
5-18 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

employee's individual salary is higher than the average salary for any of the departments,
we will report on that individual.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-19

Student Notebook

Subquery with EXISTS predicate (1 of 2)


WORKDEPT

LASTNAME

SALARY

I would like to have a


list of all employees earning
more than 30,000, if there is
any employee earning less than
25,000.

HIREDATE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

000010 HAAS
000030 KWAN
000120 O'CONNELL
000130 QUINTANA
000140 NICHOLLS

A00
C01
A00

52750.00 1965-01-01
38250.00 1975-04-05
29250.00 1963-12-05

C01
C01

23800.00 1971-07-28
28420.00 1976-12-15

EMPLOYEE

SELECT EMPNO, LASTNAME, SALARY, HIREDATE


FROM EMPLOYEE
WHERE SALARY > 30000 AND
EXISTS at least one employee
earning less than $25,000

EMPNO

LASTNAME

000130 QUINTANA

WORKDEPT

C01

SALARY

HIREDATE

23800.00 1971-07-28

Copyright IBM Corporation 2007, 2012

Figure 5-11. Subquery with EXISTS predicate (1 of 2)

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.

5-20 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Subquery with EXISTS Predicate (2 of 2)


WORKDEPT

LASTNAME

SALARY

I would like to have a


list of all employees earning
more than 30,000, if there is
any employee earning less than
25,000.

HIREDATE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

000010 HAAS

A00

52750.00 1965-01-01

000030
000120
000130
000140

C01
A00
C01
C01

38250.00
29250.00
23800.00
28420.00

KWAN
O'CONNELL
QUINTANA
NICHOLLS

1975-04-05
1963-12-05
1971-07-28
1976-12-15

EMPLOYEE

SELECT EMPNO, LASTNAME, SALARY, HIREDATE


FROM EMPLOYEE
WHERE SALARY > 30000 AND

EXISTS (SELECT * FROM EMPLOYEE


WHERE SALARY < 25000) ;
EMPNO

LASTNAME

000130 QUINTANA

WORKDEPT

C01

SALARY

HIREDATE

23800.00 1971-07-28

Copyright IBM Corporation 2007, 2012

Figure 5-12. Subquery with EXISTS Predicate (2 of 2)

CE1319.4

Notes:

cl

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.

Ex

An EXISTS predicate can be negated by preceding it with the keyword NOT.

pr

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-21

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

5-22 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Correlated subquery (1 of 2)
EMPNO

LASTNAME

WORKDEPT

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00

Which employees have


a salary that is higher than
the average of their
department?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010 HAAS
000030 KWAN
000120 O'CONNELL
000130 QUINTANA
000140 NICHOLLS

23800.00
28420.00

EMPLOYEE

SELECT EMPNO, LASTNAME, SALARY


FROM EMPLOYEE
WHERE SALARY >

Average salary of
corresponding department

AVG(SALARY)

A00 41000.00000000

AVG(SALARY)

C01 30156.66666666

Copyright IBM Corporation 2007, 2012

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.

Ex

cl

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.

pr

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-23

Student Notebook

Correlated subquery (2 of 2)
EMPNO

WORKDEPT

SALARY

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00
23800.00
28420.00

HAAS
KWAN
O'CONNELL
QUINTANA

Which employees have


a salary that is higher than
the average of their
department?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130
000140

LASTNAME

NICHOLLS

EMPLOYEE

SELECT EMPNO, LASTNAME, SALARY


FROM EMPLOYEE
E
WHERE SALARY >
(SELECT AVG(SALARY)
FROM EMPLOYEE
WHERE WORKDEPT =
E. WORKDEPT);
EMPNO

LASTNAME

SALARY

000010 HAAS
000030 KWAN

52750.00
38250.00

AVG(SALARY)

A00

AVG(SALARY)

C01

41000.0000000
0

30156.6666666
6

Copyright IBM Corporation 2007, 2012

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:

cl

1. A row is read by the outer query

2. Then a value from that row is passed to the subquery

Ex

3. That value is then used to control which rows or values are returned by the subquery.

pr

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

5-24 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

average salary for all employees in the work department that was passed from the outer
row.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-25

Student Notebook

Correlated subquery with EXISTS


EMPNO

HAAS
KWAN
O'CONNELL
QUINTANA
NICHOLLS
WILSON

WORKDEPT

A00
C01
A00
C01
C01
NULL

SALARY

52750.00
38250.00
29250.00
EMPLOYEE
23800.00
28420.00
25400.00

Which departments
have no employees?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130
000140
000400

LASTNAME

DEPTNO DEPTNAME

A00
C01
D01

MGRNO

SPIFFY COMPUTER SERVICE DIV.000010


INFORMATION CENTER
000030 DEPARTMENT
DEVELOPMENT CENTER
-

SELECT DEPTNO, DEPTNAME


FROM DEPARTMENT D
WHERE NOT EXISTS
(SELECT *
FROM EMPLOYEE
WHERE
WORKDEPT = D.DEPTNO);

DEPTNO DEPTNAME
DEVELOPMENT CENTER
D01
RESULT

Copyright IBM Corporation 2007, 2012

Figure 5-15. Correlated subquery with EXISTS

CE1319.4

Notes:

pr

Ex

cl

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.

5-26 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Scalar Fullselect

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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' ;

Copyright IBM Corporation 2007, 2012

Figure 5-16. Scalar Fullselect

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-27

Student Notebook

UPDATE including subquery

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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 ;

Copyright IBM Corporation 2007, 2012

Figure 5-17. UPDATE including subquery

CE1319.4

Notes:

cl

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:

pr

Ex

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.

5-28 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Checkpoint
List three quantified predicates used with subqueries.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.
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?

Copyright IBM Corporation 2007, 2012

Figure 5-18. Checkpoint

CE1319.4

Notes:

Enter your answers below:


1.

cl

2.

pr

Ex

3.

Copyright IBM Corp. 2007, 2013

Unit 5. Using Subqueries

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-29

Student Notebook

Unit summary
Having completed this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Code subqueries using the ALL, ANY/SOME, and EXISTS


keywords
Code correlated subqueries

Choose the proper type of subquery to use in each case

Copyright IBM Corporation 2007, 2012

Figure 5-19. Unit summary

CE1319.4

pr

Ex

cl

Notes:

5-30 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 6. Scalar Functions


What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

Checkpoint

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Extend your knowledge of scalar functions which:


Manipulate arithmetic data
Manipulate date values

Manipulate character data

Copyright IBM Corporation 2007, 2012

Figure 6-1. Unit objectives

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.

6-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Functions Two types

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

FUNCTION_NAME(argument)

Column Functions

Scalar Functions

(AKA SUMMARY functions)


function

SUM
value
AVG
MIN
MAX
COUNT(*)
COUNT(DISTINCT column_name)

function

value

value
value
value

Copyright IBM Corporation 2007, 2012

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.

pr

Ex

cl

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-3

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Scalar functions

S U S A N

Copyright IBM Corporation 2007, 2012

Figure 6-3. Scalar functions

CE1319.4

Notes:

cl

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.

Ex

Some of the general categories of scalar functions include:


1. String manipulation

2. Date, time and timestamp comparisons and calculations

pr

3. Mathematical functions
a. Algebraic

b. Statistical
c. Calculus

6-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Scalar function SUBSTR


)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SUBSTR(string, start
,length

DEPTNO DEPTNAME

A00
C01
D01

MGRNO

SPIFFY COMPUTER SERVICE DIV.000010


INFORMATION CENTER
000030 DEPARTMENT
DEVELOPMENT CENTER

SELECT SUBSTR(DEPTNO,1,2) AS AREA, DEPTNAME


FROM DEPARTMENT
WHERE DEPTNAME LIKE '%CENTER;
AREA

C0
D0

DEPTNAME

INFORMATION CENTER
DEVELOPMENT CENTER

Copyright IBM Corporation 2007, 2012

Figure 6-4. Scalar function - SUBSTR

CE1319.4

Notes:

The SUBSTR() function is used to extract a portion of a string.

Ex

cl

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.

pr

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-5

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

6-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Position string
E

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT LASTNAME, POSSTR(LASTNAME, 'SON')


FROM EMPLOYEE
WHERE UPPER(LASTNAME) like '%SON% ;
HENDERSON
ADAMSON
JEFFERSON
JOHNSON

7
5
7
5

Copyright IBM Corporation 2007, 2012

Figure 6-5. Position string

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-7

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Scalar function COALESCE/VALUE (1 of 2)

COALESCE(expression

,expression

DEPTNO DEPTNAME

A00
C01
D01

SPIFFY COMPUTER SERVICE


INFORMATION CENTER
DEVELOPMENT CENTER

MGRNO

000010
000030

DEPARTMENT

SELECT DEPTNO, DEPTNAME,


COALESCE(MGRNO,'NONE') AS MANAGER
FROM DEPARTMENT ;
DEPTNO DEPTNAME

A00
C01
D01

SPIFFY COMPUTER SERVICE


INFORMATION CENTER
DEVELOPMENT CENTER

MANAGER

000010
000030
NONE

Copyright IBM Corporation 2007, 2012

Figure 6-6. Scalar function - COALESCE/VALUE (1 of 2)

CE1319.4

Notes:

cl

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.

Ex

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

pr

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
6-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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:

pr

Ex

cl

SELECT DEPTNO,DEPTNAME,
CASE WHEN MGRNO IS NULL THEN 'NONE'
ELSE MGRNO
END AS MANAGER
FROM DEPT ;

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-9

Student Notebook

Scalar function COALESCE/VALUE (2 of 2)


EMPNO LASTNAME

HAAS

A00
C01
A00
C01
C01

52750.00
38250.00
29250.00
28420.00

COMM

4220.00
3060.00
2340.00
1904.00
-

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130
000140

WORKDEPT SALARY

KWAN
O'CONNELL
QUINTANA
NICHOLLS

EMPLOYEE

SELECT EMPNO,
COALESCE(SALARY+COMM,SALARY,COMM,0)
AS TOT_SALARY
FROM EMPLOYEE ;
EMPNO

000010
000030
000120
000130
000140

TOT_SAL

56970.00
41310.00
31590.00
1904.00
28420.00

Prefer COALESCE
over VALUE

Copyright IBM Corporation 2007, 2012

Figure 6-7. Scalar function - COALESCE/VALUE (2 of 2)

CE1319.4

Notes:

cl

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.

Ex

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 ;

pr

The VALUE function can be specified in place of the COALESCE function. COALESCE
should be used for conformance to SQL 2003 Core.

6-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Scalar function DECIMAL


DECIMAL(expression

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

,integer

,integer

EMPNO

000010
000030
000120
000130
000140

LASTNAME

HAAS

KWAN
O'CONNELL
QUINTANA
NICHOLLS

WORKDEPT

SALARY

HIREDATE

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

EMPLOYEE

SELECT AVG(SALARY) AS AVERAGE_1,


DECIMAL(AVG(SALARY),8,2) AS AVERAGE_2,
DECIMAL(AVG(SALARY)+0.005,8,2) AS AVERAGE_3
FROM EMPLOYEE
WHERE WORKDEPT = 'C01 ;
AVERAGE_1

AVERAGE_2

AVERAGE_3

30156.66666666 30156.66 30156.67


Copyright IBM Corporation 2007, 2012

Figure 6-8. Scalar function - DECIMAL

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-11

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

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.

6-12 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Scalar function ROUND


)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

ROUND ( expression1, expression2


EMPNO

000010
000030
000120
000130
000140

LASTNAME

HAAS

KWAN
O'CONNELL
QUINTANA
NICHOLLS

WORKDEPT

SALARY

HIREDATE

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

EMPLOYEE

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_4
FROM EMPLOYEE
WHERE WORKDEPT = 'C01 ;
AVERAGE_2

AVERAGE_1

LUW:
z/OS:

30156.66666666
30156.66666666

AVERAGE_4

AVERAGE_3

+3.01566700000000E+004 +3.01600000000000E+004
30156.67000000
30160.00000000

30160.00

30160.00

Copyright IBM Corporation 2007, 2012

Figure 6-9. Scalar function - ROUND

CE1319.4

Notes:

cl

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:

Ex

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.

pr

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-13

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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;

pr

Ex

cl

124. 123.

6-14 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Scalar function DIGITS

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DIGITS(expression)
EMPNO

000010
000030
000120
000130
000140

LASTNAME

HAAS

KWAN
O'CONNELL
QUINTANA
NICHOLLS

WORKDEPT

SALARY

HIREDATE

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

EMPLOYEE

DECIMAL(9,2)

SELECT DIGITS(SALARY) AS SALARY_1,


SUBSTR(DIGITS(SALARY),3,5) AS SALARY_2
FROM EMPLOYEE
WHERE EMPNO = '000140'

Datatype:

SALARY_1

SALARY_2

002842000

28420

char(9)

char(5)

Copyright IBM Corporation 2007, 2012

Figure 6-10. Scalar function - DIGITS

CE1319.4

Notes:

cl

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.

Ex

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:

pr

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-15

Student Notebook

Note

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

6-16 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Scalar functions SQRT and POWER


SQRT(expression)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

POWER(expression1, expression2)

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 EMPLOYEE
WHERE 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

Copyright IBM Corporation 2007, 2012

Figure 6-11. Scalar functions - SQRT and POWER

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-17

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2 DATE, TIME and TIMESTAMP

Data Type

Internal Format

Internal Length

External Length

DATE

yyyymmdd

4 bytes

10 bytes

TIME

hhmmss

3 bytes

8 bytes

TIMESTAMP

yyyymmddhhmmssnnnnnn 10 bytes

26 bytes

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.

Copyright IBM Corporation 2007, 2012

Figure 6-12. DB2 DATE, TIME and TIMESTAMP

CE1319.4

Notes:

cl

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.

Ex

A timestamp is a seven-part value (year, month, day, hour, minute, second, and
microsecond) that represents a date and a time including microseconds.

pr

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 below
would execute successfully:

6-18 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT EMPNO, LASTNAME, BIRTHDATE


FROM EMPLOYEE
WHERE BIRTHDATE = 1950-12-18 ;
SELECT EMPNO, LASTNAME, BIRTHDATE
FROM EMPLOYEE
WHERE BIRTHDATE = 12/18/1950 ;
SELECT EMPNO, LASTNAME, BIRTHDATE
FROM EMPLOYEE
WHERE BIRTHDATE = 18.12.1950 ;

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-19

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Output formats for date and time

Copyright IBM Corporation 2007, 2012

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.

cl

The pattern for dates is as follows, where Y is year, M is month, D is day:


ISO (International Standards Organization) = YYYY-MM-DD

Ex

USA = MM/DD/YY

EUR (European Standard) = DD.MM.YYYY

JIS (Japanese International Standard) = YYYY-MM-DD

pr

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
6-20 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

It is also possible to generate other formats for date and time, depending upon your needs
(this is accommodated by the LOCAL format).

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-21

Student Notebook

Scalar function CHAR (1 of 2)


CHAR(expression

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

,ISO
,USA
,EUR
,JIS
,LOCAL

WORKDEPT

SALARY

HIREDATE

000010 HAAS

A00

52750.00

1965-01-01

000030 KWAN

C01

38250.00

1975-04-05

000120 O'CONNELL

A00

29250.00

1963-12-05

000130 QUINTANA

C01

23800.00

1971-07-28

000140 NICHOLLS

C01

28420.00

1976-12-15

EMPNO

LASTNAME

EMPLOYEE

SELECT CHAR(HIREDATE,ISO)
AS ISO_DATE,
CHAR(HIREDATE,USA)
AS USA_DATE,
CHAR(HIREDATE,EUR)
AS EUR_DATE
FROM EMPLOYEE
WHERE EMPNO = '000130 ;
ISO_DATE

USA_DATE

EUR_DATE

1971-07-28 07/28/1971 28.07.1971


Copyright IBM Corporation 2007, 2012

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.

cl

When the first argument is a date, time or timestamp, the second argument is the name
of a date/time format.

Ex

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.

pr

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.

6-22 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

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).

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-23

Student Notebook

Scalar function CHAR (2 of 2)


)

CHAR(character-expression
,integer

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CHAR(integer-expression

CHAR(floating-point-expression
CHAR(decimal-expression
CHAR(decimal-expression

,decimal-character

WORKDEPT

SALARY

HIREDATE

000010 HAAS

A00

52750.00

1965-01-01

000030 KWAN

C01

38250.00

1975-04-05

000120 O'CONNELL

A00

29250.00

1963-12-05

000130 QUINTANA

C01

23800.00

1971-07-28

000140 NICHOLLS

C01

28420.00

1976-12-15

EMPNO

LASTNAME

EMPLOYEE

DECIMAL(9,2)

SELECT CHAR(SALARY,',') AS DEC_COMMA


FROM EMPLOYEE
WHERE EMPNO = '000130 ;
DEC_COMMA

0023800,00

Copyright IBM Corporation 2007, 2012

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:

cl

Character string value if the first argument is any type of character string

Ex

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

pr

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

6-24 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

precision of the decimal-expression (with a preceding minus sign if the argument is


negative).

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-25

Student Notebook

Scalar function LENGTH

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

LENGTH(expression))
EMPNO

WORKDEPT

LASTNAME

000010 HAAS
000030 KWAN
000120 O'CONNELL
000130 QUINTANA
000140 NICHOLLS

A00
C01

A00
C01
C01

SALARY

52750.00
38250.00
29250.00
23800.00
28420.00

HIREDATE

1965-01-01
1975-04-05
1963-12-05
1971-07-28
1976-12-15

EMPLOYEE

DECIMAL(9,2)

SELECT LENGTH(LASTNAME) AS LENGTH_LASTNAME,


LENGTH(SALARY) AS LENGTH_SALARY,
LENGTH(DIGITS(SALARY)) AS DIGITS_SALARY
FROM EMPLOYEE
WHERE EMPNO = '000120'
LENGTH_LASTNAME

LENGTH_SALARY

DIGITS_SALARY

Copyright IBM Corporation 2007, 2012

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.

cl

The result of the function is an integer, unless the argument is a null, in which case the
result is null.

Ex

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.

pr

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

6-26 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

blanks, these would be counted as well. Apparently, the name O'CONNELL has been
stored without trailing blanks.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-27

Student Notebook

Scalar functions LTRIM / RTRIM


)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

LTRIM(expression
RTRIM(expression

SALARY

HIREDATE

10

HAAS

A00

52750.00

1965-01-01

30

KWAN

EMPNO

Assume leading '0's have


been translated to blanks

LASTNAME

WORKDEPT
C01

38250.00

1975-04-05

120

O'CONNELL

A00

29250.00

1963-12-05

130

QUINTANA

C01

23800.00

1971-07-28

140

NICHOLLS

C01

28420.00

1976-12-15

EMPLOYEE

SELECT CHAR(LTRIM(EMPNO),10) AS STRIPPED_EMPNO,


LENGTH(RTRIM(LASTNAME)) AS LENGTH_NAME
FROM EMPLOYEE
WHERE EMPNO IN('
30','
120','
140')
STRIPPED_EMPNO

LENGTH_NAME

30
120

4
9

140

Copyright IBM Corporation 2007, 2012

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.

cl

The example can be done with the following SQL statement without changing the
EMPLOYEE table:

pr

Ex

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_NAME
FROM EMP
WHERE EMPNO IN ('
30','
120','
140');

6-28 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

This results in the following output:


STRIPPED_EMPNO LENGTH_NAME
---------+---------+-------30
4
120
9
140
8

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

cl

The previous example is changed to use the STRIP function instead of the LTRIM function
as shown below:

Ex

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'))

pr

SELECT CHAR(STRIP(EMPNO,LEADING,'0'),10) AS STRIPPED_EMPNO,


LENGTH(RTRIM(LASTNAME)) AS LENGTH_NAME
FROM EMP
WHERE EMPNO IN ('
30','
120','
140');

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-29

Student Notebook

This results in the following output:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

--------+---------+--------STRIPPED_EMPNO LENGTH_NAME
---------+---------+-------30
4
120
9
140
8

pr

Ex

cl

Notice the leading blanks in the first column in the output.

6-30 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Date-related scalar functions (1 of 4)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DATE(expression))

YEAR(expression))

MONTH(expression))

DAY(expression))

DAYS(expression))

Copyright IBM Corporation 2007, 2012

Figure 6-18. Date-related scalar functions (1 of 4)

CE1319.4

Notes:

Ex

cl

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

pr

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-31

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

6-32 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Date-related scalar functions (2 of 4)


EMPNO

HAAS
KWAN
O'CONNELL
QUINTANA

WORKDEPT

SALARY

HIREDATE

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

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130
000140

LASTNAME

NICHOLLS

EMPLOYEE

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 EMPLOYEE
WHERE YEAR(DATE(2005-04-04)- HIREDATE) BETWEEN 29 AND 30 ;
HIREDATE

EUR_DATE

DATE

YEAR

1975-04-05 05.04.1975 1975-04-05 1975

MONTH

DAY

DAYS

PERIOD

PERIOD_IN_DAYS

Y2K

10957

1999-12-31

721083 291129

Copyright IBM Corporation 2007, 2012

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.

Ex

cl

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.

pr

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-33

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Ex

cl

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).

pr

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.

6-34 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-35

Student Notebook

Date-related scalar functions (3 of 4)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DAYOFYEAR(expression))

WEEK(expression))

DAYOFWEEK(expression))

WEEK_ISO(expression))

DAYOFWEEK_ISO(expression))

JULIAN_DAY(expression)

Copyright IBM Corporation 2007, 2012

Figure 6-20. Date-related scalar functions (3 of 4)

CE1319.4

Notes:

This visual lists some additional date-related scalar functions.

cl

The DAYOFYEAR() function returns the day of the year in argument as an integer
value in the range 1-366.

Ex

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.

pr

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.

6-36 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-37

Student Notebook

Date-related scalar functions (4 of 4)


EMPNO

WORKDEPT

LASTNAME

000010 HAAS
000030 KWAN
000120 O'CONNELL
000130 QUINTANA

52750.00 1965-01-01
38250.00 1975-04-05
29250.00 1963-12-05

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

A00
C01
A00
C01
C01

HIREDATE

SALARY

000140 NICHOLLS

23800.00 1971-07-28
28420.00 1976-12-15

SELECT HIREDATE,
DAYOFYEAR(HIREDATE)
DAYOFWEEK(HIREDATE)
DAYOFWEEK_ISO(HIREDATE)
CHAR(DAYNAME(HIREDATE),9)
WEEK(HIREDATE)
WEEK_ISO(HIREDATE)
JULIAN_DAY(HIREDATE)
MOD(JULIAN_DAY(HIREDATE),7)+1
FROM EMPLOYEE
WHERE EMPNO = '000010 ;
HIREDATE

DAYOFYEAR

1965-01-01

DAYOFWEEK

DAYOFWEEK
_ISO

EMPLOYEE

DAYNAME

WEEK

Friday

AS
AS
AS
AS
AS
AS
AS
AS

DAYOFYEAR,
DAYOFWEEK,
DAYOFWEEK_ISO,
DAYNAME,
WEEK,
WEEK_ISO,
JULIAN_DAY,
JULIAN_WEEKDAY

WEEK
_ISO

53

JULIAN_DAY

2438762

JULIAN_
WEEKDAY

Copyright IBM Corporation 2007, 2012

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.

cl

The first column of the result (HIREDATE) shows the hiring date with default format as
previously.

Ex

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.

pr

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.)

6-38 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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).

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-39

Student Notebook

Time/Timestamp-related scalar functions (1 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

TIME(expression))

HOUR(expression))

MINUTE(expression))

SECOND(expression))

MICROSECOND(expression))

TIMESTAMP(expression

,expression

Copyright IBM Corporation 2007, 2012

Figure 6-22. Time/Timestamp-related scalar functions (1 of 2)

CE1319.4

Notes:

cl

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.

pr

Ex

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
6-40 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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:

pr

Ex

cl

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
;

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-41

Student Notebook

Time/Timestamp-related scalar functions (2 of 2)


EMPNO

WORKDEPT

LASTNAME

A00
C01
A00
C01
C01

52750.00 1965-01-01
38250.00 1975-04-05
29250.00 1963-12-05

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010 HAAS
000030 KWAN
000120 O'CONNELL
000130 QUINTANA

HIREDATE

SALARY

000140 NICHOLLS

EMPLOYEE

23800.00 1971-07-28
28420.00 1976-12-15

SELECT
TIMESTAMP(HIREDATE,'08:15:35')
TIME(TIMESTAMP(HIREDATE,'08:15:35'))
HOUR(TIMESTAMP(HIREDATE,'08:15:35'))
MINUTE(TIMESTAMP(HIREDATE,'08:15:35'))
SECOND(TIMESTAMP(HIREDATE,'08:15:35'))
MICROSECOND(TIMESTAMP(HIREDATE,'08:15:35'))

AS
AS
AS
AS
AS
AS

TIMESTAMP,
TIME,
HOUR,
MINUTE,
SECOND,
MICRO,

TIMESTAMP('20000101000000')-TIMESTAMP(HIREDATE,'08:15:35')
AS UNTIL_Y2K FROM EMPLOYEE
WHERE EMPNO = '000030 ;

TIMESTAMP

TIME

1975-04-05-08.15.35.000000 08.15.35

HOUR

MINUTE

SECOND

MICRO

UNTIL_Y2K

15

35

240825154425.000000

Copyright IBM Corporation 2007, 2012

Figure 6-23. Time/Timestamp-related scalar functions (2 of 2)

CE1319.4

Notes:

cl

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'.

Ex

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.

pr

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.

6-42 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Labeled durations
LASTNAME

EMPNO

HAAS
KWAN
O'CONNELL
QUINTANA

SALARY

HIREDATE

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

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

000010
000030
000120
000130
000140

WORKDEPT

NICHOLLS

EMPLOYEE

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 EMPLOYEE
WHERE EMPNO = '000120 ;
HIREDATE

DATE_1

DATE_2

TODAY

1963-12-05

1988-12-05

1962-08-30

1998-11-16

SELECT HIREDATE + (2 YEARS + 3 MONTHS + 5 DAYS)


FROM EMPLOYEE
WHERE EMPNO = '000010' ;
Copyright IBM Corporation 2007, 2012

Figure 6-24. Labeled durations

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-43

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

6-44 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Checkpoint
Which function can be used to extract the first three
characters of an alphanumeric column?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.

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?

Copyright IBM Corporation 2007, 2012

Figure 6-25. Checkpoint

CE1319.4

Notes:

Enter your answers below:


1.

cl

2.

Ex

3.

pr

4.

Copyright IBM Corp. 2007, 2013

Unit 6. Scalar Functions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-45

Student Notebook

Unit summary
Having completed this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Extend your knowledge of scalar functions which:


Manipulate arithmetic data
Manipulate date values

Manipulate character data

Copyright IBM Corporation 2007, 2012

Figure 6-26. Unit summary

CE1319.4

pr

Ex

cl

Notes:

6-46 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 7. Table Expressions and Recursive SQL


What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

Checkpoint

Copyright IBM Corp. 2007, 2013

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2012

Figure 7-1. Unit objectives

CE1319.4

Notes:

pr

Ex

cl

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.

7-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Nested Table Expressions

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Much like a subquery


Not defined in the DB2 catalog
Temporary in nature

Nested

In the FROM clause, rather than WHERE

Table

Sometimes called pseudo or inline views

Expression

Outer

SELECT .

Query

FROM (SELECT . FROM

WHERE . )

WHERE .

ORDER BY .

Copyright IBM Corporation 2007, 2012

Figure 7-2. Nested Table Expressions

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-3

Student Notebook

Nested Table Expressions


Rules for writing nested table expressions:
Enclose the expression in parentheses

2.

Provide a correlation name in the expression

3.

Ensure unique names for the columns of the results table

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.

Nested
Table

Outer

Expression

Intermediate Results

Query

Final Result Set

DB2 table on DASD

Copyright IBM Corporation 2007, 2012

Figure 7-3. Nested Table Expressions

CE1319.4

Notes:

Follow these rules when writing nested table expressions:


1. Enclose the table expression in parentheses.

Ex

cl

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.

pr

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.

7-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Nested table expressions


WORKDEPT

SALARY

HIREDATE

000010 HAAS

A00

52750.00

1965-01-01

000030 KWAN

C01

38250.00

1975-04-05

LASTNAME

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

000120 O'CONNELL

A00

29250.00

1963-12-05

000130 QUINTANA

C01

23800.00

1971-07-28

000140 NICHOLLS

C01

28420.00

1976-12-15

EMPLOYEE

SELECT S.HIREDECADE, MIN(S.SALARY) AS MINIMUM_SALARY


FROM
( SELECT SUBSTR(CHAR(HIREDATE,ISO),1,3)
CONCAT '0 - 9' AS HIREDECADE,
SALARY
FROM EMPLOYEE
) AS S
GROUP BY S.HIREDECADE ;
HIREDECADE

MINIMUM_SALARY

1960 - 9

29250.00

1970 - 9

23800.00

Copyright IBM Corporation 2007, 2012

Figure 7-4. Nested table expressions

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-5

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) ;

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

cl

Table, view

pr

Ex

Common table expressions

7-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Nested table expressions in joins (1 of 4)


WORKDEPT

SALARY

HIREDATE

000010 HAAS

A00

52750.00

1965-01-01

000030 KWAN

C01

38250.00

1975-04-05

LASTNAME

EMPLOYEE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

000120 O'CONNELL

A00

29250.00

1963-12-05

000130 QUINTANA

C01

23800.00

1971-07-28

000140 NICHOLLS

C01

28420.00

1976-12-15

SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE, E.SALARY, M.MINIMUM_SALARY


FROM
( subselect
) AS E
INNER JOIN
( subselect
) AS M
ON E.HIREDECADE = M.HIREDECADE ;
HIREDECADE

SALARY

MINIMUM_SALARY

000010 HAAS

1960 - 9

52750.00

29250.00

000030 KWAN

1970 - 9

38250.00

23800.00

000120 O'CONNELL

1960 - 9

29250.00

29250.00

000130 QUINTANA

1970 - 9

23800.00

23800.00

000140 NICHOLLS

1970 - 9

28420.00

23800.00

EMPNO

LASTNAME

Copyright IBM Corporation 2007, 2012

Figure 7-5. Nested table expressions in joins (1 of 4)

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-7

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

7-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Nested table expressions in joins (2 of 4)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE, E.SALARY, M.MINIMUM_SALARY


FROM
(
EMPNO

subselect

)
AS E
INNER JOIN
(

subselect

SALARY

HIREDECADE

000010 HAAS

LASTNAME

52750.00

1960 - 9

000030 KWAN

38250.00

1970 - 9

000120 O'CONNELL

29250.00

1960 - 9

000130 QUINTANA

23800.00

1970 - 9

000140 NICHOLLS

28420.00

1970 - 9

HIREDECADE

MINIMUM_SALARY

1960 - 9

29250.00

1970 - 9

23800.00

) AS M
ON E.HIREDECADE = M.HIREDECADE ;

Copyright IBM Corporation 2007, 2012

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:

cl

The first table expression, called E, should provide, for each employee, his/her
employee number, last name, salary, and hiring decade (column HIREDECADE).

Ex

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.

pr

By joining the two tables, we will get the desired result.

Copyright IBM Corp. 2007, 2013

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-9

Student Notebook

Nested table expressions in joins (3 of 4)


WORKDEPT

SALARY

HIREDATE

000010 HAAS

A00

52750.00

1965-01-01

000030 KWAN

C01

38250.00

1975-04-05

000120 O'CONNELL

A00

29250.00

1963-12-05

000130 QUINTANA

C01

23800.00

1971-07-28

000140 NICHOLLS

C01

28420.00

1976-12-15

LASTNAME

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

EMPLOYEE
(Base Table)

SELECT EMPNO, LASTNAME, SALARY,


SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0 - 9'
AS HIREDECADE
FROM EMPLOYEE
Nested Table
Expression

SALARY

HIREDECADE

000010 HAAS

52750.00

1960 - 9

000030 KWAN

38250.00

1970 - 9

000120 O'CONNELL

29250.00

1960 - 9

000130 QUINTANA

23800.00

1970 - 9

000140 NICHOLLS

28420.00

1970 - 9

EMPNO

LASTNAME

Intermediate
Results

Copyright IBM Corporation 2007, 2012

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.

Ex

cl

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.

pr

An AS clause is used to give the derived column the name HIREDECADE.

7-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Nested table expressions in joins (4 of 4)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE, E.SALARY, M.MINIMUM_SALARY


FROM
(
SELECT EMPNO, LASTNAME,SALARY,
SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0 - 9'
AS HIREDECADE
FROM EMPLOYEE

) AS E
INNER JOIN
( SELECT S.HIREDECADE, MIN(S.SALARY) AS MINIMUM_SALARY
FROM
(
SELECT SUBSTR(CHAR(HIREDATE,ISO),1,3)
CONCAT '0 - 9' AS HIREDECADE,
SALARY
FROM EMPLOYEE
) AS S
GROUP BY S.HIREDECADE

) AS M
ON E.HIREDECADE = M.HIREDECADE ;

Copyright IBM Corporation 2007, 2012

Figure 7-8. Nested table expressions in joins (4 of 4)

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-11

Student Notebook

Common table expressions

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

WITH
E AS
(
SELECT EMPNO, LASTNAME, SALARY,
SUBSTR(CHAR(HIREDATE,ISO),1,3) CONCAT '0 - 9'
AS HIREDECADE
FROM EMPLOYEE
),
M (HIREDECADE, MINIMUM_SALARY) AS
(
SELECT HIREDECADE, MIN(SALARY)
FROM E
GROUP BY HIREDECADE
)
SELECT E.EMPNO, E.LASTNAME, E.HIREDECADE,
E.SALARY, M.MINIMUM_SALARY
FROM E INNER JOIN M
ON E.HIREDECADE = M.HIREDECADE
Copyright IBM Corporation 2007, 2012

Figure 7-9. Common table expressions

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.
7-12 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

Copyright IBM Corp. 2007, 2013

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-13

Student Notebook

Recursive SQL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

WITH
RPL (PART, SUBPART, QUANTITY) AS
(

Initialization Select

SELECT ROOT.PART, ROOT.SUBPART, ROOT.QUANTITY


FROM PARTLIST ROOT
WHERE ROOT.PART = '01'

UNION ALL

Iterative Select

SELECT CHILD.PART, CHILD.SUBPART, PARENT.QUANTITY*CHILD.QUANTITY


FROM RPL PARENT, PARTLIST CHILD
WHERE PARENT.SUBPART = CHILD.PART

Main Select

SELECT

PART, SUBPART, SUM(QUANTITY) AS QUANTITY


FROM RPL
GROUP BY PART, SUBPART
ORDER BY PART, SUBPART
Copyright IBM Corporation 2007, 2012

Figure 7-10. Recursive SQL

CE1319.4

Notes:

cl

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?

pr

Ex

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.

7-14 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-15

Student Notebook

Recursive SQL Initialization SELECT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT ROOT.PART, ROOT.SUBPART, ROOT.QUANTITY


FROM PARTLIST ROOT
WHERE ROOT.PART = '01'

PART

SUBPART

00
00
01
01
01
01
02
02
03
04
04
05
05
06
06
07
07

QUANTITY

5
3
2
3
4
3
7
6
6
10
11
10
10
10
10
8
8

01
05
02
03
04
06
05
06
07
08
09
10
11
12
13
12
14

PARTLIST Table

PART

01
01
01
01

SUBPART

02
03
04
06

QUANTITY

2
3
4
3

RPL

Copyright IBM Corporation 2007, 2012

Figure 7-11. Recursive SQL - Initialization SELECT

CE1319.4

Notes:

cl

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'.

pr

Ex

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.

7-16 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Recursive SQL First iteration

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT CHILD.PART, CHILD.SUBPART, PARENT.QUANTITY*CHILD.QUANTITY


FROM RPL PARENT, PARTLIST CHILD
WHERE PARENT.SUBPART = CHILD.PART
PART

00
00
01
01
01
01
02
02
03
04
04
05
05
06
06
07
07

SUBPART

QUANTITY

01
05
02
03
04
06
05
06
07
08
09
10
11
12
13
12
14

5
3
2
3
4
3
7
6
6
10
11
10
10
10
10
8
8

PARTLIST Table

PART

SUBPART

01
01
01
01
02
02
03
04
04
06
06

02
03
04
06
05
06
07
08
09
12
13

QUANTITY

2
3
4
3
14
12
18
40
44
30
30

RPL

Copyright IBM Corporation 2007, 2012

Figure 7-12. Recursive SQL - First iteration

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-17

Student Notebook

Note

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

7-18 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Recursive SQL Second iteration

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT CHILD.PART, CHILD.SUBPART, PARENT.QUANTITY*CHILD.QUANTITY


FROM RPL PARENT, PARTLIST CHILD
WHERE PARENT.SUBPART = CHILD.PART
PART

SUBPART

00
00
01
01
01
01
02
02
03
04
04
05
05
06
06
07
07

01
05
02
03
04
06
05
06
07
08
09
10
11
12
13
12
14

QUANTITY

5
3
2
3
4
3
7
6
6
10
11
10
10
10
10
8
8

PARTLIST Table

PART

No correspondence
in PARTLIST table

01
01
01
01
02
02
03
04
04
06
06
05
05
06
06
07
07

Copyright IBM Corporation 2007, 2012

SUBPART

02
03
04
06
05
06
07
08
09
12
13
10
11
12
13
12
14

QUANTITY

2
3
4
3
14
12
18
40
44
30
30
140
140
120
120
144
144

RPL

Figure 7-13. Recursive SQL - Second iteration

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-19

Student Notebook

Note

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

7-20 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Recursive SQL Main SELECT

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT PART, SUBPART, SUM(QUANTITY) AS QUANTITY


FROM RPL
GROUP BY PART, SUBPART
ORDER BY PART, SUBPART

PART

01
01
01
01
02
02
03
04
04
06
06
05
05
06
06
07
07

SUBPART

02
03
04
06
05
06
07
08
09
12
13
10
11
12
13
12
14

QUANTITY

2
3
4
3
14
12
18
40
44
30
30
140
140
120
120
144
144

RPL

PART

SUBPART

QUANTITY

01
01
01
01
02
02
03
04
04
05
05
06
06
07
07

02
03
04
06
05
06
07
08
09
10
11
12
13
12
14

2
3
4
3
14
12
18
40
44
140
140
150
150
144
144

Final Result Table

Copyright IBM Corporation 2007, 2012

Figure 7-14. Recursive SQL - Main SELECT

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-21

Student Notebook

Controlling depth of recursion (1 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

WITH
RPL (LEVEL, PART, SUBPART, QUANTITY) AS
(

Initialization Select

SELECT 0, ROOT.PART, ROOT.SUBPART, ROOT.QUANTITY


FROM PARTLIST ROOT
WHERE ROOT.PART = '01'
UNION ALL

Iterative Select

SELECT PARENT.LEVEL+1, CHILD.PART, CHILD.SUBPART,


PARENT.QUANTITY*CHILD.QUANTITY
FROM RPL PARENT, PARTLIST CHILD
WHERE PARENT.SUBPART = CHILD.PART
AND PARENT.LEVEL < 2

Main Select

SELECT LEVEL, PART, SUBPART, SUM(QUANTITY) AS QUANTITY


FROM RPL
GROUP BY LEVEL, PART, SUBPART
ORDER BY LEVEL, PART, SUBPART
Copyright IBM Corporation 2007, 2012

Figure 7-15. Controlling depth of recursion (1 of 2)

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.
7-22 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-23

Student Notebook

Controlling depth of recursion (2 of 2)


SUBPART

00
00
01
01
01
01
02
02
03
04
04
05
05
06
06
07
07

01
05
02
03
04
06
05
06
07
08
09
10
11
12
13
12
14

QUANTITY

LEVEL

5
3
2
3
4
3
7
6
6
10
11
10
10
10
10
8
8

PART

SUBPART

QUANTITY

01

02

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

PART

PARTLIST Table

0
0
0
1
1
1
1
1
1
1
2
2
2
2

01
01
01
02
02
03
04
04
06
06
05
05
06
06

03
04
06
05
06
07
08
09
12
13
10
11
12
13

3
4
3
14
12
18
40
44
30
30
140
140
120
120

07

12

144

07

14

144

Final Result Table

Copyright IBM Corporation 2007, 2012

Figure 7-16. Controlling depth of recursion (2 of 2)

CE1319.4

Notes:

pr

Ex

cl

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.

7-24 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Recursive SQL Recommendations

Desk-check recursive SQL statement

Test recursive SQL against small test tables

Use controls to limit recursion

Copyright IBM Corporation 2007, 2012

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.

Ex

cl

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.

pr

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

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-25

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

7-26 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Checkpoint
T/F. Table expressions have many similarities to views.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.
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?

Copyright IBM Corporation 2007, 2012

Figure 7-18. Checkpoint

CE1319.4

Notes:

Enter your answers below:


1.

cl

2.

Ex

3.

pr

4.

Copyright IBM Corp. 2007, 2013

Unit 7. Table Expressions and Recursive SQL

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-27

Student Notebook

Unit summary
Having completed this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2012

Figure 7-19. Unit summary

CE1319.4

pr

Ex

cl

Notes:

7-28 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 8. An Introduction to UDTs/UDFs and Stored


Procedures
What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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 UDTs,
UDFs and SPs.

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

pr

Ex

cl

Checkpoint

Copyright IBM Corp. 2007, 2013

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

8-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Describe the concepts behind User-Defined Distinct Types,


User-Defined Functions and Stored Procedures

Copyright IBM Corporation 2007, 2012

Figure 8-1. Unit objectives

CE1319.4

Notes:

pr

Ex

cl

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.

8-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

User-defined Distinct Types The need

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Need to establish context for values


DB2 enforced typing

But... I was
only doing a
hundred!

SPEED
LIMIT
100

SPEED
LIMIT
65

CANADA

USA

Copyright IBM Corporation 2007, 2012

Figure 8-2. User-defined Distinct Types - The need

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

8-3

Student Notebook

User-defined Distinct Types Definition


AS INTEGER

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE DISTINCT TYPE KPH


WITH COMPARISONS

CREATE DISTINCT TYPE MPH AS INTEGER


WITH COMPARISONS

CREATE TABLE SPEED_LIMITS


( ROUTE_NUM SMALLINT NOT NULL,
KPH
CANADA_SL
NOT NULL,
MPH
US_SL
NOT NULL )
SELECT ROUTE_NUM
FROM SPEED_LIMITS
WHERE CANADA_SL > KPH(80)
SELECT ROUTE_NUM
FROM SPEED_LIMITS
WHERE CANADA_SL > US_SL

FAILS

Copyright IBM Corporation 2007, 2012

Figure 8-3. User-defined Distinct Types - Definition

CE1319.4

Notes:

cl

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.

Ex

UDTs are always based on existing built-in data types like CHAR and SMALLINT. UDTs
cannot be used as source types for newer UDTs.

pr

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.

8-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

CAST from the source type to the UDT. These functions are important for the
manipulation of the UDTs in queries.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Note

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

8-5

Student Notebook

User-Defined Functions The need

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

etc.

rating

cubic
yards

SCALAR
FUNCTIONS
REQUESTED
BY

mph

horsepower

UDTs?

watts

feet

DB2 CUSTOMERS

cups

etc.

etc.

Copyright IBM Corporation 2007, 2012

Figure 8-4. User-Defined Functions - The need

CE1319.4

Notes:

cl

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:

Ex

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

pr

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.

8-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

User-Defined Functions Sourced

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE FUNCTION "+" (MPH,MPH)


RETURNS MPH
SOURCE "+" (INTEGER,INTEGER)

CREATE FUNCTION AVG_US_SPEED (MPH)


RETURNS MPH
SOURCE AVG(INTEGER)

Existing Built-in Functions


(or UDFs)
SUM + - / * AVG MIN, etc.

Copyright IBM Corporation 2007, 2012

Figure 8-5. User-Defined Functions - Sourced

CE1319.4

Notes:

The CREATE FUNCTION statement is used to define UDFs.

Ex

cl

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.

pr

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

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

8-7

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

exists. The database manager will invoke the function that matches the input parameters
passed in an overloading situation.

8-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

User-Defined Functions External

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE FUNCTION
KPH_TO_MPH(KPH)
RETURNS MPH
SPECIFIC kph_conv
EXTERNAL NAME 'convert!kph2mph'
LANGUAGE C
PARAMETER STYLE DB2SQL
DETERMINISTIC
NO EXTERNAL ACTION
FENCED
NOT NULL CALL
NO SQL
NO SCRATCHPAD
NO FINAL CALL

CREATE FUNCTION
MPH_TO_KPH(MPH)
RETURNS KPH ....

Copyright IBM Corporation 2007, 2012

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.

Ex

cl

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.

pr

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

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

8-9

Student Notebook

User-Defined SQL Functions

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

Copyright IBM Corporation 2007, 2012

Figure 8-7. User-Defined SQL Functions

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.

8-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Stored Procedures Example 1

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE PROCEDURE KPH_TO_MPH (IN KPH SMALLINT, OUT MPH


SMALLINT)
LANGUAGE SQL
P1: BEGIN

SET MPH = KPH * 1.60934;


END P1@

CREATE PROCEDURE MPH_TO_KPH (IN MPH SMALLINT, OUT KPH


SMALLINT)
LANGUAGE SQL
P1: BEGIN

SET KPH = MPH * 0.62137;


END P1@

Copyright IBM Corporation 2007, 2012

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:

cl

CALL KPH_TO_MPH(CANADA_SL,?)
CALL MPH_TO_KPH(US_SL,?)

pr

Ex

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

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

8-11

Student Notebook

Stored Procedures Example 2

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE PROCEDURE MEDIAN


(OUT medianSalary DEC(7,2))
LANGUAGE SQL
P1: BEGIN
DECLARE v_numRecords INT DEFAULT 1;
DECLARE v_counter INT DEFAULT 0;
DECLARE c1 CURSOR FOR
SELECT salary FROM EMPLOYEE
ORDER BY salary;

Procedure
Body

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@

Copyright IBM Corporation 2007, 2012

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.

cl

A brief explanation of the code:

pr

Ex

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.

8-12 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Checkpoint
T/F. User-Defined Distinct Data Types can be compared
without restrictions.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.
2.

T/F. UDFs and Stored Procedures may only be written in


the C language.

3.

Briefly describe the functionality that SQL/PL provides.

Copyright IBM Corporation 2007, 2012

Figure 8-10. Checkpoint

CE1319.4

Notes:

Write your answers below:


1.

cl

2.

pr

Ex

3.

Copyright IBM Corp. 2007, 2013

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

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

8-13

Student Notebook

Unit summary
Having completed this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Describe the concepts behind User-Defined Distinct Types,


User-Defined Functions and Stored Procedures

Copyright IBM Corporation 2007, 2012

Figure 8-11. Unit summary

CE1319.4

pr

Ex

cl

Notes:

8-14 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Unit 9. SQL and DB2 Performance


What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

Checkpoint

Copyright IBM Corp. 2007, 2013

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-1

Student Notebook

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After completing this unit, you should be able to:

Explain some basic principles of DB2 performance

Code SQL statements to obtain better performance

Copyright IBM Corporation 2007, 2013

Figure 9-1. Unit objectives

CE1319.4

Notes:

pr

Ex

cl

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.

9-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

The Optimizer
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

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Each code version and operating system for DB2 has its own
distinct Optimizer

The rewrite techniques vary for each Optimizer

Poorly written SQL can dramatically hurt the performance of a


given query

The Optimizers have been evolving steadily since DB2 was first released

Each Optimizer is capable of rewriting some queries to improve


performance

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

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
Copyright IBM Corporation 2007, 2013

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.

Ex

cl

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.

pr

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

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-3

Student Notebook

Convert correlated subqueries to non correlated subqueries


Convert multiple OR predicates into an IN predicate
Convert a 'NOT <' predicate to a '>=' predicate

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

9-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Index concepts

INDEX

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

VALUE LOCATION (RID)


Copyright IBM Corporation 2007, 2013

Figure 9-3. Index concepts

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-5

Student Notebook

Clustered and Non-clustered indexes


Non-Clustering
Index
Root Node

Root Node

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Clustering
Index

13

45

86

13

45

86

Non-leaf
Node

Non-leaf
Node

13

20 33 45

75

86

13

20 33 45

Leaf
Node

75

86

Leaf
Node

Data Page

Data Page

Copyright IBM Corporation 2007, 2013

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.

Ex

cl

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.

pr

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.

9-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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).

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-7

Student Notebook

Index utilization
LASTNAME FIRSTNAME
Jim

RID

40

SELECT *
WHERE LASTNAME = 'Free'

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Abell
...
Campbell
...
Cheng

AGE

Debbie

32

David

45

Free
Free
Free
Free
Free
Free
Free
Free
Free
Free

Ann
Bernie
David
Debbie
Dennis
Diane
Donald
Donna
Doreen
Doug

64
66
35
39
37
41
33
42
36
34

Klonne
...

Dennis

42

SELECT LASTNAME
WHERE FIRSTNAME = 'David'

SELECT *
WHERE LASTNAME = 'Free'
AND FIRSTNAME > 'Diane'

SELECT *
WHERE LASTNAME = 'Free'
AND AGE > 36

SELECT ...
WHERE ...
GROUP BY LASTNAME
-orORDER BY LASTNAME

Copyright IBM Corporation 2007, 2013

Figure 9-5. Index utilization

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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.
9-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

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.

Ex

cl

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.

pr

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

Copyright IBM Corp. 2007, 2013

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-9

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Note

pr

Ex

cl

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.

9-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Predicates

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Predicate = ONE Condition in a WHERE Clause

Predicate

WHERE

A = B

AND

C = D

OR

J > K

Predicate

Predicate

Copyright IBM Corporation 2007, 2013

Figure 9-6. Predicates

CE1319.4

Notes:

pr

Ex

cl

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

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-11

Student Notebook

Predicate processing
Index Manager

best

Index-sargable predicates

P
E
R
F
O
R
M
A
N
C
E

DEPTNO = 10

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Range-delimiting predicates

(stage 1)

SALARY > 50000


when SALARY on
leading index column

Data Management Services

Data-sargable predicates

Relational Data Services

Residual (stage 2) predicates

worst

DEPTNO = 10 when
DEPTNO not in index

Correlated subqueries
Quantified subqueries
(ANY, ALL, SOME, IN)
LONG VARCHAR

Copyright IBM Corporation 2007, 2013

Figure 9-7. Predicate processing

CE1319.4

Notes:

cl

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.

Ex

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.

pr

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
9-12 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

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.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

Let us look at some general guidelines that could help you write better-performing SQL
statements.

Copyright IBM Corp. 2007, 2013

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-13

Student Notebook

General guidelines Correlated subqueries

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT EMPNO, LASTNAME, WORKDEPT, SALARY


FROM EMPLOYEE E
WHERE
SALARY >
(SELECT AVG(SALARY)
FROM EMPLOYEE
WHERE WORKDEPT = E.WORKDEPT)
SELECT EMPNO, LASTNAME, E.WORKDEPT, SALARY
FROM EMPLOYEE E INNER JOIN
(SELECT WORKDEPT,
AVG(SALARY) AS AVG_SALARY
FROM EMPLOYEE
GROUP BY WORKDEPT
) AS D
ON E.WORKDEPT = D.WORKDEPT
WHERE SALARY > AVG_SALARY
Copyright IBM Corporation 2007, 2013

Figure 9-8. General guidelines - Correlated subqueries

CE1319.4

Notes:

Correlated subqueries tend to perform quite poorly. The first example illustrates why.

Ex

cl

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.

pr

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

9-14 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

situation, if the EMPLOYEE table contains a million rows, the subquery will be executed a
million times.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

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.

Copyright IBM Corp. 2007, 2013

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-15

Student Notebook

General guidelines Minimize DB2 sorts


EXEC SQL

A
FEW
KEYSTROKES

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

>

I/O
CPU

ORDER BY

TABLE

INDEX ON WORKDEPT

WORKDEPT SALARY

...
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

EXEC SQL

VALUE

RID

GROUP BY WORKDEPT

DB2 MAY USE AN


INDEX TO AVOID
AN INTERNAL SORT

Copyright IBM Corporation 2007, 2013

Figure 9-9. General guidelines - Minimize DB2 sorts

CE1319.4

Notes:

Ex

cl

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.

pr

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.

9-16 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

General guidelines Use views

DEPARTMENT

HAAS

SPIFFY COMPUTER SERVICE DIV.

KWAN

INFORMATION CENTER

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

NAME

VIEW

EMP_DEPT View

SUBSET

EMPNO LASTNAME

WORKDEPT

000010 HAAS

A00

SALAR
Y
52750.00

000030 KWAN

C01

38250.00

000120 O'CONNELL

A00

29250.00

000130 QUINTANA

C01

23800.00

000140 NICHOLLS

C01

28420.00

JOIN

EMPLOYEE Table

DEPTNO DEPTNAME
SPIFFY COMPUTER SERVICE DIV.
A00

BASE TABLES

C01

INFORMATION CENTER

D01

DEVELOPMENT CENTER

MGRNO
000010
000030

DEPARTMENT Table

Copyright IBM Corporation 2007, 2013

Figure 9-10. General guidelines - Use views

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-17

Student Notebook

General guidelines Expressions

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Given:
WORKDEPT is a column of table EMPLOYEE
There is a nonunique index on column WORKDEPT
Example 1:

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE SUBSTR (WORKDEPT, 1, 2) = 'A0'

Example 2:

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE WORKDEPT LIKE 'A0%'

Avoid expressions in predicates whenever possible


Copyright IBM Corporation 2007, 2013

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.

Ex

cl

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.

pr

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.

9-18 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

General guidelines NOT EQUAL Predicates

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Given:
WORKDEPT is a column of table EMPLOYEE
All employees belong to departments A00 or C01
There is a nonunique index on column WORKDEPT
Example 1:

SELECT EMPNO, LASTNAME, SALARY


FROM EMPLOYEE
WHERE WORKDEPT <> 'A00'

Example 2:

SELECT EMPNO, LASTNAME, SALARY


FROM EMPLOYEE
WHERE WORKDEPT = 'C01'

NOT predicates may not be very efficient


Copyright IBM Corporation 2007, 2013

Figure 9-12. General guidelines - NOT EQUAL Predicates

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-19

Student Notebook

General guidelines Arithmetic

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Given:
SALARY is a column of table EMPLOYEE
There is a nonunique index on column SALARY
Example 1:

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE SALARY+2000 > 40000

Example 2:

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE SALARY > 38000

Avoid arithmetic in predicates whenever possible


Copyright IBM Corporation 2007, 2013

Figure 9-13. General guidelines - Arithmetic

CE1319.4

Notes:

cl

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.

pr

Ex

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.

9-20 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

General guidelines Search value length

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Given:
WORKDEPT is a column of table EMPLOYEE
WORKDEPT is defined as CHAR(3)
There is a nonunique index on column WORKDEPT
Example 1:

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE WORKDEPT = 'A00

'

Example 2:

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE WORKDEPT = 'A00'

Avoid making search values longer than the searched


column whenever possible
Copyright IBM Corporation 2007, 2013

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.

pr

Ex

cl

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

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-21

Student Notebook

General guidelines Conversions

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Given:
EDLEVEL is a column of table EMPLOYEE
EDLEVEL is defined as SMALLINT
There is a nonunique index on EDLEVEL
Example 1:

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE EDLEVEL > 15.00

Example 2:

SELECT EMPNO, LASTNAME


FROM EMPLOYEE
WHERE EDLEVEL > 15

Avoid using search values that have different data types


from searched column whenever possible
Copyright IBM Corporation 2007, 2013

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.

pr

Ex

cl

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.

9-22 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

General guidelines Retrieve only necessary data

SELECT

WHERE condition-1

AND condition-2
OR condition-3

Copyright IBM Corporation 2007, 2013

Figure 9-16. General guidelines - Retrieve only necessary data

CE1319.4

Notes:

cl

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.

pr

Ex

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

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-23

Student Notebook

General guidelines - Statements


Return only the columns that are actually needed

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Use local predicates

Write joins if equivalent to subquery

UNION ALL performs better than UNION

Use Evaluate at Open subqueries when possible

Copyright IBM Corporation 2007, 2013

Figure 9-17. General guidelines - Statements

CE1319.4

pr

Ex

cl

Notes:

9-24 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0
Student Notebook

Uempty

Checkpoint
How much data must DB2 read to satisfy a WHERE
condition if no index is available?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.

2.

What kinds of predicates can be evaluated very efficiently?

3.

Why will a correlated subquery sometimes perform poorly?

Copyright IBM Corporation 2007, 2013

Figure 9-18. Checkpoint

CE1319.4

Notes:

Please write your answers here:

Ex

cl

1.

pr

2.

3.

Copyright IBM Corp. 2007, 2013

Unit 9. SQL and DB2 Performance

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

9-25

Student Notebook

Unit summary
Having completed this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After completing this unit, you should be able to:

Explain some basic principles of DB2 performance

Code SQL statements to obtain better performance

Copyright IBM Corporation 2007, 2013

Figure 9-19. Unit summary

CE1319.4

pr

Ex

cl

Notes:

9-26 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0.1
Student Notebook

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Appendix A. Checkpoint Solutions

pr

Ex

AP

Copyright IBM Corp. 2007, 2013

Appendix A. Checkpoint Solutions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

A-1

Student Notebook

Unit 1. Introduction

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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

pr

Ex

cl

Copyright IBM Corporation 2007, 2013

A-2

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0.1
Student Notebook

Unit 2. Create Objects

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

cl

Copyright IBM Corporation 2007, 2013

pr

Ex

AP

Copyright IBM Corp. 2007, 2013

Appendix A. Checkpoint Solutions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

A-3

Student Notebook

Unit 3. Join

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

Copyright IBM Corporation 2007, 2012

A-4

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0.1
Student Notebook

Unit 4. CASE, CAST, Summary Tables and Temporary tables

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Checkpoint Solutions
1. True
2. True

cl

Copyright IBM Corporation 2007, 2013

pr

Ex

AP

Copyright IBM Corp. 2007, 2013

Appendix A. Checkpoint Solutions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

A-5

Student Notebook

Unit 5. Using Subqueries

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Checkpoint Solutions
1. ALL, ANY and SOME
2. True

3. A correlated subquery

pr

Ex

cl

Copyright IBM Corporation 2007, 2012

A-6

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0.1
Student Notebook

Unit 6. Scalar Functions

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

cl

Copyright IBM Corporation 2007, 2012

pr

Ex

AP

Copyright IBM Corp. 2007, 2013

Appendix A. Checkpoint Solutions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

A-7

Student Notebook

Unit 7. Table Expressions and Recursive SQL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

Copyright IBM Corporation 2007, 2012

A-8

DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V7.0.1
Student Notebook

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

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Checkpoint Solutions
1. False. They must be compared to compatible data types.
2. False. UDFs and SPs 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.

cl

Copyright IBM Corporation 2007, 2012

pr

Ex

AP

Copyright IBM Corp. 2007, 2013

Appendix A. Checkpoint Solutions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

A-9

Student Notebook

Unit 9. SQL and DB2 Performance

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

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.

pr

Ex

cl

Copyright IBM Corporation 2007, 2013

A-10 DB2 SQL Workshop for Experienced Users

Copyright IBM Corp. 2007, 2013

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

backpg

pr

cl

Ex
V6.0

Back page

pr

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

cl

Ex

CONTACTO
Telfono
91 761 21 78
Pngase en contacto con nuestro equipo y le
informaremos de cualquier duda o cuestin
que pueda surgirle.

Email
formacion@arrowecs.es
Mndenos un email y le atenderemos
enseguida.

Online
@Arrow_Edu_ES
O bien puede contactarnos a travs de
nuestro perfil en Twitter.

Vistenos
Arrow ECS Education Services
Avenida de Europa 21,
Parque Empresarial La Moraleja
28108 Alcobendas, Madrid

EDUCATION
S

Você também pode gostar