Sei sulla pagina 1di 114

Business

Intelligence

Data
Centre

Cloud

Mobility

Enterprise Computing Solutions

Student Manual

Direccin General de Formacin


CONSEJERA DE EMPLEO,
TURISMO Y CULTURA

Comunidad de Madrid

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

EDUCATION
S

V6.0

cover

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

Front cover

DB2 SQL Workshop


(Course code CE121)

Student Exercises

pr

Ex

cl

ERC 6.1

IBM certified course material

Student Exercises

Trademarks
IBM and the IBM logo are registered trademarks of International Business Machines
Corporation.
The following are trademarks of International Business Machines Corporation, registered in
many jurisdictions worldwide:
DB2
RACF
400

Informix
z/OS

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

AIX
QMF
zSeries

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

Windows is a trademark of Microsoft Corporation in the United States, other countries, or


both.
UNIX is a registered trademark of The Open Group in the United States and other
countries.

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

Ex

cl

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

pr

February 2013 edition

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

Copyright International Business Machines Corporation 2013.


This document may not be reproduced in whole or in part without the prior written permission of IBM.
Note to U.S. Government Users Documentation related to restricted rights Use, duplication or disclosure is subject to restrictions
set forth in GSA ADP Schedule Contract with IBM Corp.

V7.0
Student Exercises

Contents
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v

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

Exercises description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii


General Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii
General Information for DB2 Users on z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
General Information for DB2 Users on Workstations . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
General Information for Self-paced and Elab Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
Table Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Table Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Exercise instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv
Exercise 1. Simple SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1
Problem List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Problem List with Expected Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12

2-1
2-2
2-4
2-9

Exercise 3. Scalar Functions and Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


Problem List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Problem List with Expected Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3-1
3-2
3-5
3-9

Exercise 4. Column Functions and Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


Problem List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Problem List with Expected Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4-1
4-2
4-3
4-6

cl

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


Problem List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Problem List With Expected Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Exercise 5. UNION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Problem List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Problem List with Expected Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5-1
5-2
5-4
5-7

Exercise 6. Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


Problem List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Problem List with Expected Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6-1
6-2
6-3
6-5

pr

Ex

TOC

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


Problem List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2
Problem List with Expected Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4

Copyright IBM Corp. 2013

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

iii

Student Exercises

pr

Ex

cl

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

Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-13

iv

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V7.0
Student Exercises

Trademarks
The reader should recognize that the following terms, which appear in the content of this
training document, are official trademarks of IBM or other companies:
IBM is a registered trademark of International Business Machines Corporation.

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

The following are trademarks of International Business Machines Corporation in the United
States, or other countries, or both:
AIX
QMF
zSeries

DB2
RACF
400

Informix
z/OS

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

Windows is a trademark of Microsoft Corporation in the United States, other countries, or


both.
UNIX is a registered trademark of The Open Group in the United States and other
countries.

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

cl

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

pr

Ex

TMK

Copyright IBM Corp. 2013

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

pr

Ex

cl

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

Student Exercises

vi

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

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 can 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 can verify your results.

pr

Ex

cl

Solutions contains the query that solves the problem. There are
sometimes several possible solutions. Your solution might 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.

Copyright IBM Corp. 2013

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

vii

Student Exercises

General Information
This Exercise Guide provides the information necessary to complete
the labs for the SQL Workshop course.

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

In environments that provide several query tools for each environment,


this lab guide can be supplemented by additional documentation
describing the basics of each query tool.
The exercises for this course can be done using DB2 in z/OS, or DB2
in a workstation environment.

You can 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 are 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.

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

Note

There might be slight differences in the Expected Results shown in the


notes.

pr

Ex

cl

In the Expected Results, if we have used a particular date format, bear


in mind that it might vary on your particular setup.

viii

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

General Information for DB2 Users on z/OS


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

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

___________________________________________________
___________________________________________________
___________________________________________________

The instructor will tell you how to log on to the z/OS system and what
specific userid and password to use. In addition, you will 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 data set in order to complete
the exercises. This data set already exists. The data set name is:
_____________________________________________________

Within your SQL it is not necessary to prefix the table names with its
qualifier.

When you create TESTEMP table during Exercise 7, you might need
to add an IN clause to your CREATE TABLE statements.

pr

Ex

cl

Each of the tables described in the Table Relationships section of this


document already exists and will be accessed through views owned by
your userid.

Copyright IBM Corp. 2013

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

ix

Student Exercises

General Information for DB2 Users on Workstations


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

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

__________________________________________________
__________________________________________________
__________________________________________________

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

PASSWORD is ibm2blue.

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

cl

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

The qualifier is _DB2ADMIN___________________________.

When you refer to these tables in your SQL, you might need to prefix
the table names with the qualifier identified above, if you are
connected to the database using a different ID than the qualifier.
Please ask your instructor to see if this is the case.

pr

Ex

Each of the tables described in the Table Relationships section of this


document already exists and has the same qualifier.

For example, if you want to display the complete contents of the


EMPLOYEE table, you might have to enter the following, substituting
the qualifier you were given:
SELECT * FROM qualifier.EMPLOYEE

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

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:

Uempty

SELECT * FROM TESTEMP


or:

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

SELECT * FROM logon ID.TESTEMP

pr

Ex

cl

If you choose to do the exercises in CLP, you can create your SQL in
files in a specific directory. You might already have a \CF12 directory; if
not, create a directory of your choice.

Copyright IBM Corp. 2013

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

xi

Student Exercises

General Information for Self-paced and Elab Users

General Information for Self-Paced or eLab Users

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

Logon to Citrix

Copyright IBM Corporation 2010

Figure 1. General Information for Self-Paced or eLab Users

CE1216.1

Notes:

pr

Ex

cl

Invoke your Web browser, and go to the URL as indicated in the email message sent to all
students. Use the ID and password provided as your credentials to logon to the Citrix
system.

xii

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

Starting VMware

Copyright IBM Corporation 2010

Figure 2. Starting VMware

CE1216.1

Notes:

pr

Ex

cl

Single-click the icon in order to launch the VMware software.

Copyright IBM Corp. 2013

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

xiii

Student Exercises

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

Initiate the VMware Host

...

Copyright IBM Corporation 2010

Figure 3. Initiate the VMware Host

CE1216.1

Notes:

pr

Ex

cl

Using the TCP/IP hostname or address, user name and password provided in the email
message to students, initiate the VMware host by keying in the appropriate information and
clicking OK.

xiv

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

Initiate the Windows lab environment

Copyright IBM Corporation 2010

Figure 4. Initiate the Windows lab environment

CE1216.1

Notes:

pr

Ex

cl

On the left-hand side of the screen, under Inventory, start the session labeled cf120 by
doing a right-click and then choose Open. This will initiate a Windows XP session.

Copyright IBM Corp. 2013

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

xv

Student Exercises

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

Start the Command Line Processor

Copyright IBM Corporation 2010

Figure 5. Start the Command Line Processor

CE1216.1

Notes:

cl

In the Windows session, select Start > Programs > IBM DB2 > DB2COPY1 > Command
Line Tools > Command Line Processor, to start the CLP session.
Note

Ex

Windows USERID is db2admin.

pr

Windows PASSWORD is ibm2blue.

xvi

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

Connect to the SAMPLE database

Copyright IBM Corporation 2010

Figure 6. Connect to the SAMPLE database

CE1216.1

Notes:

cl

At the DB2 prompt, type in connect to sample and press the Enter key in order to access
the sample database, which forms the basis for all of the SQL exercises.
Note

pr

Ex

You also have the option of invoking the Command Window. With the command window,
you need to prefix each command with db2 in order to route the request. Or optionally, you
can type in db2 by itself, and press the Enter key. This in effect puts you into Command
Line Processor mode.

Copyright IBM Corp. 2013

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

xvii

Student Exercises

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

Setting the CURRENT SQLID special register

