You are on page 1of 104

DB2 SQL Workshop

(Course Code CF12)

Instructor Exercises Guide


ERC3.0

IBM Learning Services


Worldwide Certified Material
Instructor Exercises Guide

Publishing Information

This publication has been produced using BookMaster (Program Number


5668-015), the Document Composition Facility (Program Number 5748-XX9), and
Freelance 97 for Windows. It was printed on the IBM 3820 Page Printer.

Trademarks

IBM is a registered trademark of International Business Machines Corporation.

The following are trademarks International Business Machines Corporation in the


United States, or other countries, or both:

BookMaster DB2 OS/390


QMF

Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft
Corporation in the United States, other countries, or both.

UNIX is a registered trademark in the United States and other countries licensed
exclusively through The Open Group.

Other company, product, and service names may be trademarks or service marks of
others.

December, 2000

The information contained in this document has not been submitted to any formal IBM test and is
distributed on an “as is” basis without any warranty either express or implied. The use of this
information or the implementation of any of these techniques is a customer responsibility and depends
on the customer's ability to evaluate and integrate them into the customer's operational environment.
While each item may have been reviewed by IBM for accuracy in a specific situation, there is no
guarantee that the same or similar results will result elsewhere. Customers attempting to adapt these
techniques to their own environments do so at their own risk. The original repository material for this
course has been certified as being Year 2000 compliant.

 Copyright International Business Machines Corporation 1999, 2000. All rights reserved.
This document may not be reproduced in whole or in part without the prior written permission of IBM.
Note to U.S. Government Users — Documentation related to restricted rights — Use, duplication or
disclosure is subject to restrictions set forth in GSA ADP Schedule Contract with IBM Corp.
Instructor Exercises Guide

Contents
Instructor Exercises Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v

Exercises Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii

Table Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Table Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii

Exercises
Exercise 1. Simple SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1

Exercise 2. Retrieving Data from Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . 2-1

Exercise 3. Scalar Functions and Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . 3-1

Exercise 4. Column Functions and Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . 4-1

Exercise 5. UNION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-1

Exercise 6. Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-1

Exercise 7. Maintaining Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-1

 Copyright IBM Corp. 1999, 2000 Contents iii


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

iv DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Instructor Exercises Overview


Students are not required to do all of the problems in an exercise.
They should complete as many problems as they can within the
time allotted.

 Copyright IBM Corp. 1999, 2000 Instructor Exercises Overview v


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

vi DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Exercises Description
Read the General Information section and the description of the
lab tables before attempting the exercises.

Feel free to consult with the instructor if you need a hint or


encounter difficulties while you are formulating a query.

Problem List — Contains a listing of the problems for that exercise.


You may complete the lab problems from this listing.

Expected Results — repeats the problem and shows the results


from the completed query. In most cases, the complete result is
shown. In cases where the result set is too large, an ellipsis (...) is
shown to indicate that there are additional rows in the result. The
number of rows for the result set is shown so you may verify your
results.

Solutions — contains the query that solves the problem. There are
sometimes several possible solutions. Your solution may differ
from the one shown. If you have any doubts about whether your
solution is correct or if it is as good as the given solution from a
performance point of view, please consult your instructor.

General Information
This lab guide provides the information necessary to complete the
required and optional labs for the DB2 SQL Workshop course.

In countries that provide several query tools for each environment,


this lab guide may be supplemented by additional documentation
describing the basics of each query tool.

The exercises for this course may be done in OS/390 or in a


Workstation environment. Depending on the class location, any of
the following Workstation platforms may be available: OS/2, AIX,
Windows 95 or Windows NT. Read the instructions for your
operating system before attempting the exercises. All Workstation
platforms use the same instructions.

You may use any of the query tools installed on the operating
system. Please note that the emphasis of this course is on
teaching SQL, not on tool use. Therefore, only the basics of the
tool use is covered. For additional information about these tools,
ask the instructor for recommendations on manuals to read or
courses to take for this purpose.

Although saving your queries is not required, feel free to comment


out or save your solutions.

 Copyright IBM Corp. 1999, 2000 Exercises Description vii


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

Full solutions are provided for every question but if you wish to
save your own solutions to diskette or print them, ask your
instructor when you begin the first exercise. Diskettes or printers
may not be available in every class.

viii DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

General Information for OS/390 Users


There are a variety of query tools that can work with DB2 for
OS/390, including SPUFI, QMF, and others. You may use any of
the tools that are installed to do the labs. The following query
tools are available for this course:
__________________________
__________________________
__________________________

The instructor will tell you how to logon to the OS/390 system and
what specific user ID and password to use. In addition, you will
probably be given a team number: this number will be used
occasionally during the exercises so that you can distinguish your
files or objects from those of other students.
Your logon ID is __________________________________________.
Your password is __________________________________________.
Your team number is _______________________________________.
The instructor will give you brief verbal or written instructions on
how to use the query tool you choose. For more information about
a tool, for example how to save a query, be sure to ask the
instructor.

For SPUFI, you will need a partitioned dataset in order to


complete the exercises. This dataset already exists.
The dataset name is ________________________________________.
Each of the tables described in the Table Relationships section of
this document already exists and will be accessed through views
owned by your lab ID.
Your lab ID is _______________.
Within your SQL it is not necessary to prefix the table names with
its qualifier.

When you create TESTEMP table during the exercise 7, you may
need to add an IN clause to your CREATE TABLE statements that
identifies where the table is being created. The IN clause you
need to add is:
IN DBCF12xx.TSxx (where the xx is your team number).
You are not required to save your SQL but if you want to save it,
you may put it in your partitioned dataset.

 Copyright IBM Corp. 1999, 2000 Exercises Description ix


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

General Information for Workstation Users


There are a variety of query tools that can work with DB2 on the
workstation platforms including Command Center, CLP, QMF for
Windows, and others. You may use any of the installed tools to do
the labs. The following query tools are available for this course:
__________________________
__________________________
__________________________

Students doing the exercises on workstation platforms do not need


a team number. Each workstation student has a complete
standalone environment and will not share any files or tables with
other students. Each student will need to sign on with the
following information:
USERID is _____________________
PASSWORD is ____________________
The instructor will give you brief verbal or written instructions on
how to use the tool you choose so that you know enough to get
started. If you want to know more about a tool, for example how
to save a query, be sure to ask the instructor.

A database named sample has already been created for you.


to connect in a single-user environment, enter the following at
a command prompt:
db2 connect to sample
To connect in a client/server environment, enter the following
at a command prompt, substituting the userid and password
you were assigned:
db2 connect to sample user userid using password
Each of the tables described in the Table Relationships section of
this document already exists and has the same qualifier.
The qualifier is _________________.
When you refer to these tables in your SQL, you will need to prefix
the table names with the qualifier identified above. For example, if
you want to display the complete contents of the EMPLOYEE table,
enter the following, substituting the qualifier you were given:
SELECT * FROM qualifier.EMPLOYEE
In cases where you are using your TESTEMP table you need to
omit the qualifier or use your logon ID as qualifier. For example, if
you are trying to read the TESTEMP table, you will need to write:

x DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

SELECT * FROM TESTEMP


or SELECT * FROM logon ID.TESTEMP
If you choose to do the labs in CLP, you may create your SQL in
the \CF12 directory.

 Copyright IBM Corp. 1999, 2000 Exercises Description xi


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

Figure 0-1. Table Relationships (CF12X010)

Notes:
This diagram illustrates the relationships between the tables used in the exercises for
this course. The lines show the connection between the tables.

xii DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

 Copyright IBM Corp. 1999, 2000 Exercises Description xiii


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

xiv DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Table Descriptions
The tables are described in hierarchical order, as shown in the Table Relationships on
the previous page.

DEPARTMENT

There is one row in the DEPARTMENT table for each department in the company.

Column Name Meaning Data Type NULLS allowed


DEPTNO Department Number CHAR(3) N
DEPTNAME Department Name VARCHAR(36) N
MGRNO Employee Number of the Responsible CHAR(6) Y
Manager
ADMRDEPT Department Number of the CHAR(3) N
Department to which the Department
reports
LOCATION Location Number CHAR(5) Y

EMPLOYEE

There is one row in the EMPLOYEE table for each of the employees in the company.

Column Name Meaning Data Type NULLS allowed


EMPNO Employee Number CHAR(6) N
FIRSTNME First Name VARCHAR(20) N
MIDINIT Middle Initial CHAR(1) N
LASTNAME Last Name VARCHAR(15) N
WORKDEPT Department in which the Employee CHAR(3) Y
Works
PHONENO Phone Number CHAR(4) Y
HIREDATE Date of Hire DATE Y
JOB Job CHAR(8) Y
EDLEVEL Number of Years of Formal Education SMALLINT Y
SEX Sex (M male, F female) CHAR(1) Y
BIRTHDATE Date of Birth DATE Y
SALARY Yearly Salary DECIMAL(9, 2) Y
BONUS Yearly Bonus DECIMAL(9, 2) Y
COMM Yearly Commission DECIMAL(9, 2) Y

 Copyright IBM Corp. 1999, 2000 Table Descriptions xv


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

PROJECT

There is one row in the PROJECT table for each project.

Column Name Meaning Data Type NULLS allowed


PROJNO Project Number CHAR(6) N
PROJNAME Project Name VARCHAR(24) N
DEPTNO Responsible Department CHAR(3) N
RESPEMP Employee Number of the Responsible CHAR(6) N
Employee
PRSTAFF Estimated Mean Staffing DECIMAL(5, 2) Y
PRSTDATE Estimated Start Date DATE Y
PRENDATE Estimated End Date DATE Y
MAJPROJ Major Project for a Subproject CHAR(6) Y

EMP_ACT

There are many rows in the EMP_ACT table for any employee or any project.

Column Name Meaning Data Type NULLS allowed


EMPNO Employee Number of Employee CHAR(6) N
Performing the Activity
PROJNO Project Number CHAR(6) N
ACTNO Activity Number SMALLINT N
EMPTIME Proportion of Employee's Time Spent DECIMAL(5, 2) Y
on Project
EMSTDATE Date Activity Starts DATE Y
EMENDATE Date Activity Ends DATE Y

xvi DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Table Content
DEPARTMENT table

DEPTNO DEPTNAME MGRNO ADMRDEPT LOCATION


------ ------------------------------ ------ -------- --------
A00 SPIFFY COMPUTER SERVICE DIV. 000010 A00
B01 PLANNING 000020 A00
C01 INFORMATION CENTER 000030 A00
D01 DEVELOPMENT CENTER - A00
D11 MANUFACTURING SYSTEMS 000060 D01
D21 ADMINISTRATION SYSTEMS 000070 D01
E01 SUPPORT SERVICES 000050 A00
E11 OPERATIONS 000090 E01
E21 SOFTWARE SUPPORT 000100 E01

 Copyright IBM Corp. 1999, 2000 Table Content xvii


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

EMPLOYEE table

EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE


------ ---------- ------- ---------- -------- ------- ----------
000010 CHRISTINE I HAAS A00 3978 1965-01-01
000020 MICHAEL L THOMPSON B01 3476 1973-10-10
000030 SALLY A KWAN C01 4738 1975-04-05
000050 JOHN B GEYER E01 6789 1949-08-17
000060 IRVING F STERN D11 6423 1973-09-14
000070 EVA D PULASKI D21 7831 1980-09-30
000090 EILEEN W HENDERSON E11 5498 1970-08-15
000100 THEODORE Q SPENSER E21 0972 1980-06-19
000110 VINCENZO G LUCCHESI A00 3490 1958-05-16
000120 SEAN O'CONNELL A00 2167 1963-12-05
000130 DOLORES M QUINTANA C01 4578 1971-07-28
000140 HEATHER A NICHOLLS C01 1793 1976-12-15
000150 BRUCE ADAMSON D11 4510 1972-02-12
000160 ELIZABETH R PIANKA D11 3782 1977-10-11
000170 MASATOSHI J YOSHIMURA D11 2890 1978-09-15
000180 MARILYN S SCOUTTEN D11 1682 1973-07-07
000190 JAMES H WALKER D11 2986 1974-07-26
000200 DAVID BROWN D11 4501 1966-03-03
000210 WILLIAM T JONES D11 0942 1979-04-11
000220 JENNIFER K LUTZ D11 0672 1968-08-29
000230 JAMES J JEFFERSON D21 4265 1966-11-21
000240 SALVATORE M MARINO D21 3780 1979-12-05
000250 DANIEL S SMITH D21 0961 1969-10-30
000260 SYBIL V JOHNSON D21 8953 1975-09-11
000270 MARIA L PEREZ D21 9001 1980-09-30
000280 ETHEL R SCHNEIDER E11 8997 1967-03-24
000290 JOHN R PARKER E11 4502 1980-05-30
000300 PHILIP X SMITH E11 2095 1972-06-19
000310 MAUDE F SETRIGHT E11 3332 1964-09-12
000320 RAMLAL V MEHTA E21 9990 1965-07-07
000330 WING LEE E21 2103 1976-02-23
000340 JASON R GOUNOT E21 5698 1947-05-05

