Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Intelligence
Data
Centre
Cloud
Mobility
Student Manual
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
Student Exercises
pr
Ex
cl
ERC 6.1
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.
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
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.
V7.0
Student Exercises
Contents
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2-1
2-2
2-4
2-9
3-1
3-2
3-5
3-9
4-1
4-2
4-3
4-6
cl
Exercise 5. UNION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Problem List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Problem List with Expected Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-1
5-2
5-4
5-7
6-1
6-2
6-3
6-5
pr
Ex
TOC
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
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.
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
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
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.
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
.
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
pr
Ex
cl
viii
V6.0
Student Exercises
Uempty
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 ____________________________________.
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
Exercises description
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
ix
Student Exercises
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
__________________________________________________
__________________________________________________
__________________________________________________
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.
cl
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
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
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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.
Exercises description
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
xi
Student Exercises
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Logon to Citrix
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
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Starting VMware
CE1216.1
Notes:
pr
Ex
cl
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
.
...
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
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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.
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
.
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
pr
xvi
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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.
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
.
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.
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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.
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
.
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
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
connect to sample
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
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.
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
.
connect to sample
Jim Hammonds/Atlanta/IBM
connect to sample
Database server
= DB2/NT 9.5.0
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.
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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.
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
.
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.
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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.
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
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.
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
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
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
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
V6.0
Student Exercises
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
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
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
Exercises description
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
xxxiii
Student Exercises
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
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
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
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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
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
Problem 5
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
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
.
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
1-3
Student Exercises
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
......
......
...
1-4
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
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
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
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
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
.
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
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
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
pr
DEPTNO DEPTNAME
---------+---------+---------+---------+
D11
MANUFACTURING SYSTEMS
E11
OPERATIONS
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
1-11
Student Exercises
Solutions
Problem 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem 2
Problem 3
Problem 4
Problem 5
cl
Problem 6
pr
Ex
Problem 7
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 8
Problem 9
Ex
cl
Problem 10
pr
Problem 11
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
Problem 14
Problem 15
pr
Ex
cl
End of solutions
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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
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
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
2-3
Student Exercises
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
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
LASTNAME
--------------PULASKI
KWAN
STERN
HENDERSON
THOMPSON
SPENSER
HAAS
GEYER
FIRSTNME
-----------EVA
SALLY
IRVING
EILEEN
MICHAEL
THEODORE
CHRISTINE
JOHN
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
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
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
2-8
V6.0
Student Exercises
Uempty
Solutions
Problem 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem 2
pr
Ex
cl
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
Problem 4
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!
V6.0
Student Exercises
Uempty
Problem 5
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
or
Problem 6
OR
pr
Ex
cl
2-11
Student Exercises
Problem 7
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
OR
pr
Ex
cl
End of solutions
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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
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:
Problem 2
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
pr
List department number, last name, salary, and decreased salary. Sort the result by
salary.
3-2
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
pr
Ex
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
V6.0
Student Exercises
Uempty
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:
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
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
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
.
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
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
.
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
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
3-8
V6.0
Student Exercises
Uempty
Solutions
Problem 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
In DB2, the DECIMAL function is needed to specify that only the two decimal points from
the ROUND are displayed.
3-9
Student Exercises
Problem 2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Alternative
pr
Ex
cl
In DB2, the DECIMAL function is needed to specify that only the two decimal points from
the ROUND are displayed.
V6.0
Student Exercises
Uempty
Problem 3
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem 4
pr
Ex
cl
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:
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
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
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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
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
V6.0
Student Exercises
Uempty
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
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
4-4
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
4-5
Student Exercises
Solutions
Problem 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem 2
Problem 3
Problem 4
Ex
cl
Problem 5
pr
End of solutions
4-6
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
.
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
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
pr
5-2
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
.
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
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
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
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
.
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
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
.
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
pr
Ex
5-6
V6.0
Student Exercises
Uempty
Solutions
Problem 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem 2
pr
Ex
cl
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
.
Problem 4
pr
Ex
cl
End of solutions
5-8
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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
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.
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
V6.0
Student Exercises
Uempty
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
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
6-4
V6.0
Student Exercises
Uempty
Solutions
Problem 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem 2
Problem 3
DB2:
Problem 4
pr
Ex
cl
6-5
Student Exercises
Problem 5
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
End of solutions
6-6
V6.0
Student Exercises
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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
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
.
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:
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
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
Problem 9
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
7-3
Student Exercises
Problem 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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
pr
Ex
cl
7-4
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
.
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
---------+---------+---------+---------+---------+---------+
pr
Ex
cl
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:
pr
Ex
cl
7-6
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
.
pr
Ex
cl
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
---------+---------+---------+---------+---------+---------+-
Problem 6
After a short time, Mr. Smith leaves the company. Make the appropriate changes.
Result
In SPUFI:
pr
Ex
cl
7-8
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
---------+---------+---------+---------+---------+--------
Problem 8
Result
In SPUFI:
pr
Ex
cl
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:
pr
Ex
cl
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
.
pr
Ex
cl
7-11
Student Exercises
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
V6.0
Student Exercises
Uempty
Solutions
Problem 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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
7-13
Student Exercises
Problem 3
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
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
pr
Rows deleted: 1
V6.0
Student Exercises
Uempty
Problem 7
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Rows inserted: 28
Problem 8
UPDATE TESTEMP
SET WORKDEPT = 'E01'
WHERE EMPNO = '000100';
pr
Ex
cl
Rows updated: 1
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
Problem 10
pr
Ex
cl
End of solutions
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