Copyright IBM Corporation 2010

Figure 7. Setting the Current SQLID special register

CE1216.1

Notes:

Next you will need to set the special register to the owner of the tables used in lab by typing
in the statement set current sqlid = 'DB2ADMIN' and pressing the Enter key.

Ex

cl

Please note that you need to use uppercase on this value, surrounded by apostrophes
(single quotes). Also, you cannot have an underscore between current and sqlid in this
environment (in some environments, the special register can be referred to as either
current_sqlid or current sqlid).

pr

From this point on, you can proceed with the exercises. When you are finished for the day,
you can issue the command quit to leave the DB2 environment. You then should close the
CLP window by clicking the X in the upper right-hand corner.

xviii DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

Sample query

Copyright IBM Corporation 2010

Figure 8. Sample query

CE1216.1

Notes:

pr

Ex

cl

Here is an example of an SQL query, using the Command Line Processor. After the DB2
prompt, type in select * from department and press the Enter key.

Copyright IBM Corp. 2013

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

xix

Student Exercises

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

Sample query results

Copyright IBM Corporation 2010

Figure 9. Sample query results

CE1216.1

Notes:

pr

Ex

cl

The Command Line Processor executes the SQL query and returns the results to the user
as shown above. You can continue to type other SQL statements and/or DB2 commands or
leave the Command Line Processor, by typing EXIT at the prompt and then press Enter.

xx

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

Command Editor option: Input

connect to sample

Copyright IBM Corporation 2010

Figure 10. Command Editor option: Input

CE1216.1

Notes:

cl

If you choose the Command Editor instead of the Command Line Processor for your lab
environment, this dialog window will appear. Many students find this GUI to be easier to
use than either the Command Window or Command Line Processor.
The upper section is for user input, and the lower section displays the output.

Ex

To do work in the Command Editor, you must first connect to a database.

pr

For example, you can type in the connect to sample command and then click the green
triangle (highlighted by the arrow) in order to gain access to the SAMPLE database that is
used in the exercises.

Copyright IBM Corp. 2013

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

xxi

Student Exercises

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

Command Editor option: Output

connect to sample

Jim Hammonds/Atlanta/IBM

connect to sample

Database Connection Information

Database server

= DB2/NT 9.5.0

SQL authorization ID = ADMINIST...


Local database alias = SAMPLE

A JDBC connection to the target has succeeded.

Copyright IBM Corporation 2010

Figure 11. Command Editor option: Output

CE1216.1

Notes:

cl

After execution, the DB2 Command Editor displays the results of the user command in the
lower section of the GUI panel. From here, the user can continue with the exercise as
outlined above (setting the current sqlid, and then proceeding to the exercises).

Ex

Note

pr

If you want to clear the screen, you can press Ctrl+R, or you can select the Clear Results
option from the Edit drop-down menu just above the toolbar, as indicated by the arrow.

xxii DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

Command Editor option: Sample query

select * from department

Copyright IBM Corporation 2010

Figure 12. Command Editor option: Sample query

CE1216.1

Notes:

pr

Ex

cl

In this sample query, the user types in select * from department and then clicks the green
triangle (highlighted by the arrow) in order to run the query and display the (results)
contents of the DEPARTMENT table.

Copyright IBM Corp. 2013

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

xxiii

Student Exercises

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

Command Editor option: Sample results

Copyright IBM Corporation 2010

Figure 13. Command Editor option: Sample results

CE1216.1

Notes:

pr

Ex

cl

DB2 executes the SQL query and then displays the results under the Query Results tab,
as indicated by the arrow. If the results fill up more than the lower window of the display, a
scroll bar will appear on the right-hand side of the results.

xxiv DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

Command Editor option: Multiple statements

Copyright IBM Corporation 2010

Figure 14. Command Editor option: Multiple statements

CE1216.1

Notes:

Note

Ex

cl

At the bottom of this panel, DB2 displays the default statement termination character, which
is normally the semicolon. If you enter only one SQL statement in the upper section, this
termination character is optional. If you enter more than one SQL statement, the
termination character is required for each statement except for the last one.

pr

In the example above, you will notice the semicolon at the end of the first of the two SQL
statements.

To leave the Command Editor, click the X in the upper right-hand corner of the panel, or
click the C> in the upper left-hand corner of the panel, select and click Close.

Copyright IBM Corp. 2013

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

xxv

Student Exercises

Table relationships

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

DEPARTMENT Table
DEPTNO

MGRNO

EMPLOYEE Table
EMPNO

WORKDEPT

PROJECT Table
PROJNO

DEPTNO RESPEMP

EMP_ACT Table

EMPNO PROJNO

Copyright IBM Corporation 2010

Figure 15. Table relationships

CE1216.1

Notes:

pr

Ex

cl

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

xxvi DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

DEPARTMENT
There is one row in the DEPARTMENT table for each department in the company.
Column Name
DEPTNO
DEPTNAME
MGRNO

ADMRDEPT
LOCATION

Meaning
Department Number
Department Name
Employee Number of
the Responsible
Manager
Department Number
of the Department to
which the Department
reports
Location Number

Data Type
CHAR(3)
VARCHAR(36)

NULLS allowed
N
N

CHAR(6)

CHAR(3)

CHAR(16)

EMPLOYEE

There is one row in the EMPLOYEE table for each of the employees in the company.
Column Name
EMPNO
FIRSTNME
MIDINIT
LASTNAME
WORKDEPT

Ex

cl

PHONENO
HIREDATE
JOB
EDLEVEL

pr

SEX

BIRTHDATE
SALARY
BONUS
COMM

Copyright IBM Corp. 2013

Meaning
Employee Number
First Name
Middle Initial
Last Name
Department in which
the Employee Works
Phone Number
Date of Hire
Job
Number of Years of
Formal Education
Sex (M male, F
female)
Date of Birth
Yearly Salary
Yearly Bonus
Yearly Commission

Data Type
CHAR(6)
VARCHAR(12)
CHAR(1)
VARCHAR(15)

NULLS allowed
N
N
N
N

CHAR(3)

CHAR(4)
DATE
CHAR(8)

Y
Y
Y

SMALLINT

CHAR(1)

DATE
DECIMAL(9, 2)
DECIMAL(9, 2)
DECIMAL(9, 2)

Y
Y
Y
Y

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

xxvii

Student Exercises

PROJECT
There is one row in the PROJECT table for each project.
Meaning
Project Number
Project Name
Responsible
Department
Employee Number of
the Responsible
Employee
Estimated Mean
Staffing
Estimated Start Date
Estimated End Date
Major Project for a
Subproject

Data Type
CHAR(6)
VARCHAR(24)

NULLS allowed
N
N

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

Column Name
PROJNO
PROJNAME
DEPTNO

RESPEMP
PRSTAFF

PRSTDATE
PRENDATE
MAJPROJ

CHAR(3)

CHAR(6)

DECIMAL(5, 2)

DATE
DATE

Y
Y

CHAR(6)

EMP_ACT

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

PROJNO
ACTNO

EMPTIME

Data Type

NULLS allowed

CHAR(6)

CHAR(6)
SMALLINT

N
N

DECIMAL(5, 2)

DATE
DATE

Y
Y

pr

Ex

cl

EMSTDATE
EMENDATE

Meaning
Employee Number of
Employee Performing
the Activity
Project Number
Activity Number
Proportion of
Employee's Time
Spent on Project
Date Activity Starts
Date Activity Ends

xxviii DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Table Content
DEPARTMENT table
DEPTNAME
-----------------------------SPIFFY COMPUTER SERVICE DIV.
PLANNING
INFORMATION CENTER
DEVELOPMENT CENTER
MANUFACTURING SYSTEMS
ADMINISTRATION SYSTEMS
SUPPORT SERVICES
OPERATIONS
SOFTWARE SUPPORT

MGRNO
-----000010
000020
000030
000060
000070
000050
000090
000100