xviii DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

EMPLOYEE table (cont'd)

JOB EDLEVEL SEX BIRTHDATE SALARY BONUS COMM


--------- ------- --- ---------- --------- -------- --------
PRES 18 F 1933-08-14 52750.00 1000.00 4220.00
MANAGER 18 M 1948-02-02 41250.00 800.00 3300.00
MANAGER 20 F 1941-05-11 38250.00 800.00 3060.00
MANAGER 16 M 1925-09-15 40175.00 800.00 3214.00
MANAGER 16 M 1945-07-07 32250.00 600.00 2580.00
MANAGER 16 F 1953-05-26 36170.00 700.00 2893.00
MANAGER 16 F 1941-05-15 29750.00 600.00 2380.00
MANAGER 14 M 1956-12-18 26150.00 500.00 2092.00
SALESREP 19 M 1929-11-05 46500.00 900.00 3720.00
CLERK 14 M 1942-10-18 29250.00 600.00 2340.00
ANALYST 16 F 1925-09-15 23800.00 500.00 1904.00
ANALYST 18 F 1946-01-19 28420.00 600.00 2274.00
DESIGNER 16 M 1947-05-17 25280.00 500.00 2022.00
DESIGNER 17 F 1955-04-12 22250.00 400.00 1780.00
DESIGNER 16 M 1951-01-05 24680.00 500.00 1974.00
DESIGNER 17 F 1949-02-21 21340.00 500.00 1707.00
DESIGNER 16 M 1952-06-25 20450.00 400.00 1636.00
DESIGNER 16 M 1941-05-29 27740.00 600.00 2217.00
DESIGNER 17 M 1953-02-23 18270.00 400.00 1462.00
DESIGNER 18 F 1948-03-19 29840.00 600.00 2387.00
CLERK 14 M 1935-05-30 22180.00 400.00 1774.00
CLERK 17 M 1954-03-31 28760.00 600.00 2301.00
CLERK 15 M 1939-11-12 19180.00 400.00 1534.00
CLERK 16 F 1936-10-05 17250.00 300.00 1380.00
CLERK 15 F 1953-05-26 27380.00 500.00 2190.00
OPERATOR 17 F 1936-03-28 26250.00 500.00 2100.00
OPERATOR 12 M 1946-07-09 15340.00 300.00 1227.00
OPERATOR 14 M 1936-10-27 17750.00 400.00 1420.00
OPERATOR 12 F 1931-04-21 15900.00 300.00 1272.00
FIELDREP 16 M 1932-08-11 19950.00 400.00 1596.00
FIELDREP 14 M 1941-07-18 25370.00 500.00 2030.00
FIELDREP 16 M 1926-05-17 23840.00 500.00 1907.00

 Copyright IBM Corp. 1999, 2000 Table Content xix


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

PROJECT table

DEPT PR
PROJNO PROJNAME NO RESPEMP STAFF PRSTDATE PRENDATE MAJPROJ
------ --------------------------------- ----- ---------- ---------- -------
AD3100 ADMIN SERVICES D01 000010 6.50 1982-01-01 1983-02-01 -
AD3110 GENERAL AD SYSTEMS D21 000070 6.00 1982-01-01 1983-02-01 AD3100
AD3111 PAYROLL PROGRAMMING D21 000230 2.00 1982-01-01 1983-02-01 AD3110
AD3112 PERSONNEL PROGRAMMG D21 000250 1.00 1982-01-01 1983-02-01 AD3110
AD3113 ACCOUNT.PROGRAMMING D21 000270 2.00 1982-01-01 1983-02-01 AD3110
IF1000 QUERY SERVICES C01 000030 2.00 1982-01-01 1983-02-01 -
IF2000 USER EDUCATION C01 000030 1.00 1982-01-01 1983-02-01 -
MA2100 WELD LINE AUTOMATION D01 000010 12.00 1982-01-01 1983-02-01 -
MA2110 W L PROGRAMMING D11 000060 9.00 1982-01-01 1983-02-01 MA2100
MA2111 W L PROGRAM DESIGN D11 000220 2.00 1982-01-01 1982-12-01 MA2110
MA2112 W L ROBOT DESIGN D11 000150 3.00 1982-01-01 1982-12-01 MA2110
MA2113 W L PROD CONT PROGS D11 000160 3.00 1982-02-15 1982-12-01 MA2110
OP1000 OPERATION SUPPORT E01 000050 6.00 1982-01-01 1983-02-01 -
OP1010 OPERATION E11 000090 5.00 1982-01-01 1983-02-01 OP1000
OP2000 GEN SYSTEMS SERVICES E01 000050 5.00 1982-01-01 1983-02-01 -
OP2010 SYSTEMS SUPPORT E21 000100 4.00 1982-01-01 1983-02-01 OP2000
OP2011 SCP SYSTEMS SUPPORT E21 000320 1.00 1982-01-01 1983-02-01 OP2010
OP2012 APPLICATIONS SUPPORT E21 000330 1.00 1982-01-01 1983-02-01 OP2010
OP2013 DB/DC SUPPORT E21 000340 1.00 1982-01-01 1983-02-01 OP2010
PL2100 WELD LINE PLANNING B01 000020 1.00 1982-01-01 1982-09-15 MA2100

xx DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

EMP_ACT table

EMPNO PROJNO ACTNO EMPTIME EMSTDATE EMENDATE


------ ------ ------ -------- ---------- ----------
000010 AD3100 10 0.50 1982-01-01 1982-07-01
000070 AD3110 10 1.00 1982-01-01 1983-02-01
000230 AD3111 60 1.00 1982-01-01 1982-03-15
000230 AD3111 60 0.50 1982-03-15 1982-04-15
000230 AD3111 70 0.50 1982-03-15 1982-10-15
000230 AD3111 80 0.50 1982-04-15 1982-10-15
000230 AD3111 180 1.00 1982-10-15 1983-01-01
000240 AD3111 70 1.00 1982-02-15 1982-09-15
000240 AD3111 80 1.00 1982-09-15 1983-01-01
000250 AD3112 60 0.50 1982-02-01 1982-03-15
000250 AD3112 60 1.00 1982-01-01 1982-02-01
000250 AD3112 60 1.00 1983-01-01 1983-02-01
000250 AD3112 60 0.50 1982-12-01 1983-01-01
000250 AD3112 70 1.00 1982-03-15 1982-08-15
000250 AD3112 70 0.50 1982-02-01 1982-03-15
000250 AD3112 70 0.25 1982-08-15 1982-10-15
000250 AD3112 80 0.25 1982-08-15 1982-10-15
000250 AD3112 80 0.50 1982-10-15 1982-12-01
000250 AD3112 180 0.50 1982-08-15 1983-01-01
000260 AD3113 70 0.50 1982-06-15 1982-07-01
000260 AD3113 70 1.00 1982-07-01 1983-02-01
000260 AD3113 80 1.00 1982-01-01 1982-03-01
000260 AD3113 80 0.50 1982-03-01 1982-04-15
000260 AD3113 180 0.50 1982-03-01 1982-04-15
000260 AD3113 180 0.50 1982-06-01 1982-07-01
000260 AD3113 180 1.00 1982-04-15 1982-06-01
000270 AD3113 60 0.25 1982-09-01 1982-10-15
000270 AD3113 60 1.00 1982-04-01 1982-09-01
000270 AD3113 60 0.50 1982-03-01 1982-04-01
000270 AD3113 70 0.75 1982-09-01 1982-10-15
000270 AD3113 70 1.00 1982-10-15 1983-02-01
000270 AD3113 80 1.00 1982-01-01 1982-03-01
000270 AD3113 80 0.50 1982-03-01 1982-04-01
000030 IF1000 10 0.50 1982-06-01 1983-01-01
000130 IF1000 90 1.00 1982-01-01 1982-10-01
000130 IF1000 100 0.50 1982-10-01 1983-01-01
000140 IF1000 90 0.50 1982-10-01 1983-01-01

 Copyright IBM Corp. 1999, 2000 Table Content xxi


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

EMP_ACT table (cont'd)

EMPNO PROJNO ACTNO EMPTIME EMSTDATE EMENDATE


------ ------ ------ -------- ---------- ----------
000030 IF2000 10 0.50 1982-01-01 1983-01-01
000140 IF2000 100 1.00 1982-01-01 1982-03-01
000140 IF2000 100 0.50 1982-03-01 1982-07-01
000140 IF2000 110 0.50 1982-03-01 1982-07-01
000140 IF2000 110 0.50 1982-10-01 1983-01-01
000010 MA2100 10 0.50 1982-01-01 1982-11-01
000110 MA2100 20 1.00 1982-01-01 1982-03-01
000010 MA2110 10 1.00 1982-01-01 1983-02-01
000200 MA2111 50 1.00 1982-01-01 1982-06-15
000200 MA2111 60 1.00 1982-06-15 1983-02-01
000220 MA2111 40 1.00 1982-01-01 1983-02-01
000150 MA2112 60 1.00 1982-01-01 1982-07-15
000150 MA2112 180 1.00 1982-07-15 1983-02-01
000170 MA2112 60 1.00 1982-01-01 1983-06-01
000170 MA2112 70 1.00 1982-06-01 1983-02-01
000190 MA2112 70 1.00 1982-02-01 1982-10-01
000190 MA2112 80 1.00 1982-10-01 1983-10-01
000160 MA2113 60 1.00 1982-07-15 1983-02-01
000170 MA2113 80 1.00 1982-01-01 1983-02-01
000180 MA2113 70 1.00 1982-04-01 1982-06-15
000210 MA2113 80 0.50 1982-10-01 1983-02-01
000210 MA2113 180 0.50 1982-10-01 1983-02-01
000050 OP1000 10 0.25 1982-01-01 1983-02-01
000090 OP1010 10 1.00 1982-01-01 1983-02-01
000280 OP1010 130 1.00 1982-01-01 1983-02-01
000290 OP1010 130 1.00 1982-01-01 1983-02-01
000300 OP1010 130 1.00 1982-01-01 1983-02-01
000310 OP1010 130 1.00 1982-01-01 1983-02-01
000050 OP2010 10 0.75 1982-01-01 1983-02-01
000100 OP2010 10 1.00 1982-01-01 1983-02-01
000320 OP2011 140 0.75 1982-01-01 1983-02-01
000320 OP2011 150 0.25 1982-01-01 1983-02-01
000330 OP2012 140 0.25 1982-01-01 1983-02-01
000330 OP2012 160 0.75 1982-01-01 1983-02-01
000340 OP2013 140 0.50 1982-01-01 1983-02-01
000340 OP2013 170 0.50 1982-01-01 1983-02-01
000020 PL2100 30 1.00 1982-01-01 1982-09-15

xxii DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Each exercise in this course is divided into sections as described below. Select the
section that best fits your method of performing labs. You may elect to use a
combination of these sections as appropriate.

Problem List — This section contains what it is you are to accomplish. There are no
definitive details on how to perform the tasks. You are given the opportunity to work
through the exercise given what you learned in the unit presentation, utilizing the unit
Student Notebook, your past experience and maybe a little intuition.

Problem List With Expected Results — This section is an exact duplicate of the
Problem List section except that it includes the expected result of the query you are to
write.

Feel free to use the Problem List or the Problem List with Expected Results when
doing your lab problems.

Solutions — Follow the Problem List With Expected Results.

 Copyright IBM Corp. 1999, 2000 Table Content xxiii


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

xxiv DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Exercise 1. Simple SQL Queries

What This Exercise is About


This exercise provides an opportunity to write and execute basic
SQL statements.

What You Should Be Able to Do


At the end of the lab, you should be able to:
Code SELECT statements using four clauses of an SQL SELECT
statement.
Use a SELECT statement to:
− Retrieve all rows of a table
− Retrieve specific columns
− Retrieve rows based on comparisons to numeric or
alphanumeric data
− Retrieve rows based on specific columns containing NULL
values
Code SELECT statements using keywords BETWEEN, IN, LIKE
and DISTINCT
Produce a result table whose rows are sorted in a desired
sequence

Introduction
See the data model at the start of this exercise guide to get the
column names and descriptions for each table.

Required Materials
Student handout
SQL Reference

Instructor Exercise Overview


The students are not required to finish all problems. The student's experience and
skill level will affect the number of problems they can complete. Hopefully there are
more problems than can be completed within the time allotted so the experienced
students will be as challenged as the novice.

 Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 1-1
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem List

Problem 1
List employee number, last name, date of birth, and salary for all
employees who make more than $30,000 a year. Sequence the results
in descending order by salary.
Problem 2
List last name, first name, and the department number for all
employees. The listing should be ordered by descending department
numbers. Within the same department, the last names should be sorted
in descending order.
Problem 3
List the different education levels in the company in descending order.
List only one occurrence of duplicate result rows.
Problem 4
List employees, by employee number, and their assigned projects, by
project number. Display only those employees with an employee
number less than or equal to 100. List only one occurrence of duplicate
rows. Sort the result rows by employee number.
(Use the EMP_ACT table.)
Problem 5
List last name, salary, and bonus of all male employees.
Problem 6
List last name, salary, and commission for all employees with a salary
greater than $20,000 and hired after 1979.
Problem 7
List last name, salary, bonus, and commission for all employees with a
salary greater than $22,000 and a bonus of $400, or for all employees
with a bonus of $500 and a commission lower than $1,900. The list
should be ordered by last name.
Problem 8
List last name, salary, bonus, and commission for all employees with a
salary greater than $22,000, a bonus of $400 or $500, and a commission
less than $1,900. The list should be ordered by last name.
Problem 9
Using the EMP_ACT table, for all projects that have a project number
beginning with AD and have activities 10, 80, and 180 associated with
them, list the following:
Project number
Activity number
Starting date for activity
Ending date for activity
Order the list by activity number within project number.

1-2 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 10
List manager number and department number for all departments to
which a manager has been assigned.
The list should be ordered by manager number.
Problem 11
List employee number, last name, salary, and bonus for all employees
that have a bonus ranging from $800 to $1,000.
Sort the report by employee number within bonus, lowest bonus first.
Problem 12
List employee number, last name, salary, and department number for all
employees in departments A00 through C01 (inclusive).
Order the results alphabetically by last name and employee number.
Problem 13
List all projects that have SUPPORT as part of the project name. Order
the results by project number.
Problem 14
List all departments that have a 1 as the middle character in the
department number.
Order the results by department number.
Problem 15
List the last name, first name, middle initial, and salary of the five
highest paid non-manager, non-president employees.
Order the results by highest salary first.

END OF LAB

 Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 1-3
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem List With Expected Results

Problem 1
List employee number, last name, date of birth, and salary for all
employees who make more than $30,000 a year. Sequence the results
in descending order by salary.
Result
EMPNO LASTNAME BIRTHDATE SALARY
---------+---------+---------+---------+--------
000010 HAAS 1933-08-14 52750.00
000110 LUCCHESI 1929-11-05 46500.00
000020 THOMPSON 1948-02-02 41250.00
000050 GEYER 1925-09-15 40175.00
000030 KWAN 1941-05-11 38250.00
000070 PULASKI 1953-05-26 36170.00
000060 STERN 1945-07-07 32250.00
Problem 2
List last name, first name, and the department number for all
employees. The listing should be ordered by descending department
numbers. Within the same department, the last names should be sorted
in descending order.
Result
LASTNAME FIRSTNME WORKDEPT
---------+---------+---------+---------+
SPENSER THEODORE E21
MEHTA RAMLAL E21
LEE WING E21
GOUNOT JASON E21
SMITH PHILIP E11
SETRIGHT MAUDE E11
SCHNEIDER ETHEL E11
PARKER JOHN E11
HENDERSON EILEEN E11
GEYER JOHN E01
...... ...... ...
The complete result set contains 32 rows.

1-4 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 3
List the different education levels in the company in descending order.
List only one occurrence of duplicate result rows.
Result
EDLEVEL
-------
20
19
18
17
16
15
14
12
Problem 4
List employees, by employee number, and their assigned projects, by
project number. Display only those employees with an employee
number less than or equal to 100. List only one occurrence of duplicate
rows. Sort the result rows by employee number.
(Use the EMP_ACT table.)
Result

EMPNO PROJNO
---------+----
000010 AD3100
000010 MA2100
000010 MA2110
000020 PL2100
000030 IF1000
000030 IF2000
000050 OP1000
000050 OP2010
000070 AD3110
000090 OP1010
000100 OP2010

 Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 1-5
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem 5
List last name, salary, and bonus of all male employees.
Result
LASTNAME SALARY BONUS
---------+---------+---------+---------+-
THOMPSON 41250.00 800.00
GEYER 40175.00 800.00
STERN 32250.00 600.00
SPENSER 26150.00 500.00
LUCCHESI 46500.00 900.00
O'CONNELL 29250.00 600.00
ADAMSON 25280.00 500.00
YOSHIMURA 24680.00 500.00
WALKER 20450.00 400.00
BROWN 27740.00 600.00
JONES 18270.00 400.00
JEFFERSON 22180.00 400.00
MARINO 28760.00 600.00
SMITH 19180.00 400.00
PARKER 15340.00 300.00
SMITH 17750.00 400.00
MEHTA 19950.00 400.00
LEE 25370.00 500.00
GOUNOT 23840.00 500.00
Problem 6
List last name, salary, and commission for all employees with a salary
greater than $20,000 and hired after 1979.
Result
LASTNAME SALARY COMM
---------+---------+---------+---------+-
PULASKI 36170.00 2893.00
SPENSER 26150.00 2092.00
PEREZ 27380.00 2190.00

1-6 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 7
List last name, salary, bonus, and commission for all employees with a
salary greater than $22,000 and a bonus of $400, or for all employees
with a bonus of $500 and a commission lower than $1,900. The list
should be ordered by last name.
Result
LASTNAME SALARY BONUS COMM
---------+---------+---------+---------+---------+----
JEFFERSON 22180,00 400,00 1774,00
PIANKA 22250,00 400,00 1780,00
SCOUTTEN 21340,00 500,00 1707,00
Problem 8
List last name, salary, bonus, and commission for all employees with a
salary greater than $22,000, a bonus of $400 or $500, and a commission
less than $1,900. The list should be ordered by last name.
Result
LASTNAME SALARY BONUS COMM
---------+---------+---------+---------+---------+----
JEFFERSON 22180.00 400.00 1774.00
PIANKA 22250.00 400.00 1780.00

 Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 1-7
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem 9
Using the EMP_ACT table, for all projects that have a project number
beginning with AD and have activities 10, 80, and 180 associated with
them, list the following:
Project number
Activity number
Starting date for activity
Ending date for activity
Order the list by activity number within project number.
Result
PROJNO ACTNO EMSTDATE EMENDATE
---------+---------+---------+--------
AD3100 10 1982-01-01 1982-07-01
AD3110 10 1982-01-01 1983-02-01
AD3111 80 1982-04-15 1982-10-15
AD3111 80 1982-09-15 1983-01-01
AD3111 180 1982-10-15 1983-01-01
AD3112 80 1982-08-15 1982-10-15
AD3112 80 1982-10-15 1982-12-01
AD3112 180 1982-08-15 1983-01-01
AD3113 80 1982-01-01 1982-03-01
AD3113 80 1982-01-01 1982-03-01
AD3113 80 1982-03-01 1982-04-15
AD3113 80 1982-03-01 1982-04-01
AD3113 180 1982-03-01 1982-04-15
AD3113 180 1982-04-15 1982-06-01
AD3113 180 1982-06-01 1982-07-01

1-8 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 10
List manager number and department number for all departments to
which a manager has been assigned.
The list should be ordered by manager number.
Result
MGRNO DEPTNO
---------+----
000010 A00
000020 B01
000030 C01
000050 E01
000060 D11
000070 D21
000090 E11
000100 E21
Problem 11
List employee number, last name, salary, and bonus for all employees
that have a bonus ranging from $800 to $1,000.
Sort the report by employee number within bonus, lowest bonus first.
Result
EMPNO LASTNAME SALARY BONUS
------ --------------- ------------ ------------
000020 THOMPSON 41250.00 800.00
000030 KWAN 38250.00 800.00
000050 GEYER 40175.00 800.00
000110 LUCCHESSI 46500.00 900.00
000010 HAAS 52750.00 1000.00

 Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 1-9
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem 12
List employee number, last name, salary, and department number for all
employees in departments A00 through C01 (inclusive).
Order the results alphabetically by last name and employee number.
Result
EMPNO LASTNAME SALARY WORKDEPT
---------+---------+---------+---------+---------+
000010 HAAS 52750.00 A00
000030 KWAN 38250.00 C01
000110 LUCCHESI 46500.00 A00
000140 NICHOLLS 28420.00 C01
000120 O'CONNELL 29250.00 A00
000130 QUINTANA 23800.00 C01
000020 THOMPSON 41250.00 B01
Problem 13
List all projects that have SUPPORT as part of the project name. Order
the results by project number.
Result
PROJNO PROJNAME
---------+---------+---------+---------
OP1000 OPERATION SUPPORT
OP2010 SYSTEMS SUPPORT
OP2011 SCP SYSTEMS SUPPORT
OP2012 APPLICATIONS SUPPORT
OP2013 DB/DC SUPPORT
Problem 14
List all departments that have a 1 as the middle character in the
department number.
Order the results by department number.
Result
DEPTNO DEPTNAME
---------+---------+---------+---------+
D11 MANUFACTURING SYSTEMS
E11 OPERATIONS

1-10 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 15
List the last name, first name, middle initial, and salary of the five
highest paid non-manager, non-president employees.
Order the results by highest salary first.
Result
LASTNAME FIRSTNME MIDINIT SALARY
--------------- ------------ ------- ------------
LUCCHESSI VINCENZO G 46500.00
LUTZ JENNIFER K 29840.00
O'CONNELL SEAN 29250.00
MARINO SALVATORE M 28760.00
NICHOLLS HEATHER A 28420.00

END OF Problem List With Expected Results

 Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 1-11
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Solution

Problem 1
SELECT EMPNO, LASTNAME, BIRTHDATE, SALARY
FROM EMPLOYEE
WHERE SALARY > 30000
ORDER BY SALARY DESC
Problem 2
SELECT LASTNAME, FIRSTNME, WORKDEPT
FROM EMPLOYEE
ORDER BY WORKDEPT DESC, LASTNAME DESC
Problem 3
SELECT DISTINCT EDLEVEL
FROM EMPLOYEE
ORDER BY EDLEVEL DESC
Problem 4
SELECT DISTINCT EMPNO, PROJNO
FROM EMP_ACT
WHERE EMPNO <= '000100'
ORDER BY EMPNO
Problem 5
SELECT LASTNAME, SALARY, BONUS
FROM EMPLOYEE
WHERE SEX = 'M'
Problem 6
SELECT LASTNAME, SALARY, COMM
FROM EMPLOYEE
WHERE HIREDATE >= '1980-01-01'
AND SALARY > 20000
Problem 7
SELECT LASTNAME, SALARY, BONUS, COMM
FROM EMPLOYEE
WHERE SALARY > 22000 AND BONUS = 400
OR BONUS = 500 AND COMM < 1900
ORDER BY LASTNAME

1-12 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 8
SELECT LASTNAME, SALARY, BONUS, COMM
FROM EMPLOYEE
WHERE SALARY > 22000
AND ( BONUS = 400 OR BONUS = 500 )
AND COMM < 1900
ORDER BY LASTNAME
Problem 9
SELECT PROJNO, ACTNO, EMSTDATE, EMENDATE
FROM EMP_ACT
WHERE ACTNO IN (10, 80, 180)
AND PROJNO LIKE 'AD%'
ORDER BY PROJNO, ACTNO
Problem 10
SELECT MGRNO, DEPTNO
FROM DEPARTMENT
WHERE MGRNO IS NOT NULL
ORDER BY MGRNO
Problem 11
SELECT EMPNO, LASTNAME, SALARY, BONUS
FROM EMPLOYEE
WHERE BONUS BETWEEN 800 AND 1000
ORDER BY BONUS, EMPNO
Problem 12
SELECT EMPNO, LASTNAME, SALARY, WORKDEPT
FROM EMPLOYEE
WHERE WORKDEPT BETWEEN 'A00' AND 'C01'
ORDER BY LASTNAME, EMPNO
Problem 13
SELECT PROJNO, PROJNAME
FROM PROJECT
WHERE PROJNAME LIKE '%SUPPORT%'
ORDER BY PROJNO
Problem 14
SELECT DEPTNO, DEPTNAME
FROM DEPARTMENT
WHERE DEPTNO LIKE '_1_'
ORDER BY DEPTNO

 Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 1-13
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem 15
SELECT LASTNAME, FIRSTNME, MIDINIT, SALARY
FROM EMPLOYEE
WHERE JOB NOT IN ('PRES', 'MANAGER')
ORDER BY SALARY DESC
FETCH FIRST 5 ROWS ONLY

END OF SOLUTION

1-14 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Exercise 2. Retrieving Data from Multiple Tables

What This Exercise is About


This exercise provides an opportunity to write queries that retrieve
data from more than one table.

What You Should Be Able to Do


At the end of the lab, you should be able to:
Code SELECT statements referencing multiple tables or views.

Introduction
See the data model at the start of this exercise guide to get the
column names and descriptions for each table.

Required Materials
Student handout
SQL Reference

Instructor Exercise Overview


Remind the students to complete as many of the problems as they can within the
allotted time. It is not necessary that they complete all of the problems.

 Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables 2-1


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

Problem List

Problem 1
Produce a report that lists employees' last names, first names, and
department names. Sequence the report on first name within last name,
within department name.
Problem 2
Modify the previous query to include job. Also, list data for only
departments between A02 and D22, and exclude managers from the list.
Sequence the report on first name within last name, within job, within
department name.
Problem 3
List the name of each department and the lastname and first name of its
manager. Sequence the list by department name. Use the EMPNO and
MGRNO columns to relate the two tables. Sequence the result rows by
department name.
Problem 4
Try the following: modify the previous query using WORKDEPT and
DEPTNO as the join predicate. Include a local predicate that looks for
people whose job is manager.
Are the results from both queries the same? __________________
Why?
___________________________________________________________________
______________________________________________________________________
_____________________________________________________________________
Problem 5
For all projects that have a project number beginning with AD, list
project number, project name, and activity number. List identical rows
once. Order the list by project number and then by activity number.
Problem 6
Which employees are assigned to project number AD3113? List
employee number, last name, and project number. Order the list by
employee number and then by project number. List only one
occurrence of duplicate result rows.

2-2 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 7
Which activities began on October 1, 1982? For each of these activities,
list the employee number of the person performing the activity, the
project number,project name, activity number, and starting date of the
activity. Order the list by project number, then by employee number,
and then by activity number.
Problem 8
Display department number, last name, project name, and activity
number for activities performed by the employees in department A00.
Sequence the results first by project name and then by activity number.
Problem 9
List department number, last name, project name, and activity number
for those employees in work departments A00 through C01. Suppress
identical rows.
Sort the list by department number, last name, and activity number.
Problem 10
The second line manager needs a list of activities which began on
October 15, 1982 or thereafter.
For these activities, list the activity number, the manager number of the
manager of the department assigned to the project, the starting date for
the activity, the project number, and the last name of the employee
performing the activity.
The list should be ordered by the activity number and then by the
activity start date.
Problem 11
Which employees in department A00 were hired before their manager?
List department number, the manager's last name, the employee's last
name, and the hiring dates of both the manager and the employee.
Order the list by the employee's last name.

END OF LAB

 Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables 2-3


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

Problem List With Expected Results

Problem 1
Produce a report that lists employees' last names, first names, and
department names. Sequence the report on first name within last name,
within department name.
RESULT
LASTNAME FIRSTNME DEPTNAME
--------------- ------------ -----------------------------
JEFFERSON JAMES ADMINISTRATION SYSTEMS
JOHNSON SYBIL ADMINISTRATION SYSTEMS
MARINO SALVATORE ADMINISTRATION SYSTEMS
PEREZ MARIA ADMINISTRATION SYSTEMS
PULASKI EVA ADMINISTRATION SYSTEMS
SMITH DANIEL ADMINISTRATION SYSTEMS
KWAN SALLY INFORMATION CENTER
NICHOLLS HEATHER INFORMATION CENTER
QUINTANA DOLORES INFORMATION CENTER
ADAMSON BRUCE MANUFACTURING SYSTEMS
BROWN DAVID MANUFACTURING SYSTEMS
JONES WILLIAM MANUFACTURING SYSTEMS
LUTZ JENNIFER MANUFACTURING SYSTEMS
PIANKA ELIZABETH MANUFACTURING SYSTEMS
SCOUTTEN MARILYN MANUFACTURING SYSTEMS
STERN IRVING MANUFACTURING SYSTEMS
WALKER JAMES MANUFACTURING SYSTEMS
YOSHIMURA MASATOSHI MANUFACTURING SYSTEMS
HENDERSON EILEEN OPERATIONS
PARKER JOHN OPERATIONS
SCHNEIDER ETHEL OPERATIONS
SETRIGHT MAUDE OPERATIONS
SMITH PHILIP OPERATIONS
THOMPSON MICHAEL PLANNING
GOUNOT JASON SOFTWARE SUPPORT
LEE WING SOFTWARE SUPPORT
MEHTA RAMLAL SOFTWARE SUPPORT
SPENSER THEODORE SOFTWARE SUPPORT
HAAS CHRISTINE SPIFFY COMPUTER SERVICE DIV.
LUCCHESSI VINCENZO SPIFFY COMPUTER SERVICE DIV.
O'CONNELL SEAN SPIFFY COMPUTER SERVICE DIV.
GEYER JOHN SUPPORT SERVICES

2-4 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 2
Modify the previous query to include job. Also, list data for only
departments between A02 and D22, and exclude managers from the list.
Sequence the report on first name within last name, within job, within
department name.
RESULT
LASTNAME FIRSTNME DEPTNAME JOB
--------------- ------------ ----------------------------- --------
JEFFERSON JAMES ADMINISTRATION SYSTEMS CLERK
JOHNSON SYBIL ADMINISTRATION SYSTEMS CLERK
MARINO SALVATORE ADMINISTRATION SYSTEMS CLERK
PEREZ MARIA ADMINISTRATION SYSTEMS CLERK
SMITH DANIEL ADMINISTRATION SYSTEMS CLERK
NICHOLLS HEATHER INFORMATION CENTER ANALYST
QUINTANA DOLORES INFORMATION CENTER ANALYST
ADAMSON BRUCE MANUFACTURING SYSTEMS DESIGNER
BROWN DAVID MANUFACTURING SYSTEMS DESIGNER
JONES WILLIAM MANUFACTURING SYSTEMS DESIGNER
LUTZ JENNIFER MANUFACTURING SYSTEMS DESIGNER
PIANKA ELIZABETH MANUFACTURING SYSTEMS DESIGNER
SCOUTTEN MARILYN MANUFACTURING SYSTEMS DESIGNER
WALKER JAMES MANUFACTURING SYSTEMS DESIGNER
YOSHIMURA MASATOSHI MANUFACTURING SYSTEMS DESIGNER

Problem 3
List the name of each department and the lastname and first name of its
manager. Sequence the list by department name. Use the EMPNO and
MGRNO columns to relate the two tables. Sequence the result rows by
department name.
RESULT
DEPTNAME LASTNAME FIRSTNME
----------------------------- --------------- ------------
ADMINISTRATION SYSTEMS PULASKI EVA
INFORMATION CENTER KWAN SALLY
MANUFACTURING SYSTEMS STERN IRVING
OPERATIONS HENDERSON EILEEN
PLANNING THOMPSON MICHAEL
SOFTWARE SUPPORT SPENSER THEODORE
SPIFFY COMPUTER SERVICE DIV. HAAS CHRISTINE
SUPPORT SERVICES GEYER JOHN

 Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables 2-5


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

Problem 4
Try the following: modify the previous query using WORKDEPT and
DEPTNO as the join predicate. Include a local predicate that looks for
people whose job is manager.
Are the results from both queries the same? __________________
Why?
___________________________________________________________________
______________________________________________________________________
_____________________________________________________________________
RESULT
DEPTNAME LASTNAME FIRSTNME
----------------------------- --------------- ------------
ADMINISTRATION SYSTEMS PULASKI EVA
INFORMATION CENTER KWAN SALLY
MANUFACTURING SYSTEMS STERN IRVING
OPERATIONS HENDERSON EILEEN
PLANNING THOMPSON MICHAEL
SOFTWARE SUPPORT SPENSER THEODORE
SUPPORT SERVICES GEYER JOHN
Problem 5
For all projects that have a project number beginning with AD, list
project number, project name, and activity number. List identical rows
once. Order the list by project number and then by activity number.
Result
PROJNO PROJNAME ACTNO
---------+---------+---------+---------+-
AD3100 ADMIN SERVICES 10
AD3110 GENERAL AD SYSTEMS 10
AD3111 PAYROLL PROGRAMMING 60
AD3111 PAYROLL PROGRAMMING 70
AD3111 PAYROLL PROGRAMMING 80
AD3111 PAYROLL PROGRAMMING 180
AD3112 PERSONNEL PROGRAMMG 60
AD3112 PERSONNEL PROGRAMMG 70
AD3112 PERSONNEL PROGRAMMG 80
AD3112 PERSONNEL PROGRAMMG 180
AD3113 ACCOUNT.PROGRAMMING 60
AD3113 ACCOUNT.PROGRAMMING 70
AD3113 ACCOUNT.PROGRAMMING 80
AD3113 ACCOUNT.PROGRAMMING 180

2-6 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 6
Which employees are assigned to project number AD3113? List
employee number, last name, and project number. Order the list by
employee number and then by project number. List only one
occurrence of duplicate result rows.
Result
EMPNO LASTNAME PROJNO
---------+---------+---------+--
000260 JOHNSON AD3113
000270 PEREZ AD3113
Problem 7
Which activities began on October 1, 1982? For each of these activities,
list the employee number of the person performing the activity, the
project number,project name, activity number, and starting date of the
activity. Order the list by project number, then by employee number,
and then by activity number.
Result
EMPNO PROJNO PROJNAME ACTNO EMSTDATE
---------+---------+---------+---------+---------+---------+
000130 IF1000 QUERY SERVICES 100 1982-10-01
000140 IF1000 QUERY SERVICES 90 1982-10-01
000140 IF2000 USER EDUCATION 110 1982-10-01
000190 MA2112 W L ROBOT DESIGN 80 1982-10-01
000210 MA2113 W L PROD CONT PROGS 80 1982-10-01
000210 MA2113 W L PROD CONT PROGS 180 1982-10-01

 Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables 2-7


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

Problem 8
Display department number, last name, project name, and activity
number for activities performed by the employees in department A00.
Sequence the results first by project name and then by activity number.
Result
WORKDEPT LASTNAME PROJNAME ACTNO
---------+---------+---------+---------+---------+---------
A00 HAAS ADMIN SERVICES 10
A00 HAAS W L PROGRAMMING 10
A00 HAAS WELD LINE AUTOMATION 10
A00 LUCCHESI WELD LINE AUTOMATION 20
Problem 9
List department number, last name, project name, and activity number
for those employees in work departments A00 through C01. Suppress
identical rows.
Sort the list by department number, last name, and activity number.
Result
WORKDEPT LASTNAME PROJNAME ACTNO
---------+---------+---+---------+---------+---------
A00 HAAS ADMIN SERVICES 10
A00 HAAS W L PROGRAMMING 10
A00 HAAS WELD LINE AUTOMATION 10
A00 LUCCHESI WELD LINE AUTOMATION 20
B01 THOMPSON WELD LINE PLANNING 30
C01 KWAN QUERY SERVICES 10
C01 KWAN USER EDUCATION 10
C01 NICHOLLS QUERY SERVICES 90
C01 NICHOLLS USER EDUCATION 100
C01 NICHOLLS USER EDUCATION 110
C01 QUINTANA QUERY SERVICES 90
C01 QUINTANA QUERY SERVICES 100

2-8 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 10
The second line manager needs a list of activities which began on
October 15, 1982 or thereafter.
For these activities, list the activity number, the manager number of the
manager of the department assigned to the project, the starting date for
the activity, the project number, and the last name of the employee
performing the activity.
The list should be ordered by the activity number and then by the
activity start date.
Result
ACTNO MGRNO EMSTDATE PROJNO LASTNAME
--------+---------+---------+---------+------
60 000070 1982-12-01 AD3112 SMITH
60 000070 1983-01-01 AD3112 SMITH
70 000070 1982-10-15 AD3113 PEREZ
80 000070 1982-10-15 AD3112 SMITH
180 000070 1982-10-15 AD3111 JEFFERSON
Problem 11
Which employees in department A00 were hired before their manager?
List department number, the manager's last name, the employee's last
name, and the hiring dates of both the manager and the employee.
Order the list by the employee's last name.
Result
DEPTNO MANAGER EMPLOYEE M_HIREDATE E_HIREDATE
---------+---------+---------+---------+---------+-------
A00 HAAS LUCCHESI 1965-01-01 1958-05-16
A00 HAAS O'CONNELL 1965-01-01 1963-12-05

END OF Problem List With Expected Results

 Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables 2-9


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

Solution

Problem 1
SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAME
FROM EMPLOYEE E, DEPARTMENT D
WHERE E.WORKDEPT = D.DEPTNO
ORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME

OR

SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAME


FROM EMPLOYEE E JOIN DEPARTMENT D
ON E.WORKDEPT = D.DEPTNO
ORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME
Problem 2
SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAME, E.JOB
FROM EMPLOYEE E, DEPARTMENT D
WHERE E.WORKDEPT = D.DEPTNO
AND E.WORKDEPT BETWEEN 'A02' AND 'D22'
AND JOB <> 'MANAGER'
ORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME

OR
SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAME, E.JOB
FROM EMPLOYEE E JOIN DEPARTMENT D
ON E.WORKDEPT = D.DEPTNO
WHERE E.WORKDEPT BETWEEN 'A02' AND 'D22'
AND JOB <> 'MANAGER'
ORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME

2-10 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 3
SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNME
FROM DEPARTMENT D, EMPLOYEE E
WHERE D.MGRNO = E.EMPNO
ORDER BY D.DEPTNAME

OR

SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNME


FROM DEPARTMENT D JOIN EMPLOYEE E
ON D.MGRNO = E.EMPNO
ORDER BY D.DEPTNAME

Problem 4
SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNME
FROM DEPARTMENT D, EMPLOYEE E
WHERE D.DEPTNO = E.WORKDEPT
AND JOB = 'MANAGER'
ORDER BY D.DEPTNAME

OR

SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNME


FROM DEPARTMENT D JOIN EMPLOYEE E
ON D.DEPTNO = E.WORKDEPT
WHERE JOB = 'MANAGER'
ORDER BY D.DEPTNAME

The results of the queries in problem 3 and 4 are not the same.
The department
named Spiffy Computer Services Div. is not in the second report. The
reason: the job title of that department's manager is PRES. Our
WHERE clause only allows qualified persons with a job of MANAGER
to be joined with their department rows. It is very important to
learn the data within your tables!

 Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables 2-11


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

Problem 5
SELECT DISTINCT A.PROJNO, PROJNAME, ACTNO
FROM PROJECT P,
EMP_ACT A
WHERE P.PROJNO = A.PROJNO
AND A.PROJNO LIKE 'AD%'
ORDER BY A.PROJNO, ACTNO

or

SELECT DISTINCT A.PROJNO, PROJNAME, ACTNO


FROM PROJECT AS P JOIN EMP_ACT AS A
ON P.PROJNO = A.PROJNO
WHERE A.PROJNO LIKE 'AD%'
ORDER BY A.PROJNO, ACTNO
Problem 6
SELECT DISTINCT A.EMPNO, LASTNAME, PROJNO
FROM EMPLOYEE E,
EMP_ACT A
WHERE A.EMPNO = E.EMPNO
AND A.PROJNO = 'AD3113'
ORDER BY A.EMPNO, PROJNO

or
SELECT DISTINCT A.EMPNO,LASTNAME, PROJNO
FROM EMPLOYEE AS E JOIN EMP_ACT AS A
ON E.EMPNO = A.EMPNO
WHERE A.PROJNO = 'AD3113'
ORDER BY A.EMPNO, PROJNO

2-12 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 7
SELECT A.EMPNO, A.PROJNO, PROJNAME, ACTNO, EMSTDATE
FROM PROJECT P,
EMP_ACT A
WHERE A.PROJNO = P.PROJNO
AND EMSTDATE = '1982-10-01'
ORDER BY A.PROJNO, A.EMPNO, ACTNO

or

SELECT A.EMPNO, A.PROJNO, PROJNAME, ACTNO, EMSTDATE


FROM PROJECT AS P JOIN EMP_ACT AS A
ON P.PROJNO = A.PROJNO
WHERE A.EMSTDATE = '1982-10-01'
ORDER BY A.PROJNO, A.EMPNO, ACTNO
Problem 8
SELECT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNO
FROM EMPLOYEE E, PROJECT P, EMP_ACT A
WHERE E.EMPNO = A.EMPNO
AND A.PROJNO = P.PROJNO
AND E.WORKDEPT = 'A00'
ORDER BY P.PROJNAME, A.ACTNO

OR
SELECT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNO
FROM EMPLOYEE AS E JOIN EMP_ACT AS A
ON E.EMPNO = A.EMPNO
JOIN PROJECT AS P
ON A.PROJNO = P.PROJNO
WHERE E.WORKDEPT = 'A00'
ORDER BY P.PROJNAME, A.ACTNO

 Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables 2-13


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

Problem 9
SELECT DISTINCT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNO
FROM EMPLOYEE E, PROJECT P, EMP_ACT A
WHERE E.EMPNO = A.EMPNO
AND A.PROJNO = P.PROJNO
AND E.WORKDEPT BETWEEN 'A00' AND 'C01'
ORDER BY E.WORKDEPT, E.LASTNAME, A.ACTNO

OR

SELECT DISTINCT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNO


FROM EMPLOYEE E JOIN EMP_ACT A
ON E.EMPNO = A.EMPNO
JOIN PROJECT P
ON A.PROJNO = P.PROJNO
WHERE E.WORKDEPT BETWEEN 'A00' AND 'C01'
ORDER BY E.WORKDEPT, E.LASTNAME, A.ACTNO
Problem 10
SELECT A.ACTNO, D.MGRNO, A.EMSTDATE, P.PROJNO, E.LASTNAME
FROM DEPARTMENT D, EMPLOYEE E, PROJECT P, EMP_ACT A
WHERE E.EMPNO = A.EMPNO
AND A.PROJNO = P.PROJNO
AND E.WORKDEPT = D.DEPTNO
AND A.EMSTDATE >= '1982-10-15'
ORDER BY A.ACTNO, A.EMSTDATE
OR

SELECT A.ACTNO, D.MGRNO, A.EMSTDATE, E.LASTNAME


FROM EMPLOYEE AS E
JOIN EMP_ACT AS A
ON E.EMPNO = A.EMPNO
JOIN PROJECT AS P
ON A.PROJNO = P.PROJNO
JOIN DEPARTMENT AS D
ON E.WORKDEPT = D.DEPTNO
WHERE A.EMSTDATE >= '1982-10-15'
ORDER BY A.ACTNO, A.EMSTDATE

2-14 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 11
SELECT D.DEPTNO, M.LASTNAME AS MANAGER, E.LASTNAME AS EMPLOYEE,
M.HIREDATE AS M_HIREDATE, E.HIREDATE AS E_HIREDATE
FROM EMPLOYEE M, EMPLOYEE E, DEPARTMENT D
WHERE D.MGRNO = M.EMPNO
AND E.WORKDEPT = D.DEPTNO
AND M.HIREDATE > E.HIREDATE
AND E.WORKDEPT = 'A00'
ORDER BY EMPLOYEE

OR
SELECT D.DEPTNO, M.LASTNAME AS MANAGER, E.LASTNAME AS EMPLOYEE,
M.HIREDATE AS M_HIREDATE, E.HIREDATE AS E_HIREDATE
FROM EMPLOYEE AS E
JOIN DEPARTMENT AS D
ON E.WORKDEPT = D.DEPTNO
JOIN EMPLOYEE AS M
ON M.EMPNO = D.MGRNO
WHERE M.HIREDATE > E.HIREDATE
AND E.WORKDEPT = 'A00'
ORDER BY E.LASTNAME

END OF SOLUTION

 Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables 2-15


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

2-16 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Exercise 3. Scalar Functions and Arithmetic

What This Exercise is About


This exercise provides an opportunity to work with scalar functions
and arithmetic expressions.

What You Should Be Able to Do


At the end of the lab, you should be able to:
Code queries that use scalar functions
Code queries by using calculated expressions in the select list
and in the WHERE clause
Use basic scalar functions
Use the CONCAT operator

Introduction
See the data model at the start of this exercise guide to get the
column names and descriptions for each table.

Required Materials
Student handout
SQL Reference

Instructor Exercise Overview


Remind the students they are not expected to complete all problems within the
allotted time.

 Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic 3-1
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem List

Problem 1
For employees whose salary, increased by 5 percent, is less than or
equal to $20,000, list the following:
Last name
Current Salary
Salary increased by 5 percent
Monthly salary increased by 5 percent
Use the following column names for the two generated columns:
INC-Y-SALARY and INC-M-SALARY Use the proper conversion function
to display the increased salary and monthly salary with two of the digits
to the right of the decimal point. Sort the results by annual salary.
Problem 2
All employees with an education level of 18 or 20 will receive a salary
increase of $1,200 and their bonus will be cut in half. List last name,
education level, new salary, and new bonus for these employees.
Display the new bonus with two digits to the right of the decimal point.
Use the column names NEW-SALARY and NEW-BONUS for the
generated columns.
Employees with an education level of 20 should be listed first. For
employees with the same education level, sort the list by salary.
Problem 3
The salary will be decreased by $1,000 for all employees matching the
following criteria:
They belong to department D11
Their salary is more than or equal to 80 percent of $20,000
Their salary is less than or equal to 120 percent of $20,000
Use the name DECR-SALARY for the generated column.
List department number, last name, salary, and decreased salary. Sort
the result by salary.
Problem 4
Produce a list of all employees in department D11 that have an income
(sum of salary, commission, and bonus) that is greater than their salary
increased by 10 percent.
Name the generated column INCOME.
List department number, last name, and income. Sort the result in
descending order by income.
For this problem assume that all employees have non-null salaries,
commissions, and bonuses.

3-2 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 5
List all departments that have no manager assigned. List department
number, department name, and manager number. Replace unknown
manager numbers with the word UNKNOWN and name the column
MGRNO.
Problem 6
List the project number and major project number for all projects that
have a project number beginning with MA. If the major project number
is unknown, display the text 'MAIN PROJECT.'
Name the derived column MAJOR PROJECT.
Sequence the results by PROJNO.
Problem 7
List all employees who were younger than 25 when they joined the
company.
List their employee number, last name, and age when they joined the
company.
Name the derived column AGE.
Sort the result by age and then by employee number.
Problem 8
Provide a list of all projects which ended on December 1, 1982. Display
the year and month of the starting date and the project number. Sort the
result by project number.
Name the derived columns YEAR and MONTH.
Problem 9
List the project number and duration, in weeks, of all projects that have
a project number beginning with MA. The duration should be rounded
and displayed with one decimal position.
Name the derived column WEEKS.
Order the list by the project number.
Problem 10
For projects that have a project number beginning with MA, list the
project number, project ending date, and a modified ending date
assuming the projects will be delayed by 10 percent.
Name the column containing PRENDATE, ESTIMATED. Name the
derived column EXPECTED.
Order the list by project number.

 Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic 3-3
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem 11
How many days are between the first manned landing on the moon (July
20, 1969) and the first day of the year 2000?
Since no columns from a specific table are used in this problem, you
can use any table in the FROM clause but you should indicate a WHERE
condition that derives a single result row (unique key). You may also
select from the SYSIBM.SYSDUMMY1 table which produces a one row
result.
Name the derived column DAYS.

END OF LAB

3-4 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem List With Expected Results

Problem 1
For employees whose salary, increased by 5 percent, is less than or
equal to $20,000, list the following:
Last name
Current Salary
Salary increased by 5 percent
Monthly salary increased by 5 percent
Use the following column names for the two generated columns:
INC-Y-SALARY and INC-M-SALARY Use the proper conversion function
to display the increased salary and monthly salary with two of the digits
to the right of the decimal point. Sort the results by annual salary.
Result
LASTNAME SALARY INC-Y-SALARY INC-M-SALARY
---------+---------+---------+---------+---------+---------+---
PARKER 15340.00 16107.00 1342.25
SETRIGHT 15900.00 16695.00 1391.25
JOHNSON 17250.00 18112.50 1509.37
SMITH 17750.00 18637.50 1553.12
JONES 18270.00 19183.50 1598.62
Problem 2
All employees with an education level of 18 or 20 will receive a salary
increase of $1,200 and their bonus will be cut in half. List last name,
education level, new salary, and new bonus for these employees.
Display the new bonus with two digits to the right of the decimal point.
Use the column names NEW-SALARY and NEW-BONUS for the
generated columns.
Employees with an education level of 20 should be listed first. For
employees with the same education level, sort the list by salary.
Result
LASTNAME EDLEVEL NEW-SALARY NEW-BONUS
---------+---------+---------+---------+---------+--
KWAN 20 39450.00 400.00
NICHOLLS 18 29620.00 300.00
LUTZ 18 31040.00 300.00
THOMPSON 18 42450.00 400.00
HAAS 18 53950.00 500.00

 Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic 3-5
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem 3
The salary will be decreased by $1,000 for all employees matching the
following criteria:
They belong to department D11
Their salary is more than or equal to 80 percent of $20,000
Their salary is less than or equal to 120 percent of $20,000
Use the name DECR-SALARY for the generated column.
List department number, last name, salary, and decreased salary. Sort
the result by salary.
Result
WORKDEPT LASTNAME SALARY DECR-SALARY
---------+---------+---------+---------+---------+--
D11 JONES 18270.00 17270.00
D11 WALKER 20450.00 19450.00
D11 SCOUTTEN 21340.00 20340.00
D11 PIANKA 22250.00 21250.00
Problem 4
Produce a list of all employees in department D11 that have an income
(sum of salary, commission, and bonus) that is greater than their salary
increased by 10 percent.
Name the generated column INCOME.
List department number, last name, and income. Sort the result in
descending order by income.
For this problem assume that all employees have non-null salaries,
commissions, and bonuses.
Result
WORKDEPT LASTNAME INCOME
---------+---------+---------+---------+
D11 LUTZ 32827.00
D11 BROWN 30557.00
D11 YOSHIMURA 27154.00
D11 SCOUTTEN 23547.00
D11 JONES 20132.00
Problem 5
List all departments that have no manager assigned. List department
number, department name, and manager number. Replace unknown
manager numbers with the word UNKNOWN and name the column
MGRNO.
Result
DEPTNO DEPTNAME MGRNO
------ ----------------------------- -------
D01 DEVELOPMENT CENTER UNKNOWN

3-6 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 6
List the project number and major project number for all projects that
have a project number beginning with MA. If the major project number
is unknown, display the text 'MAIN PROJECT.'
Name the derived column MAJOR PROJECT.
Sequence the results by PROJNO.
Result
PROJNO MAJOR PROJECT
---------+---------+-
MA2100 MAIN PROJECT
MA2110 MA2100
MA2111 MA2110
MA2112 MA2110
MA2113 MA2110
Problem 7
List all employees who were younger than 25 when they joined the
company.
List their employee number, last name, and age when they joined the
company.
Name the derived column AGE.
Sort the result by age and then by employee number.
Result
EMPNO LASTNAME AGE
---------+---------+---------+------
000220 LUTZ 20
000340 GOUNOT 20
000120 O'CONNELL 21
000160 PIANKA 22
000190 WALKER 22
000050 GEYER 23
000100 SPENSER 23
000150 ADAMSON 24
000180 SCOUTTEN 24
000200 BROWN 24

 Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic 3-7
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem 8
Provide a list of all projects which ended on December 1, 1982. Display
the year and month of the starting date and the project number. Sort the
result by project number.
Name the derived columns YEAR and MONTH.
Result
YEAR MONTH PROJNO
---------+---------+---------+--
1982 1 MA2111
1982 1 MA2112
1982 2 MA2113
Problem 9
List the project number and duration, in weeks, of all projects that have
a project number beginning with MA. The duration should be rounded
and displayed with one decimal position.
Name the derived column WEEKS.
Order the list by the project number.
Result
PROJNO WEEKS
---------+--------
MA2100 56.6
MA2110 56.6
MA2111 47.7
MA2112 47.7
MA2113 41.3
Problem 10
For projects that have a project number beginning with MA, list the
project number, project ending date, and a modified ending date
assuming the projects will be delayed by 10 percent.
Name the column containing PRENDATE, ESTIMATED. Name the
derived column EXPECTED.
Order the list by project number.
Result
PROJNO ESTIMATED EXPECTED
---------+---------+---------+
MA2100 1983-02-01 1983-03-12
MA2110 1983-02-01 1983-03-12
MA2111 1982-12-01 1983-01-03
MA2112 1982-12-01 1983-01-03
MA2113 1982-12-01 1982-12-29

3-8 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 11
How many days are between the first manned landing on the moon (July
20, 1969) and the first day of the year 2000?
Since no columns from a specific table are used in this problem, you
can use any table in the FROM clause but you should indicate a WHERE
condition that derives a single result row (unique key). You may also
select from the SYSIBM.SYSDUMMY1 table which produces a one row
result.
Name the derived column DAYS.
Result
DAYS
---------+-
11122

END OF Problem List With Expected Results

 Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic 3-9
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Solution

Problem 1
SELECT LASTNAME, SALARY,
DECIMAL(SALARY*1.05,9,2) AS "INC-Y-SALARY",
DECIMAL(SALARY*1.05/12,9,2) AS "INC-M-SALARY"
FROM EMPLOYEE
WHERE SALARY*1.05 <= 20000
ORDER BY SALARY
Problem 2
SELECT LASTNAME, EDLEVEL,
SALARY+1200 AS "NEW-SALARY",
DECIMAL(BONUS*0.5,9,2) AS "NEW-BONUS"
FROM EMPLOYEE
WHERE EDLEVEL = 18 OR EDLEVEL = 20
ORDER BY EDLEVEL DESC, 3

ALTERNATIVE
SELECT LASTNAME, EDLEVEL,
SALARY+1200 AS "NEW-SALARY",
DECIMAL(BONUS*0.5,9,2) AS "NEW-BONUS"
FROM EMPLOYEE
WHERE EDLEVEL IN (18, 20)
ORDER BY EDLEVEL DESC, "NEW-BONUS"
Problem 3
SELECT WORKDEPT, LASTNAME, SALARY,
SALARY-1000 AS "DECR-SALARY"
FROM EMPLOYEE
WHERE SALARY BETWEEN 20000*0.80 AND 20000*1.20
AND WORKDEPT = 'D11'
ORDER BY SALARY

3-10 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 4
SELECT WORKDEPT, LASTNAME, SALARY+COMM+BONUS AS INCOME
FROM EMPLOYEE
WHERE SALARY+COMM+BONUS > 1.1*SALARY
AND WORKDEPT = 'D11'
ORDER BY 3 DESC
ALTERNATIVE

SELECT WORKDEPT, LASTNAME, SALARY+COMM+BONUS AS INCOME


FROM EMPLOYEE
WHERE SALARY+COMM+BONUS > 1.1*SALARY
AND WORKDEPT = 'D11'
ORDER BY INCOME DESC
Problem 5
SELECT DEPTNO, DEPTNAME , COALESCE(MGRNO, 'UNKNOWN') AS MGRNO
FROM DEPARTMENT
WHERE MGRNO IS NULL
Problem 6
SELECT PROJNO,
COALESCE(MAJPROJ,'MAIN PROJECT') AS "MAJOR PROJECT"
FROM PROJECT
WHERE SUBSTR(PROJNO,1,2) ='MA'
ORDER BY PROJNO
Problem 7
SELECT EMPNO, LASTNAME, YEAR(HIREDATE-BIRTHDATE) AS AGE
FROM EMPLOYEE
WHERE YEAR(HIREDATE-BIRTHDATE) < 25
ORDER BY AGE, EMPNO
Problem 8
SELECT YEAR(PRSTDATE) AS YEAR, MONTH(PRSTDATE) AS MONTH, PROJNO
FROM PROJECT
WHERE PRENDATE = '1982-12-01'
ORDER BY PROJNO
Problem 9
SELECT PROJNO,
DECIMAL((DAYS(PRENDATE)-DAYS(PRSTDATE))/7.+0.05,8,1) AS WEEKS
FROM PROJECT
WHERE PROJNO LIKE 'MA%'
ORDER BY PROJNO

 Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic 3-11
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem 10
SELECT PROJNO, PRENDATE AS ESTIMATED,
PRSTDATE + ((DAYS(PRENDATE) - DAYS(PRSTDATE))*1.1) DAYS
AS EXPECTED
FROM PROJECT
WHERE PROJNO LIKE 'MA%'
ORDER BY PROJNO
Problem 11
SELECT DAYS('2000-01-01')-DAYS('1969-07-20') AS DAYS
FROM EMPLOYEE
WHERE EMPNO = '000010'

ALTERNATIVE
SELECT DAYS('2000-01-01')-DAYS('1969-07-20') AS DAYS
FROM SYSIBM.SYSDUMMY1

END OF SOLUTION

3-12 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Exercise 4. Column Functions and Grouping

What This Exercise is About


This exercise provides an opportunity to work with column
functions.

What You Should Be Able to Do


At the end of the lab, you should be able to:
Code queries that use column functions

Introduction
See the data model at the start of this exercise guide to get the
column names and descriptions for each table.

Required Materials
Student handout
SQL Reference

Instructor Exercise Overview


Remind the students that they may not have time to finish all lab problems.

 Copyright IBM Corp. 1999, 2000 Exercise 4. Column Functions and Grouping 4-1
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem List

Problem 1
For all departments, display department number and the sum of all
salaries for each department. Name the derived column SUM_SALARY.
Problem 2
For all departments, display the department number and the number of
employees. Name the derived column EMP_COUNT.
Problem 3
Display those departments which have more than 3 employees.
Problem 4
For all departments with at least one designer, display the number of
designers and the department number. Name the derived column
DESIGNER.
Problem 5
Show the average salary for men and the average salary for women for
each department. Display the work department, the sex, the average
salary, average bonus, average commission, and the number of people
in each group. Include only those groups that have two or more people.
Show only two decimal places in the averages.
Use the following names for the derived columns: AVG-SALARY,
AVG-BONUS, AVG-COMM, and COUNT.
Problem 6
Display the average bonus and average commission for all departments
with an average bonus greater than $500 and an average commission
greater than $2,000. Display all averages with two digits to the right of
the decimal point. Use the column headings AVG-BONUS and
AVG-COMM for the derived columns.

END OF LAB

4-2 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem List With Expected Results

Problem 1
For all departments, display department number and the sum of all
salaries for each department. Name the derived column SUM_SALARY.
Result
WORKDEPT SUM_SALARY
-------- ----------
A00 128500.00
B01 41250.00
C01 90470.00
D11 222100.00
D21 150920.00
E01 40175.00
E11 104990.00
E21 95310.00
Problem 2
For all departments, display the department number and the number of
employees. Name the derived column EMP_COUNT.
Result
WORKDEPT EMP_COUNT
-------- ---------
A00 3
B01 1
C01 3
D11 9
D21 6
E01 1
E11 5
E21 4
Problem 3
Display those departments which have more than 3 employees.
Result
WORKDEPT
--------
D11
D21
E11
E21

 Copyright IBM Corp. 1999, 2000 Exercise 4. Column Functions and Grouping 4-3
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

Problem 4
For all departments with at least one designer, display the number of
designers and the department number. Name the derived column
DESIGNER.
Result
DESIGNER WORKDEPT
---------+---------+-
8 D11
Problem 5
Show the average salary for men and the average salary for women for
each department. Display the work department, the sex, the average
salary, average bonus, average commission, and the number of people
in each group. Include only those groups that have two or more people.
Show only two decimal places in the averages.
Use the following names for the derived columns: AVG-SALARY,
AVG-BONUS, AVG-COMM, and COUNT.
Result
WORKDEPT SEX AVG-SALARY AVG-BONUS AVG-COMM COUNT
---------+---------+---------+---------+---------+-------
A00 M 37875.00 750.00 3030.00 2
C01 F 30156.66 633.33 2412.66 3
D11 F 24476.66 500.00 1958.00 3
D11 M 24778.33 500.00 1981.83 6
D21 F 26933.33 500.00 2154.33 3
D21 M 23373.33 466.66 1869.66 3
E11 F 23966.66 466.66 1917.33 3
E11 M 16545.00 350.00 1323.50 2
E21 M 23827.50 475.00 1906.25 4
Problem 6
Display the average bonus and average commission for all departments
with an average bonus greater than $500 and an average commission
greater than $2,000. Display all averages with two digits to the right of
the decimal point. Use the column headings AVG-BONUS and
AVG-COMM for the derived columns.
Result
WORKDEPT AVG-BONUS AVG-COMM
---------+---------+---------+---------+------
A00 833.33 3426.66
B01 800.00 3300.00
C01 633.33 2412.66
E01 800.00 3214.00

END OF Problem List With Expected Results

4-4 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Solution

Problem 1
SELECT WORKDEPT, SUM(SALARY) AS SUM_SALARY
FROM EMPLOYEE
GROUP BY WORKDEPT
Problem 2
SELECT WORKDEPT, COUNT(*) AS EMP_COUNT
FROM EMPLOYEE
GROUP BY WORKDEPT
Problem 3
SELECT WORKDEPT
FROM EMPLOYEE
GROUP BY WORKDEPT
HAVING COUNT(*) > 3
Problem 4
SELECT COUNT(*) AS DESIGNER, WORKDEPT
FROM EMPLOYEE
WHERE JOB = 'DESIGNER'
GROUP BY WORKDEPT
Problem 5
SELECT WORKDEPT, SEX,
DECIMAL(AVG(SALARY),8,2) AS "AVG-SALARY",
DECIMAL(AVG(BONUS),8,2) AS "AVG-BONUS",
DECIMAL(AVG(COMM),8,2) AS "AVG-COMM",
COUNT(*) AS COUNT
FROM EMPLOYEE
GROUP BY WORKDEPT, SEX
HAVING COUNT(*) > 1
Problem 6
SELECT WORKDEPT, DECIMAL(AVG(BONUS),9,2) AS "AVG-BONUS",
DECIMAL(AVG(COMM),9,2) AS "AVG-COMM"
FROM EMPLOYEE
GROUP BY WORKDEPT
HAVING AVG(BONUS) > 500 AND AVG(COMM) > 2000

END OF SOLUTION

 Copyright IBM Corp. 1999, 2000 Exercise 4. Column Functions and Grouping 4-5
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Instructor Exercises Guide

4-6 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Exercise 5. UNION

What This Exercise is About


This exercise provides an opportunity to work with the UNION
keyword.

What You Should Be Able to Do


At the end of the lab you should be able to:
Derive a final result by combining results from multiple
SELECTs.

Introduction
See the data model at the start of this exercise guide to get the
column names and descriptions for each table.

Required Materials
Student handout
SQL Reference

Instructor Exercise Overview


Remind the student of the difference between UNION and UNION ALL.

 Copyright IBM Corp. 1999, 2000 Exercise 5. UNION 5-1


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

Problem List

Problem 1
List the names and salaries for the non-managers working in
department D21 showing the effects of a 10 percent raise. Use the
following output as a guide. Apply and appropriate ORDER BY clause to
achieve the required results. Use the column headings shown.
LASTNAME FIRSTNME WHEN SALARY
--------------- ------------ -------------- --------------
JEFFERSON JAMES BEFORE A RAISE 22180.000
JEFFERSON JAMES AFTER A RAISE 24398.000
JOHNSON SYBIL BEFORE A RAISE 17250.000
JOHNSON SYBIL AFTER A RAISE 18975.000
... ... ... ...
Problem 2
List the department number, employee number, and salaries of all
employees in department A00.
For the last line of the report, display the sum of all the salaries.
Problem 3
For departments A00, B01, and C01, list the projects assigned to them
and the employees in each department. The output should consist of up
to three types of lines for each department as follows:
See expected results for clarification of the following instructions.
First line (one per department):
Department number
Text: DEPARTMENT
Department name
Second line(s) (if data available - one line per project):
Department number
Project number
Project name
Subsequent line(s) (if data available - one line per employee):
Department number
Employee number
Last name

5-2 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 4
For all projects that have a project number that begins with IF, display
the following:
First line:
Text: PROJECT
Project number
The employee number of the employee responsible for the project
Estimated starting date
Estimated ending date
Subsequent line(s) (one per employee working on the project):
Project number
The employee number of the employee performing the activity
Activity starting date
Activity ending date
Sequence the results by the project number, then by employee number,
and finally by the starting date.

END OF LAB

 Copyright IBM Corp. 1999, 2000 Exercise 5. UNION 5-3


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

Problem List With Expected Results

Problem 1
List the names and salaries for the non-managers working in
department D21 showing the effects of a 10 percent raise. Use the
following output as a guide. Apply and appropriate ORDER BY clause to
achieve the required results. Use the column headings shown.
LASTNAME FIRSTNME WHEN SALARY
--------------- ------------ -------------- --------------
JEFFERSON JAMES BEFORE A RAISE 22180.000
JEFFERSON JAMES AFTER A RAISE 24398.000
JOHNSON SYBIL BEFORE A RAISE 17250.000
JOHNSON SYBIL AFTER A RAISE 18975.000
... ... ... ...
Result
LASTNAME FIRSTNME WHEN SALARY
--------------- ------------ -------------- --------------
JEFFERSON JAMES BEFORE A RAISE 22180.000
JEFFERSON JAMES AFTER A RAISE 24398.000
JOHNSON SYBIL BEFORE A RAISE 17250.000
JOHNSON SYBIL AFTER A RAISE 18975.000
MARINO SALVATORE BEFORE A RAISE 28760.000
MARINO SALVATORE AFTER A RAISE 31636.000
PEREZ MARIA BEFORE A RAISE 27380.000
PEREZ MARIA AFTER A RAISE 30118.000
SMITH DANIEL BEFORE A RAISE 19180.000
SMITH DANIEL AFTER A RAISE 21098.000

5-4 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 2
List the department number, employee number, and salaries of all
employees in department A00.
For the last line of the report, display the sum of all the salaries.
Result
WORKDEPT EMPNO SALARY
---------+---------+---------+--------
A00 000120 29250.00
A00 000110 46500.00
A00 000010 52750.00
A00 SUM 128500.00

 Copyright IBM Corp. 1999, 2000 Exercise 5. UNION 5-5


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

Problem 3
For departments A00, B01, and C01, list the projects assigned to them
and the employees in each department. The output should consist of up
to three types of lines for each department as follows:
See expected results for clarification of the following instructions.
First line (one per department):
Department number
Text: DEPARTMENT
Department name
Second line(s) (if data available - one line per project):
Department number
Project number
Project name
Subsequent line(s) (if data available - one line per employee):
Department number
Employee number
Last name
Result
DEPTNO INFO DEPTNAME
---------+---------+---------+---------+---------+-----
A00 DEPARTMENT SPIFFY COMPUTER SERVICE DIV. 1
A00 000010 HAAS 3
A00 000110 LUCCHESI 3
A00 000120 O'CONNELL 3
B01 DEPARTMENT PLANNING 1
B01 PL2100 WELD LINE PLANNING 2
B01 000020 THOMPSON 3
C01 DEPARTMENT INFORMATION CENTER 1
C01 IF1000 QUERY SERVICES 2
C01 IF2000 USER EDUCATION 2
C01 000030 KWAN 3
C01 000130 QUINTANA 3
C01 000140 NICHOLLS 3

5-6 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 4
For all projects that have a project number that begins with IF, display
the following:
First line:
Text: PROJECT
Project number
The employee number of the employee responsible for the project
Estimated starting date
Estimated ending date
Subsequent line(s) (one per employee working on the project):
Project number
The employee number of the employee performing the activity
Activity starting date
Activity ending date
Sequence the results by the project number, then by employee number,
and finally by the starting date.
Result
PROJNO RESPEMP PRSTDATE PRENDATE
---------+---------+---------+---------+--------
PROJECT IF1000 000030 1982-01-01 1983-02-01
IF1000 000030 1982-06-01 1983-01-01
IF1000 000130 1982-01-01 1982-10-01
IF1000 000130 1982-10-01 1983-01-01
IF1000 000140 1982-10-01 1983-01-01
PROJECT IF2000 000030 1982-01-01 1983-02-01
IF2000 000030 1982-01-01 1983-01-01
IF2000 000140 1982-01-01 1982-03-01
IF2000 000140 1982-03-01 1982-07-01
IF2000 000140 1982-03-01 1982-07-01
IF2000 000140 1982-10-01 1983-01-01

END OF Problem List With Expected Results

 Copyright IBM Corp. 1999, 2000 Exercise 5. UNION 5-7


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

Solution

Problem 1
SELECT LASTNAME, FIRSTNME, 'BEFORE A RAISE' AS WHEN, SALARY
FROM EMPLOYEE
WHERE WORKDEPT = 'D21'
AND JOB <> 'MANAGER'

UNION ALL

SELECT LASTNAME, FIRSTNME, 'AFTER A RAISE' AS WHEN, SALARY * 1.1


AS SALARY
FROM EMPLOYEE
WHERE WORKDEPT = 'D21'
AND JOB <> 'MANAGER'
ORDER BY LASTNAME, FIRSTNME, WHEN DESC
Problem 2
SELECT WORKDEPT, EMPNO, SALARY
FROM EMPLOYEE
WHERE WORKDEPT = 'A00'
UNION ALL

SELECT WORKDEPT, 'SUM', SUM(SALARY)


FROM EMPLOYEE
WHERE WORKDEPT = 'A00'
GROUP BY WORKDEPT
ORDER BY 3

5-8 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 3
SELECT DEPTNO, 'DEPARTMENT' AS INFO, DEPTNAME, 1
FROM DEPARTMENT
WHERE DEPTNO IN ('A00' , 'B01' , 'C01')

UNION ALL
SELECT DEPTNO, PROJNO AS INFO, PROJNAME, 2
FROM PROJECT
WHERE DEPTNO IN ('A00' , 'B01' , 'C01')

UNION ALL

SELECT WORKDEPT AS DEPTNO, EMPNO AS INFO, LASTNAME, 3


FROM EMPLOYEE
WHERE WORKDEPT IN ('A00' , 'B01' , 'C01')
ORDER BY DEPTNO, 4
Problem 4
SELECT 'PROJECT', PROJNO, RESPEMP, PRSTDATE, PRENDATE
FROM PROJECT
WHERE PROJNO LIKE 'IF%'
UNION ALL

SELECT ' ', PROJNO, EMPNO, EMSTDATE, EMENDATE


FROM EMP_ACT
WHERE PROJNO LIKE 'IF%'
ORDER BY 2, 1 DESC, 3, 4

END OF SOLUTION

 Copyright IBM Corp. 1999, 2000 Exercise 5. UNION 5-9


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

5-10 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Exercise 6. Using Subqueries

What This Exercise is About


This exercise provides an opportunity to work with subqueries.

What You Should Be Able to Do


At the end of the lab, you should be able to:
Code basic subqueries
Code more complex subqueries using the keyword IN

Introduction
See the data model at the start of this exercise guide to get the
column names and descriptions for each table.

Required Materials
Student handout
SQL Reference

Instructor Exercise Overview


Point out that many time an SQL statement can be written as a join or a subquery.
The objective of this lab is to solve the problems using subqueries.

 Copyright IBM Corp. 1999, 2000 Exercise 6. Subqueries 6-1


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

Problem List

Problem 1
List those employees that have a salary which is greater than or equal
to the average salary of all employees plus $5,000.
Display department number, employee number, last name, and salary.
Sort the list by the department number and employee number.
Problem 2
List employee number and last name of all employees not assigned to
any projects. This means that table EMP_ACT does not contain a row
with their employee number.
Problem 3
List project number and duration (in days) of the project with the
shortest duration.
Name the derived column DAYS.
Problem 4
List department number, department name, last name, and first name of
all those employees in departments that have only male employees.
Problem 5
We want to do a salary analysis for people that have the same job and
education level as the employee Stern. Show the last name, job,
edlevel, the number of years they've worked as of January 1, 2000, and
their salary.
Name the derived column YEARS.
Sort the listing by highest salary first.

END OF LAB

6-2 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem List With Expected Results

Problem 1
List those employees that have a salary which is greater than or equal
to the average salary of all employees plus $5,000.
Display department number, employee number, last name, and salary.
Sort the list by the department number and employee number.
Result
WORKDEPT EMPNO LASTNAME SALARY
---------+---------+---------+------
A00 000010 HAAS 52750.00
A00 000110 LUCCHESI 46500.00
B01 000020 THOMPSON 41250.00
C01 000030 KWAN 38250.00
D21 000070 PULASKI 36170.00
E01 000050 GEYER 40175.00
Problem 2
List employee number and last name of all employees not assigned to
any projects. This means that table EMP_ACT does not contain a row
with their employee number.
Result
EMPNO LASTNAME
---------+-------
000060 STERN
000120 O'CONNELL

 Copyright IBM Corp. 1999, 2000 Exercise 6. Subqueries 6-3


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

Problem 3
List project number and duration (in days) of the project with the
shortest duration.
Name the derived column DAYS.
Result
PROJNO DAYS
---------+---------
PL2100 257
Problem 4
List department number, department name, last name, and first name of
all those employees in departments that have only male employees.
Result
DEPTNO DEPTNAME LASTNAME FIRSTNME
---------+---------+---------+---------+---------+-----
B01 PLANNING THOMPSON MICHAEL
E01 SUPPORT SERVICES GEYER JOHN
E21 SOFTWARE SUPPORT SPENSER THEODORE
E21 SOFTWARE SUPPORT MEHTA RAMLAL
E21 SOFTWARE SUPPORT LEE WING
E21 SOFTWARE SUPPORT GOUNOT JASON
Problem 5
We want to do a salary analysis for people that have the same job and
education level as the employee Stern. Show the last name, job,
edlevel, the number of years they've worked as of January 1, 2000, and
their salary.
Name the derived column YEARS.
Sort the listing by highest salary first.
Result
LASTNAME JOB EDLEVEL YEARS SALARY
--------------- -------- ------- ----------- ------------
GEYER MANAGER 16 50 40175.00
PULASKI MANAGER 16 19 36170.00
STERN MANAGER 16 26 32250.00
HENDERSON MANAGER 16 29 29750.00

6-4 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

END OF Problem List With Expected Results

 Copyright IBM Corp. 1999, 2000 Exercise 6. Subqueries 6-5


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

Solution

Problem 1
SELECT WORKDEPT, EMPNO, LASTNAME, SALARY
FROM EMPLOYEE
WHERE SALARY >=(SELECT AVG(SALARY) + 5000
FROM EMPLOYEE)
ORDER BY WORKDEPT, EMPNO
Problem 2
SELECT EMPNO, LASTNAME
FROM EMPLOYEE
WHERE EMPNO NOT IN (SELECT EMPNO
FROM EMP_ACT)
Problem 3
SELECT PROJNO, DAYS(PRENDATE)-DAYS(PRSTDATE) AS DAYS
FROM PROJECT
WHERE DAYS(PRENDATE)-DAYS(PRSTDATE) =
(SELECT MIN(DAYS(PRENDATE)-DAYS(PRSTDATE))
FROM PROJECT)
Problem 4
SELECT DEPTNO, DEPTNAME, LASTNAME, FIRSTNME
FROM DEPARTMENT, EMPLOYEE
WHERE DEPTNO = WORKDEPT
AND DEPTNO NOT IN(SELECT WORKDEPT
FROM EMPLOYEE
WHERE SEX = 'F')
Problem 5
SELECT LASTNAME, JOB, EDLEVEL,
YEAR('2000-01-01' - HIREDATE) AS YEARS, SALARY
FROM EMPLOYEE
WHERE (JOB, EDLEVEL) IN (SELECT JOB, EDLEVEL
FROM EMPLOYEE
WHERE LASTNAME = 'STERN')
ORDER BY SALARY DESC

END OF SOLUTION

6-6 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Exercise 7. Maintaining Data

What This Exercise is About


This exercise provides an opportunity to code SQL statements in
order to manipulate data.

What You Should Be Able to Do


At the end of the lab, you should be able to:
Code UPDATE and DELETE statements with (or without)
conditions.
Code INSERT statements using a VALUE or a SELECT clause.

Introduction
See the data model at the start of this exercise guide to get the
column names and descriptions for each table.

Required Materials
Student handout
SQL Reference

Instructor Exercise Overview


Since these lab problems build upon each other they should be done in sequence.

 Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data 7-1


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

Problem List
Feel free to run a SELECT * FROM TESTEMP between problems to verify the success
of your previous queries.

Problem 1
Create table TESTEMP with the following columns:
EMPNO
LASTNAME
WORKDEPT
HIREDATE
SALARY
BONUS
The data types and null characteristics for these columns should be the
same as the corresponding columns in the EMPLOYEE table described
at the beginning of the lab exercises. For the OS/390 environment, use
the following IN clause at the end of your statement: IN DBCF12xx.TSxx
(where xx is your assigned lab number).
Problem 2
Mr. Smith, Ms Baker, and Ms Thomas joined the company. Their data is
as follows:
Employee numbers: 000111, 000222, 000333
Last name: SMITH, BAKER, THOMAS
Department number: C01, A00, D11
Date hired: June 6, 2000
Salary: $25,000, $28,000, $33,000
Bonus: $0, NULL, $0
Add the new employees to the TESTEMP table.
Problem 3
Insert data into the TESTEMP table by copying the appropriate columns
in the EMPLOYEE table for those employees that have an employee
number less than or equal to 50.
Problem 4
Mr. Smith receives a bonus of $500. Make the appropriate data change.
Problem 5
All employees in department C01 have done a good job. Therefore, they
receive a salary increase of $1,000.
Problem 6
After a short time Mr. Smith leaves the company. Make the appropriate
changes.

7-2 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 7
Insert data into the TESTEMP table by copying the columns in the
EMPLOYEE table where the employees have an employee number
greater than 50.
Problem 8
Make the appropriate changes for the following situation:
Theodore Spenser is assigned to department E01.
Problem 9
Mrs. Brown joined the company.
For Mrs. Brown enter the following data:
Employee number: 360
Last name: BROWN
Department number: D01
Date hired: Date when data is entered
Salary: $45,000
Bonus: unknown
Problem 10
Delete all rows from the TESTEMP table and examine the contents.
Then drop the TESTEMP table and try to examine the contents.

END OF LAB

 Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data 7-3


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

Problem List With Expected Results

Problem 1
Create table TESTEMP with the following columns:
EMPNO
LASTNAME
WORKDEPT
HIREDATE
SALARY
BONUS
The data types and null characteristics for these columns should be the
same as the corresponding columns in the EMPLOYEE table described
at the beginning of the lab exercises. For the OS/390 environment, use
the following IN clause at the end of your statement: IN DBCF12xx.TSxx
(where xx is your assigned lab number).
Result
If running from SPUFI:
---------+---------+---------+---------+---------+--------
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+--------
DSNE617I COMMIT PERFORMED, SQLCODE IS 0
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
If running from QMF:
OK, running your query changed the database.
COMMAND ===>
Problem 2
Mr. Smith, Ms Baker, and Ms Thomas joined the company. Their data is
as follows:
Employee numbers: 000111, 000222, 000333
Last name: SMITH, BAKER, THOMAS
Department number: C01, A00, D11
Date hired: June 6, 2000
Salary: $25,000, $28,000, $33,000
Bonus: $0, NULL, $0
Add the new employees to the TESTEMP table.
Result

Confirmation that your rows were added to the table.

In SPUFI:

7-4 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

If rows were inserted one at a time, the following message


should appear after each INSERT statement;
---------+---------+---------+---------+---------+---------+
DSNE615I NUMBER OF ROWS AFFECTED IS 1
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+---------+

If all three rows were inserted through a single INSERT statement


then a message similar to the following should be observed:

---------+---------+---------+---------+---------+---------+
DSNE615I NUMBER OF ROWS AFFECTED IS 3
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+---------+
From QMF:
OK, number of rows modified in the database: 3.
COMMAND ===>
or, if each row was inserted separately, the following message
should be received for each row inserted.

OK, number of rows modified in the database: 1.


COMMAND ===>
Problem 3
Insert data into the TESTEMP table by copying the appropriate columns
in the EMPLOYEE table for those employees that have an employee
number less than or equal to 50.
Result
In SPUFI:
---------+---------+---------+---------+---------+--------
DSNE615I NUMBER OF ROWS AFFECTED IS 4
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+--------
DSNE617I COMMIT PERFORMED, SQLCODE IS 0
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+--------

In QMF:

OK, number of rows modified in the database: 4.


COMMAND ===>

 Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data 7-5


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

Problem 4
Mr. Smith receives a bonus of $500. Make the appropriate data change.
Result

In SPUFI:

---------+---------+---------+---------+---------+-------
DSNE615I NUMBER OF ROWS AFFECTED IS 1
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+-------
DSNE617I COMMIT PERFORMED, SQLCODE IS 0
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+-------

In QMF:
OK, number of rows modified in the database: 1
COMMAND ===>
Problem 5
All employees in department C01 have done a good job. Therefore, they
receive a salary increase of $1,000.
Result

In SPUFI:

---------+---------+---------+---------+---------+---------+-
DSNE615I NUMBER OF ROWS AFFECTED IS 2
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+---------+-

In QMF:
OK, number of rows modified in the database: 2.
COMMAND ===>

7-6 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 6
After a short time Mr. Smith leaves the company. Make the appropriate
changes.
Result

In SPUFI:

---------+---------+---------+---------+---------+-------
DSNE615I NUMBER OF ROWS AFFECTED IS 1
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+-------

In QMF:

OK, number of rows modified in the database: 1.


COMMAND ===>
Problem 7
Insert data into the TESTEMP table by copying the columns in the
EMPLOYEE table where the employees have an employee number
greater than 50.
Result

In SPUFI:

---------+---------+---------+---------+---------+--------
DSNE615I NUMBER OF ROWS AFFECTED IS 28
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+--------

In QMF:
OK, number of rows modified in the database: 28.
COMMAND ===>

 Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data 7-7


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

Problem 8
Make the appropriate changes for the following situation:
Theodore Spenser is assigned to department E01.
Result

In SPUFI:

---------+---------+---------+---------+---------+--------
DSNE615I NUMBER OF ROWS AFFECTED IS 1
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+--------

In QMF:

OK, number of rows modified in the database: 1.


COMMAND ===>
Problem 9
Mrs. Brown joined the company.
For Mrs. Brown enter the following data:
Employee number: 360
Last name: BROWN
Department number: D01
Date hired: Date when data is entered
Salary: $45,000
Bonus: unknown
Result

In SPUFI:

---------+---------+---------+---------+---------+--------
DSNE615I NUMBER OF ROWS AFFECTED IS 1
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+--------

In QMF:

OK, number of rows modified in the database: 1.


COMMAND ===>
Problem 10
Delete all rows from the TESTEMP table and examine the contents.
Then drop the TESTEMP table and try to examine the contents.

7-8 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Result
In SPUFI, after deleting the rows:

DSNE615I NUMBER OF ROWS AFFECTED IS 35


DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+---------+---------+--
SELECT * FROM TESTEMP;
---------+---------+---------+---------+---------+---------+---------+--
EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS
---------+---------+---------+---------+---------+---------+---------+--
DSNE610I NUMBER OF ROWS DISPLAYED IS 0
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100

After dropping the table:

---------+---------+---------+---------+---------+---------+---------+--
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+---------+---------+--
SELECT * FROM TESTEMP;
---------+---------+---------+---------+---------+---------+---------+--
DSNT408I SQLCODE = -204, ERROR: USERID.TESTEMP IS AN UNDEFINED NAME
DSNT418I SQLSTATE = 42704 SQLSTATE RETURN CODE

In QMF, after deleting the rows:

OK, number of rows modified in the database: 35.


COMMAND ===>

SELECT * FROM TESTEMP;


EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS
------ --------------- -------- ---------- ------------ ------------

After dropping the table:


OK, running your query changed the database.
COMMAND ===>

SELECT * FROM TESTEMP;

QUERY MESSAGES:
USERID.TESTEMP could not be found.
END OF LAB (with hints)

 Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data 7-9


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

Solutions

Problem 1
CREATE TABLE TESTEMP
(EMPNO CHAR(6) NOT NULL,
LASTNAME VARCHAR(15) NOT NULL,
WORKDEPT CHAR(3) ,
HIREDATE DATE ,
SALARY DECIMAL(9,2) ,
BONUS DECIMAL(9,2) )
IN DBCF12xx.TSxx -- This IN clause is for OS/390 classes
-- classes taught in other environments may or may not need an IN
-- clause. See your instructor for additional information.
Problem 2
Version 7 INSERT syntax follows:

INSERT INTO TESTEMP


VALUES ('000111', 'SMITH', 'C01', '06/06/2000', 25000, 0),
('000222', 'BAKER', 'A00', '06/06/2000', 28000, NULL),
('000333', 'THOMAS', 'D11', '06/06/2000', 33000, 0)

or

INSERT INTO TESTEMP


(EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUS)
VALUES ('000111', 'SMITH', 'C01', '06/06/2000', 25000, 0),
('000333', 'THOMAS', 'D11', '06/06/2000', 33000, 0)

INSERT INTO TESTEMP


(EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY)
VALUES ('000222', 'BAKER', 'A00', '06/06/2000', 28000)
Note that the stand alone insert for Baker's row does not list the BONUS
column in the column list, nor is its value specified in the VALUES
clause. As a result, the
BONUS for Baker's row will be marked as unknown (NULL).
Rows inserted: 3
Prior to version 7, each row must be added via a separate INSERT
statement (only data for one row allowed per VALUES clause) as shown
below:

7-10 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

INSERT INTO TESTEMP


VALUES ('000111', 'SMITH', 'C01', '06/06/2000', 25000, 0);

INSERT INTO TESTEMP


VALUES ('000222', 'BAKER', 'A00', '06/06/2000', 28000, NULL);

INSERT INTO TESTEMP


VALUES ('000333', 'THOMAS', 'D11', '06/06/2000', 33000, 0);
Problem 3
INSERT INTO TESTEMP
(EMPNO,LASTNAME,WORKDEPT,HIREDATE,SALARY,BONUS )
SELECT EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUS
FROM EMPLOYEE
WHERE EMPNO <= '000050'
ALTERNATIVE
INSERT INTO TESTEMP
SELECT EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUS
FROM EMPLOYEE
WHERE EMPNO <= '000050'
Rows inserted: 4
Problem 4
UPDATE TESTEMP
SET BONUS = 500
WHERE EMPNO = '000111'
Rows updated: 1
Problem 5
UPDATE TESTEMP
SET SALARY = SALARY + 1000
WHERE WORKDEPT = 'C01'

Rows updated: 2

 Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data 7-11


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

Problem 6
DELETE FROM TESTEMP
WHERE EMPNO = '000111'

Rows deleted: 1
Problem 7
INSERT INTO TESTEMP
(EMPNO,LASTNAME,WORKDEPT,HIREDATE,SALARY,BONUS)
SELECT EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUS
FROM EMPLOYEE
WHERE EMPNO > '000050'
ALTERNATIVE

INSERT INTO TESTEMP


SELECT EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUS
FROM EMPLOYEE
WHERE EMPNO > '000050'
Rows inserted: 28
Problem 8
UPDATE TESTEMP
SET WORKDEPT = 'E01'
WHERE EMPNO = '000100'
Rows updated: 1
Problem 9
INSERT INTO TESTEMP
VALUES ('000360', 'BROWN ', 'D01', CURRENT DATE, 45000, NULL )

-- For the above syntax to work a value must be provided for every
-- column, in the sequence into which the values must go

or

INSERT INTO TESTEMP


(EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY )
VALUES ('000360', 'BROWN ', 'D01', CURRENT DATE, 45000 )
-- Column names and data values have a one to one correspondence,
-- therefore, the first value with go into the first column named,
-- the second value will go into the second column named, etc.
-- Columns receiving their default values may be omitted from
-- this syntax.

7-12 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

Problem 10
DELETE FROM TESTEMP;
SELECT * FROM TESTEMP;
DROP TABLE TESTEMP;
SELECT * FROM TESTEMP;

END OF SOLUTION

 Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data 7-13


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

7-14 DB2 SQL Workshop  Copyright IBM Corp. 1999, 2000


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

You might also like