ADMRDEPT LOCATION
-------- -------A00
A00
A00
A00
D01
D01
A00
E01
E01

pr

Ex

cl

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

DEPTNO
-----A00
B01
C01
D01
D11
D21
E01
E11
E21

Copyright IBM Corp. 2013

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

xxix

Student Exercises

EMPLOYEE table
FIRSTNME
---------CHRISTINE
MICHAEL
SALLY
JOHN
IRVING
EVA
EILEEN
THEODORE
VINCENZO
SEAN
DOLORES
HEATHER
BRUCE
ELIZABETH
MASATOSHI
MARILYN
JAMES
DAVID
WILLIAM
JENNIFER
JAMES
SALVATORE
DANIEL
SYBIL
MARIA
ETHEL
JOHN
PHILIP
MAUDE
RAMLAL
WING
JASON

MIDINIT
------I
L
A
B
F
D
W
Q
G

LASTNAME
---------HAAS
THOMPSON
KWAN
GEYER
STERN
PULASKI
HENDERSON
SPENSER
LUCCHESI
O'CONNELL
QUINTANA
NICHOLLS
ADAMSON
PIANKA
YOSHIMURA
SCOUTTEN
WALKER
BROWN
JONES
LUTZ
JEFFERSON
MARINO
SMITH
JOHNSON
PEREZ
SCHNEIDER
PARKER
SMITH
SETRIGHT
MEHTA
LEE
GOUNOT

WORKDEPT
-------A00
B01
C01
E01
D11
D21
E11
E21
A00
A00
C01
C01
D11
D11
D11
D11
D11
D11
D11
D11
D21
D21
D21
D21
D21
E11
E11
E11
E11
E21
E21
E21

PHONENO
------3978
3476
4738
6789
6423
7831
5498
0972
3490
2167
4578
1793
4510
3782
2890
1682
2986
4501
0942
0672
4265
3780
0961
8953
9001
8997
4502
2095
3332
9990
2103
5698

HIREDATE
---------1965-01-01
1973-10-10
1975-04-05
1949-08-17
1973-09-14
1980-09-30
1970-08-15
1980-06-19
1958-05-16
1963-12-05
1971-07-28
1976-12-15
1972-02-12
1977-10-11
1978-09-15
1973-07-07
1974-07-26
1966-03-03
1979-04-11
1968-08-29
1966-11-21
1979-12-05
1969-10-30
1975-09-11
1980-09-30
1967-03-24
1980-05-30
1972-06-19
1964-09-12
1965-07-07
1976-02-23
1947-05-05

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

EMPNO
-----000010
000020
000030
000050
000060
000070
000090
000100
000110
000120
000130
000140
000150
000160
000170
000180
000190
000200
000210
000220
000230
000240
000250
000260
000270
000280
000290
000300
000310
000320
000330
000340

M
A

R
J
S
H

pr

Ex

cl

T
K
J
M
S
V
L
R
R
X
F
V

xxx

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

EMPLOYEE table (continued)


EDLEVEL SEX
------- --18
F
18
M
20
F
16
M
16
M
16
F
16
F
14
M
19
M
14
M
16
F
18
F
16
M
17
F
16
M
17
F
16
M
16
M
17
M
18
F
14
M
17
M
15
M
16
F
15
F
17
F
12
M
14
M
12
F
16
M
14
M
16
M

BIRTHDATE
---------1933-08-14
1948-02-02
1941-05-11
1925-09-15
1945-07-07
1953-05-26
1941-05-15
1956-12-18
1929-11-05
1942-10-18
1925-09-15
1946-01-19
1947-05-17
1955-04-12
1951-01-05
1949-02-21
1952-06-25
1941-05-29
1953-02-23
1948-03-19
1935-05-30
1954-03-31
1939-11-12
1936-10-05
1953-05-26
1936-03-28
1946-07-09
1936-10-27
1931-04-21
1932-08-11
1941-07-18
1926-05-17

SALARY
--------52750.00
41250.00
38250.00
40175.00
32250.00
36170.00
29750.00
26150.00
46500.00
29250.00
23800.00
28420.00
25280.00
22250.00
24680.00
21340.00
20450.00
27740.00
18270.00
29840.00
22180.00
28760.00
19180.00
17250.00
27380.00
26250.00
15340.00
17750.00
15900.00
19950.00
25370.00
23840.00

BONUS
-------1000.00
800.00
800.00
800.00
600.00
700.00
600.00
500.00
900.00
600.00
500.00
600.00
500.00
400.00
500.00
500.00
400.00
600.00
400.00
600.00
400.00
600.00
400.00
300.00
500.00
500.00
300.00
400.00
300.00
400.00
500.00
500.00

COMM
-------4220.00
3300.00
3060.00
3214.00
2580.00
2893.00
2380.00
2092.00
3720.00
2340.00
1904.00
2274.00
2022.00
1780.00
1974.00
1707.00
1636.00
2217.00
1462.00
2387.00
1774.00
2301.00
1534.00
1380.00
2190.00
2100.00
1227.00
1420.00
1272.00
1596.00
2030.00
1907.00

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

JOB
--------PRES
MANAGER
MANAGER
MANAGER
MANAGER
MANAGER
MANAGER
MANAGER
SALESREP
CLERK
ANALYST
ANALYST
DESIGNER
DESIGNER
DESIGNER
DESIGNER
DESIGNER
DESIGNER
DESIGNER
DESIGNER
CLERK
CLERK
CLERK
CLERK
CLERK
OPERATOR
OPERATOR
OPERATOR
OPERATOR
FIELDREP
FIELDREP
FIELDREP

pr

Ex

cl

Uempty

Copyright IBM Corp. 2013

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

xxxi

Student Exercises

PROJECT table
DEPT
PROJNAME
NO
-------------------- ---ADMIN SERVICES
D01
GENERAL AD SYSTEMS
D21
PAYROLL PROGRAMMING D21
PERSONNEL PROGRAMMG D21
ACCOUNT.PROGRAMMING D21
QUERY SERVICES
C01
USER EDUCATION
C01
WELD LINE AUTOMATION D01
W L PROGRAMMING
D11
W L PROGRAM DESIGN
D11
W L ROBOT DESIGN
D11
W L PROD CONT PROGS D11
OPERATION SUPPORT
E01
OPERATION
E11
GEN SYSTEMS SERVICES E01
SYSTEMS SUPPORT
E21
SCP SYSTEMS SUPPORT E21
APPLICATIONS SUPPORT E21
DB/DC SUPPORT
E21
WELD LINE PLANNING
B01

PR
RESPEMP STAFF
------- ----000010
6.50
000070
6.00
000230
2.00
000250
1.00
000270
2.00
000030
2.00
000030
1.00
000010 12.00
000060
9.00
000220
2.00
000150
3.00
000160
3.00
000050
6.00
000090
5.00
000050
5.00
000100
4.00
000320
1.00
000330
1.00
000340
1.00
000020
1.00

PRSTDATE
---------1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-02-15
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01

PRENDATE
---------1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1982-12-01
1982-12-01
1982-12-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1982-09-15

MAJPROJ
------AD3100
AD3110
AD3110
AD3110
MA2100
MA2110
MA2110
MA2110
OP1000
OP2000
OP2010
OP2010
OP2010
MA2100

pr

Ex

cl

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

PROJNO
-----AD3100
AD3110
AD3111
AD3112
AD3113
IF1000
IF2000
MA2100
MA2110
MA2111
MA2112
MA2113
OP1000
OP1010
OP2000
OP2010
OP2011
OP2012
OP2013
PL2100

xxxii DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

EMP_ACT table
PROJNO ACTNO
------ -----AD3100
10
AD3110
10
AD3111
60
AD3111
60
AD3111
70
AD3111
80
AD3111
180
AD3111
70
AD3111
80
AD3112
60
AD3112
60
AD3112
60
AD3112
60
AD3112
70
AD3112
70
AD3112
70
AD3112
80
AD3112
80
AD3112
180
AD3113
70
AD3113
70
AD3113
80
AD3113
80
AD3113
180
AD3113
180
AD3113
180
AD3113
60
AD3113
60
AD3113
60
AD3113
70
AD3113
70
AD3113
80
AD3113
80
IF1000
10
IF1000
90
IF1000
100
IF1000
90

EMPTIME
-------0.50
1.00
1.00
0.50
0.50
0.50
1.00
1.00
1.00
0.50
1.00
1.00
0.50
1.00
0.50
0.25
0.25
0.50
0.50
0.50
1.00
1.00
0.50
0.50
0.50
1.00
0.25
1.00
0.50
0.75
1.00
1.00
0.50
0.50
1.00
0.50
0.50

EMSTDATE
---------1982-01-01
1982-01-01
1982-01-01
1982-03-15
1982-03-15
1982-04-15
1982-10-15
1982-02-15
1982-09-15
1982-02-01
1982-01-01
1983-01-01
1982-12-01
1982-03-15
1982-02-01
1982-08-15
1982-08-15
1982-10-15
1982-08-15
1982-06-15
1982-07-01
1982-01-01
1982-03-01
1982-03-01
1982-06-01
1982-04-15
1982-09-01
1982-04-01
1982-03-01
1982-09-01
1982-10-15
1982-01-01
1982-03-01
1982-06-01
1982-01-01
1982-10-01
1982-10-01

EMENDATE
---------1982-07-01
1983-02-01
1982-03-15
1982-04-15
1982-10-15
1982-10-15
1983-01-01
1982-09-15
1983-01-01
1982-03-15
1982-02-01
1983-02-01
1983-01-01
1982-08-15
1982-03-15
1982-10-15
1982-10-15
1982-12-01
1983-01-01
1982-07-01
1983-02-01
1982-03-01
1982-04-15
1982-04-15
1982-07-01
1982-06-01
1982-10-15
1982-09-01
1982-04-01
1982-10-15
1983-02-01
1982-03-01
1982-04-01
1983-01-01
1982-10-01
1983-01-01
1983-01-01

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

EMPNO
-----000010
000070
000230
000230
000230
000230
000230
000240
000240
000250
000250
000250
000250
000250
000250
000250
000250
000250
000250
000260
000260
000260
000260
000260
000260
000260
000270
000270
000270
000270
000270
000270
000270
000030
000130
000130
000140

pr

Ex

cl

Uempty

Copyright IBM Corp. 2013

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

xxxiii

Student Exercises

EMP_ACT table (continued)


PROJNO ACTNO
------ -----IF2000
10
IF2000
100
IF2000
100
IF2000
110
IF2000
110
MA2100
10
MA2100
20
MA2110
10
MA2111
50
MA2111
60
MA2111
40
MA2112
60
MA2112
180
MA2112
60
MA2112
70
MA2112
70
MA2112
80
MA2113
60
MA2113
80
MA2113
70
MA2113
80
MA2113
180
OP1000
10
OP1010
10
OP1010
130
OP1010
130
OP1010
130
OP1010
130
OP2010
10
OP2010
10
OP2011
140
OP2011
150
OP2012
140
OP2012
160
OP2013
140
OP2013
170
PL2100
30

EMPTIME
-------0.50
1.00
0.50
0.50
0.50
0.50
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
0.50
0.50
0.25
1.00
1.00
1.00
1.00
1.00
0.75
1.00
0.75
0.25
0.25
0.75
0.50
0.50
1.00

EMSTDATE
---------1982-01-01
1982-01-01
1982-03-01
1982-03-01
1982-10-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-06-15
1982-01-01
1982-01-01
1982-07-15
1982-01-01
1982-06-01
1982-02-01
1982-10-01
1982-07-15
1982-01-01
1982-04-01
1982-10-01
1982-10-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01
1982-01-01

EMENDATE
---------1983-01-01
1982-03-01
1982-07-01
1982-07-01
1983-01-01
1982-11-01
1982-03-01
1983-02-01
1982-06-15
1983-02-01
1983-02-01
1982-07-15
1983-02-01
1983-06-01
1983-02-01
1982-10-01
1983-10-01
1983-02-01
1983-02-01
1982-06-15
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1983-02-01
1982-09-15

pr

Ex

cl

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

EMPNO
-----000030
000140
000140
000140
000140
000010
000110
000010
000200
000200
000220
000150
000150
000170
000170
000190
000190
000160
000170
000180
000210
000210
000050
000090
000280
000290
000300
000310
000050
000100
000320
000320
000330
000330
000340
000340
000020

xxxiv DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

Problem List This section contains what you should 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.

pr

Ex

cl

Solutions Follow the Problem List With Expected Results.

Copyright IBM Corp. 2013

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

xxxv

pr

Ex

cl

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

Student Exercises

xxxvi DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Exercise 1. Simple SQL Queries


What this exercise is about

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

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

cl

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

Student handout
SQL Reference

pr

Ex

Requirements

Copyright IBM Corp. 2013

Exercise 1. Simple SQL Queries


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

1-1

Student Exercises

Problem List
Problem 1

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

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

cl

List last name, salary, and commission for all employees with a salary greater than
$20,000 and hired after 1979.

Ex

Problem 7

pr

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.

1-2

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Problem 9
Using the EMP_ACT table, for all projects that have a project number beginning with
AD and have activities 10, 80, or 180 associated with them, list the following:
Project number
Activity number
Starting date for activity
Ending date for activity

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

Order the list by activity number within project number.

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

cl

List all projects that have SUPPORT as part of the project name. Order the results
by project number.

Ex

Problem 14

List all departments that have a 1 as the middle character in the department number.
Order the results by department number.

pr

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 exercise
Copyright IBM Corp. 2013

Exercise 1. Simple SQL Queries


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

1-3

Student Exercises

Problem List with Expected Results


Problem 1

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

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

Note: In DB2 LUW, the output format of the datecolumn might vary. For example, the
BIRTHDATE for Hass may display as Aug 14, 1933. This may apply to other results sets
throughout the lab exercises.

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

pr

Ex

cl

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


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

V6.0
Student Exercises

Uempty

Problem 3
List the different education levels in the company in descending order. List only one
occurrence of duplicate result rows.
Result

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

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

pr

Ex

cl

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

Exercise 1. Simple SQL Queries


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

1-5

Student Exercises

Problem 5
List last name, salary, and bonus of all male employees.
Result

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

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.

cl

Result

pr

Ex

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


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

V6.0
Student Exercises

Uempty

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

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

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

pr

Ex

cl

LASTNAME
SALARY
BONUS
COMM
---------+---------+---------+---------+---------+---JEFFERSON
22180.00
400.00
1774.00
PIANKA
22250.00
400.00
1780.00

Copyright IBM Corp. 2013

Exercise 1. Simple SQL Queries


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

1-7

Student Exercises

Problem 9
Using the EMP_ACT table, for all projects that have a project number beginning with
AD and have activities 10, 80, or 180 associated with them, list the following:
Project number
Activity number
Starting date for activity
Ending date for activity

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

Order the list by activity number within project number.


Result

pr

Ex

cl

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


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

V6.0
Student Exercises

Uempty

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.

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

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

LASTNAME
SALARY
BONUS
--------------- ------------ -----------THOMPSON
41250.00
800.00
KWAN
38250.00
800.00
GEYER
40175.00
800.00
LUCCHESSI
46500.00
900.00
HAAS
52750.00
1000.00

pr

Ex

cl

EMPNO
-----000020
000030
000050
000110
000010

Copyright IBM Corp. 2013

Exercise 1. Simple SQL Queries


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

1-9

Student Exercises

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.

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

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

cl

Problem 14

List all departments that have a 1 as the middle character in the department number.

Ex

Order the results by department number.


Result

pr

DEPTNO DEPTNAME
---------+---------+---------+---------+
D11
MANUFACTURING SYSTEMS
E11
OPERATIONS

1-10 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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.

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

Result
LASTNAME
--------------LUCCHESSI
LUTZ
O'CONNELL
MARINO
NICHOLLS

FIRSTNME
-----------VINCENZO
JENNIFER
SEAN
SALVATORE
HEATHER

MIDINIT
SALARY
------- -----------G
46500.00
K
29840.00
29250.00
M
28760.00
A
28420.00

pr

Ex

cl

End of Problem List with Expected Results

Copyright IBM Corp. 2013

Exercise 1. Simple SQL Queries


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

1-11

Student Exercises

Solutions
Problem 1

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

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

cl

SELECT LASTNAME, SALARY, BONUS


FROM EMPLOYEE
WHERE SEX = 'M';

Problem 6

pr

Ex

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


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

V6.0
Student Exercises

Uempty

Note
Parentheses are not required as the AND operator takes precedence over the OR
operator. However, the use of parentheses is recommended to improve clarity when
reading the SQL.

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

Problem 7 (Recommended Solution)


SELECT LASTNAME, SALARY, BONUS, COMM
FROM EMPLOYEE
WHERE (SALARY > 22000 AND BONUS = 400)
OR (BONUS = 500 AND COMM < 1900)
ORDER BY LASTNAME;

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;

Ex

cl

Problem 10

SELECT MGRNO, DEPTNO


FROM DEPARTMENT
WHERE MGRNO IS NOT NULL
ORDER BY MGRNO;

pr

Problem 11

SELECT EMPNO, LASTNAME, SALARY, BONUS


FROM EMPLOYEE
WHERE BONUS BETWEEN 800 AND 1000
ORDER BY BONUS, EMPNO;

Copyright IBM Corp. 2013

Exercise 1. Simple SQL Queries


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

1-13

Student Exercises

Problem 12
SELECT EMPNO, LASTNAME, SALARY, WORKDEPT
FROM EMPLOYEE
WHERE WORKDEPT BETWEEN 'A00' AND 'C01'
ORDER BY LASTNAME, EMPNO;

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

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;

Problem 15

SELECT LASTNAME, FIRSTNME, MIDINIT, SALARY


FROM EMPLOYEE
WHERE JOB NOT IN ('PRES', 'MANAGER')
ORDER BY SALARY DESC
FETCH FIRST 5 ROWS ONLY;

pr

Ex

cl

End of solutions

1-14 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Exercise 2. Retrieving Data from Multiple Tables


What this exercise is about

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

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.

Requirements

pr

Ex

cl

Student handout
SQL Reference

Copyright IBM Corp. 2013

Exercise 2. Retrieving Data from Multiple Tables


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

2-1

Student Exercises

Problem List
Problem 1

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

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.

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

Ex

cl

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

pr

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 7

The second line manager needs a list of activities which began on October 15, 1982
or thereafter.
2-2

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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.

pr

Ex

cl

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

End of exercise

Copyright IBM Corp. 2013

Exercise 2. Retrieving Data from Multiple Tables


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

2-3

Student Exercises

Problem List With Expected Results


Problem 1

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

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

pr

Ex

cl

LASTNAME
--------------JEFFERSON
JOHNSON
MARINO
PEREZ
PULASKI
SMITH
KWAN
NICHOLLS
QUINTANA
ADAMSON
BROWN
JONES
LUTZ
PIANKA
SCOUTTEN
STERN
WALKER
YOSHIMURA
HENDERSON
PARKER
SCHNEIDER
SETRIGHT
SMITH
THOMPSON
GOUNOT
LEE
MEHTA
SPENSER
HAAS
LUCCHESSI
O'CONNELL
GEYER

2-4

FIRSTNME
-----------JAMES
SYBIL
SALVATORE
MARIA
EVA
DANIEL
SALLY
HEATHER
DOLORES
BRUCE
DAVID
WILLIAM
JENNIFER
ELIZABETH
MARILYN
IRVING
JAMES
MASATOSHI
EILEEN
JOHN
ETHEL
MAUDE
PHILIP
MICHAEL
JASON
WING
RAMLAL
THEODORE
CHRISTINE
VINCENZO
SEAN
JOHN

DEPTNAME
----------------------------ADMINISTRATION SYSTEMS
ADMINISTRATION SYSTEMS
ADMINISTRATION SYSTEMS
ADMINISTRATION SYSTEMS
ADMINISTRATION SYSTEMS
ADMINISTRATION SYSTEMS
INFORMATION CENTER
INFORMATION CENTER
INFORMATION CENTER
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
OPERATIONS
OPERATIONS
OPERATIONS
OPERATIONS
OPERATIONS
PLANNING
SOFTWARE SUPPORT
SOFTWARE SUPPORT
SOFTWARE SUPPORT
SOFTWARE SUPPORT
SPIFFY COMPUTER SERVICE DIV.
SPIFFY COMPUTER SERVICE DIV.
SPIFFY COMPUTER SERVICE DIV.
SUPPORT SERVICES

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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.
Result
FIRSTNME
----------JAMES
SYBIL
SALVATORE
MARIA
DANIEL
HEATHER
DOLORES
BRUCE
DAVID
WILLIAM
JENNIFER
ELIZABETH
MARILYN
JAMES
MASATOSHI

DEPTNAME
--------------------------ADMINISTRATION SYSTEMS
ADMINISTRATION SYSTEMS
ADMINISTRATION SYSTEMS
ADMINISTRATION SYSTEMS
ADMINISTRATION SYSTEMS
INFORMATION CENTER
INFORMATION CENTER
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS
MANUFACTURING SYSTEMS

JOB
-------CLERK
CLERK
CLERK
CLERK
CLERK
ANALYST
ANALYST
DESIGNER
DESIGNER
DESIGNER
DESIGNER
DESIGNER
DESIGNER
DESIGNER
DESIGNER

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

LASTNAME
-------------JEFFERSON
JOHNSON
MARINO
PEREZ
SMITH
NICHOLLS
QUINTANA
ADAMSON
BROWN
JONES
LUTZ
PIANKA
SCOUTTEN
WALKER
YOSHIMURA

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

pr

Ex

cl

DEPTNAME
----------------------------ADMINISTRATION SYSTEMS
INFORMATION CENTER
MANUFACTURING SYSTEMS
OPERATIONS
PLANNING
SOFTWARE SUPPORT
SPIFFY COMPUTER SERVICE DIV.
SUPPORT SERVICES

Copyright IBM Corp. 2013

LASTNAME
--------------PULASKI
KWAN
STERN
HENDERSON
THOMPSON
SPENSER
HAAS
GEYER

FIRSTNME
-----------EVA
SALLY
IRVING
EILEEN
MICHAEL
THEODORE
CHRISTINE
JOHN

Exercise 2. Retrieving Data from Multiple Tables


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

2-5

Student Exercises

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? __________________

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

Why?

__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
Result

DEPTNAME
----------------------------ADMINISTRATION SYSTEMS
INFORMATION CENTER
MANUFACTURING SYSTEMS
OPERATIONS
PLANNING
SOFTWARE SUPPORT
SUPPORT SERVICES

LASTNAME
--------------PULASKI
KWAN
STERN
HENDERSON
THOMPSON
SPENSER
GEYER

FIRSTNME
-----------EVA
SALLY
IRVING
EILEEN
MICHAEL
THEODORE
JOHN

Problem 5

pr

Ex

cl

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.

2-6

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Result

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

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

Problem 6

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

pr

Ex

cl

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

Copyright IBM Corp. 2013

Exercise 2. Retrieving Data from Multiple Tables


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

2-7

Student Exercises

Problem 7
The second line manager needs a list of activities which began on October 15, 1982
or thereafter.

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

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

pr

Ex

cl

End of Problem List with Expected Results

2-8

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Solutions
Problem 1

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

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

pr

Ex

cl

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;

Copyright IBM Corp. 2013

Exercise 2. Retrieving Data from Multiple Tables


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

2-9

Student Exercises

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

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

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;

pr

Ex

cl

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!

2-10 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Problem 5

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

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

pr

Ex

cl

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;

Copyright IBM Corp. 2013

Exercise 2. Retrieving Data from Multiple Tables


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

2-11

Student Exercises

Problem 7

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

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 P.DEPTNO = D.DEPTNO
AND A.EMSTDATE >= '1982-10-15'
ORDER BY A.ACTNO, A.EMSTDATE;

OR

SELECT A.ACTNO, D.MGRNO, A.EMSTDATE, P.PROJNO, 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 P.DEPTNO = D.DEPTNO
WHERE A.EMSTDATE >= '1982-10-15'
ORDER BY A.ACTNO, A.EMSTDATE;

pr

Ex

cl

End of solutions

2-12 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Exercise 3. Scalar Functions and Arithmetic


What this exercise is about

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

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.

Requirements

pr

Ex

cl

Student handout
SQL Reference

Copyright IBM Corp. 2013

Exercise 3. Scalar Functions and Arithmetic


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

3-1

Student Exercises

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

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

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 all of the
following criteria:

Ex

cl

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.

pr

List department number, last name, salary, and decreased salary. Sort the result by
salary.

3-2

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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.

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

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.

Problem 5

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 6

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 7

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.

cl

Name the derived column WEEKS.

pr

Ex

Order the list by the project number.

Copyright IBM Corp. 2013

Exercise 3. Scalar Functions and Arithmetic


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

3-3

Student Exercises

Problem 8
How many days are between the first manned landing on the moon (July 20, 1969)
and the first day of the year 2000?

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

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).
Name the derived column DAYS.

pr

Ex

cl

End of exercise

3-4

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

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.38
SMITH
17750.00
18637.50
1553.13
JONES
18270.00
19183.50
1598.63

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.

cl

Use the column names NEW_SALARY and NEW_BONUS for the generated
columns.

Ex

Employees with an education level of 20 should be listed first. For employees with
the same education level, sort the list by salary.
Result

pr

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

Exercise 3. Scalar Functions and Arithmetic


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

3-5

Student Exercises

Problem 3
The salary will be decreased by $1,000 for all employees matching all of the
following criteria:

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

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.

cl

Result

pr

Ex

WORKDEPT LASTNAME
INCOME
---------+---------+---------+---------+
D11
LUTZ
32827.00
D11
BROWN
30557.00
D11
YOSHIMURA
27154.00
D11
SCOUTTEN
23547.00
D11
JONES
20132.00

3-6

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

Sequence the results by PROJNO.


Result

PROJNO MAJOR_PROJECT
---------+---------+MA2100 MAIN PROJECT
MA2110 MA2100
MA2111 MA2110
MA2112 MA2110
MA2113 MA2110

Problem 6

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

cl

Problem 7

Ex

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.

pr

Order the list by the project number.

Copyright IBM Corp. 2013

Exercise 3. Scalar Functions and Arithmetic


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

3-7

Student Exercises

Result

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

PROJNO
WEEKS
---------+-------MA2100
56.6
MA2110
56.6
MA2111
47.7
MA2112
47.7
MA2113
41.3

Problem 8

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).
Name the derived column DAYS.
Result

DAYS
---------+11122

pr

Ex

cl

End of Problem List with Expected Results

3-8

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Solutions
Problem 1

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

SELECT LASTNAME, SALARY,


DECIMAL(ROUND(SALARY*1.05,2),8,2) AS INC_Y_SALARY,
DECIMAL(ROUND(SALARY*1.05/12,2),8,2) AS INC_M_SALARY
FROM EMPLOYEE
WHERE SALARY*1.05 <= 20000
ORDER BY SALARY;
Note

pr

Ex

cl

In DB2, the DECIMAL function is needed to specify that only the two decimal points from
the ROUND are displayed.

Copyright IBM Corp. 2013

Exercise 3. Scalar Functions and Arithmetic


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

3-9

Student Exercises

Problem 2

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

SELECT LASTNAME, EDLEVEL,


SALARY + 1200 AS NEW_SALARY,
DECIMAL(ROUND(BONUS*0.5,2),8,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(ROUND(BONUS*0.5,2),8,2) AS NEW_BONUS
FROM EMPLOYEE
WHERE EDLEVEL IN (18,20)
ORDER BY EDLEVEL DESC, NEW_SALARY;
Note

pr

Ex

cl

In DB2, the DECIMAL function is needed to specify that only the two decimal points from
the ROUND are displayed.

3-10 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Problem 3

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

SELECT WORKDEPT, LASTNAME, SALARY,


SALARY - 1000 as DECR_SALARY
FROM EMPLOYEE
WHERE SALARY BETWEEN 20000*0.8 AND 20000*1.2
AND WORKDEPT = 'D11'
ORDER BY SALARY;

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

pr

Ex

cl

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


FROM EMPLOYEE
WHERE SALARY+COMM+BONUS > 1.1*SALARY
AND WORKDEPT = 'D11'
ORDER BY INCOME DESC;

Copyright IBM Corp. 2013

Exercise 3. Scalar Functions and Arithmetic


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

3-11

Student Exercises

Problem 5

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

SELECT PROJNO,
COALESCE(MAJPROJ,'MAIN PROJECT') AS "MAJOR_PROJECT"
FROM PROJECT
WHERE SUBSTR(PROJNO,1,2) ='MA'
ORDER BY PROJNO;

Problem 6
DB2:

SELECT YEAR(PRSTDATE) AS YEAR, MONTH(PRSTDATE) AS MONTH, PROJNO


FROM PROJECT
WHERE PRENDATE = '1982-12-01'
ORDER BY PROJNO;

Problem 7
DB2:

SELECT PROJNO,
DECIMAL(ROUND((DAYS(PRENDATE) - DAYS(PRSTDATE)) /7.0 ,1),8,1) AS
WEEKS
FROM PROJECT
WHERE PROJNO LIKE 'MA%'
ORDER BY PROJNO;

Problem 8
DB2:

cl

SELECT DAYS('2000-01-01')-DAYS('1969-07-20') AS DAYS


FROM EMPLOYEE
WHERE EMPNO = '000010';

Ex

Note

In DB2, there is a table, SYSIBM.SYSDUMMY1, which can be used instead of a real table
to obtain a one-row answer of this nature.

pr

In DB2 for Linux, UNIX and Windows, you can even write:

VALUES(DAYS('2000-01-01') - DAYS('1969-07-20'))

End of solutions
3-12 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Exercise 4. Column Functions and Grouping


What this exercise is about

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

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.

Requirements

pr

Ex

cl

Student handout
SQL Reference

Copyright IBM Corp. 2013

Exercise 4. Column Functions and Grouping


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

4-1

Student Exercises

Problem List
Problem 1
For all departments, display department number and the sum of all salaries for that
department. Name the derived column SUM_SALARY.

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

Problem 2

For all departments, display the department number and the number of employees.
Name the derived column EMP_COUNT.

Problem 3

For those departments which have more than 3 employees, in ascending order.

Problem 4

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 5

cl

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.

pr

Ex

End of exercise

4-2

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Problem List with Expected Results


Problem 1
For all departments, display department number and the sum of all salaries for that
department. Name the derived column SUM_SALARY.

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

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

pr

Ex

cl

WORKDEPT EMP_COUNT
---------------A00
3
B01
1
C01
3
D11
9
D21
6
E01
1
E11
5
E21
4

Copyright IBM Corp. 2013

Exercise 4. Column Functions and Grouping


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

4-3

Student Exercises

Problem 3
For those departments which have more than 3 employees display the department
number and the number of employees in that department, in ascending department
number order.
Result
EMP_COUNT
--------9
6
5
4

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

WORKDEPT
-------D11
D21
E11
E21

Problem 4

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

pr

Ex

cl

WORKDEPT SEX AVG_SALARY


AVG_BONUS
AVG_COMM
COUNT
---------+---------+---------+---------+---------+------A00
M
37875.00
750.00
3030.00
2
C01
F
30156.67
633.33
2412.67
3
D11
F
24476.67
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.67
1869.67
3
E11
F
23966.67
466.67
1917.33
3
E11
M
16545.00
350.00
1323.50
2
E21
M
23827.50
475.00
1906.25
4

4-4

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

Result
WORKDEPT
AVG_BONUS
AVG_COMM
---------+---------+---------+---------+-----A00
833.33
3426.67
B01
800.00
3300.00
C01
633.33
2412.67
E01
800.00
3214.00

pr

Ex

cl

End of Problem List with Expected Results

Copyright IBM Corp. 2013

Exercise 4. Column Functions and Grouping


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

4-5

Student Exercises

Solutions
Problem 1

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

SELECT WORKDEPT, SUM(SALARY) AS SUM_SALARY


FROM EMPLOYEE
GROUP BY WORKDEPT
ORDER BY WORKDEPT;

Problem 2

SELECT WORKDEPT, COUNT(*) AS EMP_COUNT


FROM EMPLOYEE
GROUP BY WORKDEPT
ORDER BY WORKDEPT;

Problem 3

SELECT WORKDEPT, COUNT(*) AS EMP_COUNT


FROM EMPLOYEE
GROUP BY WORKDEPT
HAVING COUNT(*) > 3
ORDER BY WORKDEPT;

Problem 4

Ex

cl

SELECT WORKDEPT, SEX,


DECIMAL(ROUND(AVG(SALARY),2),8,2) AS AVG_SALARY,
DECIMAL(ROUND(AVG(BONUS),2),8,2) AS AVG_BONUS,
DECIMAL(ROUND(AVG(COMM),2),8,2) AS AVG_COMM,
COUNT(*) AS COUNT
FROM EMPLOYEE
GROUP BY WORKDEPT, SEX
HAVING COUNT(*) > 1
ORDER BY WORKDEPT, SEX

Problem 5

pr

SELECT WORKDEPT, DECIMAL(ROUND(AVG(BONUS),2),8,2) AS AVG_BONUS,


DECIMAL(ROUND(AVG(COMM),2),8,2) AS AVG_COMM
FROM EMPLOYEE
GROUP BY WORKDEPT
HAVING AVG(BONUS) > 500
AND AVG(COMM) > 2000
ORDER BY WORKDEPT;

End of solutions
4-6

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Exercise 5. UNION
What this exercise is about

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

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.

Requirements

pr

Ex

cl

Student handout
SQL Reference

Copyright IBM Corp. 2013

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

5-1

Student Exercises

Problem List
Problem 1

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

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
an appropriate ORDER BY clause to achieve the required results. Use the column
headings shown.
LASTNAME
--------------JEFFERSON
JEFFERSON
JOHNSON
JOHNSON
...

FIRSTNME
-----------JAMES
JAMES
SYBIL
SYBIL
...

WHEN
SALARY
-------------- -------------BEFORE A RAISE
22180.000
AFTER A RAISE
24398.000
BEFORE A RAISE
17250.000
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):

cl

Department number
Text: DEPARTMENT
Department name

Ex

Second line(s) (if data available - one line per project):


Department number
Project number
Project name

pr

Subsequent line(s) (if data available - one line per employee):


Department number
Employee number
Last name

5-2

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

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.

pr

Ex

cl

End of exercise

Copyright IBM Corp. 2013

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

5-3

Student Exercises

Problem List with Expected Results


Problem 1

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

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
an appropriate ORDER BY clause to achieve the required results. Use the column
headings shown.
LASTNAME
--------------JEFFERSON
JEFFERSON
JOHNSON
JOHNSON
...

FIRSTNME
-----------JAMES
JAMES
SYBIL
SYBIL
...

WHEN
SALARY
-------------- -------------BEFORE A RAISE
22180.000
AFTER A RAISE
24398.000
BEFORE A RAISE
17250.000
AFTER A RAISE
18975.000
...
...

FIRSTNME
-----------JAMES
JAMES
SYBIL
SYBIL
SALVATORE
SALVATORE
MARIA
MARIA
DANIEL
DANIEL

WHEN
SALARY
-------------- -------------BEFORE A RAISE
22180.000
AFTER A RAISE
24398.000
BEFORE A RAISE
17250.000
AFTER A RAISE
18975.000
BEFORE A RAISE
28760.000
AFTER A RAISE
31636.000
BEFORE A RAISE
27380.000
AFTER A RAISE
30118.000
BEFORE A RAISE
19180.000
AFTER A RAISE
21098.000

Result

LASTNAME
--------------JEFFERSON
JEFFERSON
JOHNSON
JOHNSON
MARINO
MARINO
PEREZ
PEREZ
SMITH
SMITH

Problem 2

cl

List the department number, employee number, and salaries of all employees in
department A00.

Ex

For the last line of the report, display the sum of all the salaries.
Result

pr

WORKDEPT EMPNO
SALARY
---------+---------+---------+-------A00
000120
29250.00
A00
000110
46500.00
A00
000010
52750.00
A00
SUM
128500.00

5-4

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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.

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

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
-----A00
A00
A00
A00
B01
B01
B01
C01
C01
C01
C01
C01
C01

INFO
---------DEPARTMENT
000010
000110
000120
DEPARTMENT
PL2100
000020
DEPARTMENT
IF1000
IF2000
000030
000130
000140

NAME
----------------------------SPIFFY COMPUTER SERVICE DIV.
HAAS
LUCCHESSI
O'CONNELL
PLANNING
WELD LINE PLANNING
THOMPSON
INFORMATION CENTER
QUERY SERVICES
USER EDUCATION
KWAN
QUINTANA
NICHOLLS

pr

Ex

cl

TYPE
----------1
3
3
3
1
2
3
1
2
2
3
3
3

Copyright IBM Corp. 2013

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

5-5

Student Exercises

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

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

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

cl

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

pr

Ex

End of Problem List with Expected Results

5-6

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Solutions
Problem 1

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

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

pr

Ex

cl

SELECT WORKDEPT, 'SUM' AS EMPNO, SUM(SALARY) AS SALARY


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

Copyright IBM Corp. 2013

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

5-7

Student Exercises

Problem 3

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

SELECT 1 AS TYPE, DEPTNO, 'DEPARTMENT' AS INFO, DEPTNAME AS NAME


FROM DEPARTMENT
WHERE DEPTNO IN ('A00', 'B01', 'C01')
UNION ALL
SELECT 2 AS TYPE, DEPTNO, PROJNO AS INFO, PROJNAME AS NAME
FROM PROJECT
WHERE DEPTNO IN ('A00', 'B01', 'C01')
UNION ALL
SELECT 3 AS TYPE, WORKDEPT AS DEPTNO, EMPNO AS INFO, LASTNAME AS
NAME
FROM EMPLOYEE
WHERE WORKDEPT IN ('A00', 'B01', 'C01')
ORDER BY DEPTNO, TYPE, INFO;

Problem 4

SELECT 'PROJECT', PROJNO, RESPEMP, PRSTDATE, PRENDATE


FROM PROJECT
WHERE PROJNO LIKE 'IF%'
UNION ALL

SELECT ' ', PROJNO, EMPNO AS RESPEMP, EMSTDATE AS PRSTDATE,


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

pr

Ex

cl

End of solutions

5-8

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Exercise 6. Using Subqueries


What this exercise is about

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

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.

Requirements

pr

Ex

cl

Student handout
SQL Reference

Copyright IBM Corp. 2013

Exercise 6. Using Subqueries


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

6-1

Student Exercises

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.

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

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 number 60. Show the last name, job, edlevel, and their salary.
Sort the listing by highest salary first.

pr

Ex

cl

End of exercise

6-2

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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.

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

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

pr

Ex

cl

EMPNO
LASTNAME
---------+------000060 STERN
000120 O'CONNELL

Copyright IBM Corp. 2013

Exercise 6. Using Subqueries


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

6-3

Student Exercises

Problem 3
List project number and duration (in days) of the project with the shortest duration.
Name the derived column DAYS.
Result

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

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 number 60. Show the last name, job, edlevel, and their salary.
Sort the listing by highest salary first.

Result

JOB
EDLEVEL
SALARY
-------- ------- -----------MANAGER
16
40175.00
MANAGER
16
36170.00
MANAGER
16
32250.00
MANAGER
16
29750.00

pr

Ex

cl

LASTNAME
--------------GEYER
PULASKI
STERN
HENDERSON

End of Problem List With Expected Results

6-4

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Solutions
Problem 1

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

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
DB2:

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

pr

Ex

cl

SELECT DEPTNO, DEPTNAME, LASTNAME, FIRSTNME


FROM DEPARTMENT, EMPLOYEE
WHERE DEPTNO = WORKDEPT
AND DEPTNO NOT IN(SELECT WORKDEPT
FROM EMPLOYEE
WHERE SEX = 'F');

Copyright IBM Corp. 2013

Exercise 6. Using Subqueries


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

6-5

Student Exercises

Problem 5

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

SELECT LASTNAME, JOB, EDLEVEL, SALARY


FROM EMPLOYEE
WHERE JOB IN
(SELECT JOB
FROM EMPLOYEE
WHERE EMPNO = '000060')
AND EDLEVEL IN
(SELECT EDLEVEL
FROM EMPLOYEE
WHERE EMPNO = '000060')
ORDER BY SALARY DESC;

pr

Ex

cl

End of solutions

6-6

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Exercise 7. Maintaining Data


What this exercise is about

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

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.

Some products (for example, QMF, DB-Access) will warn you if you
are about to make changes to data/delete database objects. You might
be required to confirm your decision.

Requirements

pr

Ex

cl

Student handout
SQL Reference

Copyright IBM Corp. 2013

Exercise 7. Maintaining Data


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

7-1

Student Exercises

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

Problem 1

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

Create table TESTEMP with the following columns:


EMPNO

LASTNAME
WORKDEPT
HIREDATE
SALARY
BONUS

Your instructor will advise you whether you need to use the IN statement after your
CREATE to specify a database name, table space name or DBSPACE to hold your
table.

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.

cl

Problem 3

Ex

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

pr

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.

7-2

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

Problem 7

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

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

pr

Ex

cl

End of exercise

Copyright IBM Corp. 2013

Exercise 7. Maintaining Data


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

7-3

Student Exercises

Problem List with Expected Results

Problem 1

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

Create table TESTEMP with the following columns:

EMPNO
LASTNAME
WORKDEPT
HIREDATE
SALARY
BONUS

Your instructor will advise you whether you need to use the IN statement after your
CREATE to specify a database name, table space name or DBSPACE to hold your
table.
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 DB2 from a Workstation:

pr

Ex

cl

DB20000I The SQL command completed successfully.

7-4

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

Add the new employees to the TESTEMP table.


Result

Confirmation that your rows were added to the table.

In SPUFI, 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 running DB2 from a Workstation:

pr

Ex

cl

DB20000I The SQL command completed successfully.

Copyright IBM Corp. 2013

Exercise 7. Maintaining Data


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

7-5

Student Exercises

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

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

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

If running DB2 from a Workstation:

pr

Ex

cl

DB20000I The SQL command completed successfully.

7-6

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

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

If running DB2 from a Workstation:

pr

Ex

cl

DB20000I The SQL command completed successfully.

Copyright IBM Corp. 2013

Exercise 7. Maintaining Data


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

7-7

Student Exercises

Problem 5
All employees in department C01 have done a good job. Therefore, they receive a
salary increase of $1,000.
Result

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

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

If running DB2 from a Workstation:

DB20000I The SQL command completed successfully.

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

If running DB2 from a Workstation:

pr

Ex

cl

DB20000I The SQL command completed successfully.

7-8

DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

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

If running DB2 from a Workstation:

DB20000I The SQL command completed successfully.

Problem 8

Make the appropriate changes for the following situation:

Theodore Spenser (employee 100) is assigned to department E01.

Result

In SPUFI:

---------+---------+---------+---------+---------+-------DSNE615I NUMBER OF ROWS AFFECTED IS 1


DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+--------

If running DB2 from a Workstation:

pr

Ex

cl

DB20000I The SQL command completed successfully.

Copyright IBM Corp. 2013

Exercise 7. Maintaining Data


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

7-9

Student Exercises

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

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

Result

In SPUFI:

---------+---------+---------+---------+---------+-------DSNE615I NUMBER OF ROWS AFFECTED IS 1


DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+--------

If running DB2 from a Workstation:

pr

Ex

cl

DB20000I The SQL command completed successfully.

7-10 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

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

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

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:

pr

Ex

cl

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

Copyright IBM Corp. 2013

Exercise 7. Maintaining Data


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

7-11

Student Exercises

If running DB2 from a Workstation:


After DELETE:
DB20000I The SQL command completed successfully.
After DROP:

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

DB20000I The SQL command completed successfully.


If you try and SELECT it:

SQL0204N "DB2ADMIN.TESTEMP" is an undefined name. SQLSTATE=42704

pr

Ex

cl

End of Problem List with Expected Results

7-12 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Solutions
Problem 1

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

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

Problem 2

INSERT
VALUES
INSERT
VALUES
INSERT
VALUES

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

(The semicolons assume that you are using a tool such as SPUFI, the CLP, or the
Command Editor which allows you to submit several SQL statements at once,
separating the statements with semicolons.)
DB2 for Linux, UNIX, and Windows allows you to INSERT multiple rows in one
INSERT SQL statement, so you could also use the following on those platforms:

pr

Ex

cl

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

Copyright IBM Corp. 2013

Exercise 7. Maintaining Data


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

7-13

Student Exercises

Problem 3

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

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

cl

Rows updated: 2

Ex

Problem 6

DELETE FROM TESTEMP


WHERE EMPNO = '000111';

pr

Rows deleted: 1

7-14 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

V6.0
Student Exercises

Uempty

Problem 7

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

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

pr

Ex

cl

Rows updated: 1

Copyright IBM Corp. 2013

Exercise 7. Maintaining Data


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

7-15

Student Exercises

Problem 9
DB2 solutions:
INSERT INTO TESTEMP
VALUES ('000360', 'BROWN ', 'D01', CURRENT DATE, 45000, NULL );

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

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

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.

Problem 10

DELETE FROM TESTEMP;


SELECT * FROM TESTEMP;
DROP TABLE TESTEMP; COMMIT;
SELECT * FROM TESTEMP;

pr

Ex

cl

End of solutions

7-16 DB2 SQL Workshop

Copyright IBM Corp. 2013


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

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

backpg

pr

cl

Ex
V7.0.1

Back page

pr

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

cl

Ex

CONTACTO
Telfono
91 761 21 78
Pngase en contacto con nuestro equipo y le
informaremos de cualquier duda o cuestin
que pueda surgirle.

Email
formacion@arrowecs.es
Mndenos un email y le atenderemos
enseguida.

Online
@Arrow_Edu_ES
O bien puede contactarnos a travs de
nuestro perfil en Twitter.

Vistenos
Arrow ECS Education Services
Avenida de Europa 21,
Parque Empresarial La Moraleja
28108 Alcobendas, Madrid

EDUCATION
S

Potrebbero piacerti anche