The information contained in this document has not been submitted to any formal
IBM test and is ... course has been certified as being Year 2000 compliant.
DB2 SQL Workshop (Course Code CF12)
Student Exercises ERC3.0
IBM Learning Services Worldwide Certified Material
Student Exercises
Publishing Information This publication has been produced using BookMaster (Program Number 5668-015), the Document Composition Facility (Program Number 5748-XX9), and Freelance 97 for Windows. It was printed on the IBM 3820 Page Printer.
Trademarks IBM is a registered trademark of International Business Machines Corporation. The following are trademarks International Business Machines Corporation in the United States, or other countries, or both: BookMaster QMF
DB2
OS/390
Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. UNIX is a registered trademark in the United States and other countries licensed exclusively through The Open Group. Other company, product, and service names may be trademarks or service marks of others.
December, 2000 The information contained in this document has not been submitted to any formal IBM test and is distributed on an “as is” basis without any warranty either express or implied. The use of this information or the implementation of any of these techniques is a customer responsibility and depends on the customer's ability to evaluate and integrate them into the customer's operational environment. While each item may have been reviewed by IBM for accuracy in a specific situation, there is no guarantee that the same or similar results will result elsewhere. Customers attempting to adapt these techniques to their own environments do so at their own risk. The original repository material for this course has been certified as being Year 2000 compliant. Copyright International Business Machines Corporation 1999, 2000. All rights reserved. This document may not be reproduced in whole or in part without the prior written permission of IBM. Note to U.S. Government Users — Documentation related to restricted rights — Use, duplication or disclosure is subject to restrictions set forth in GSA ADP Schedule Contract with IBM Corp.
Student Exercises
Contents Exercises Description Table Descriptions Table Content
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
v
xiii xv
Exercises Exercise 1. Simple SQL Queries
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise 2. Retrieving Data from Multiple Tables
1-1
. . . . . . . . . . . . . . . . . . . . . . .
2-1
Exercise 3. Scalar Functions and Arithmetic
. . . . . . . . . . . . . . . . . . . . . . . . . .
3-1
Exercise 4. Column Functions and Grouping
. . . . . . . . . . . . . . . . . . . . . . . . . .
4-1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-1
Exercise 5. UNION
Exercise 6. Using Subqueries
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6-1
Exercise 7. Maintaining Data
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-1
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Contents
iii
Student Exercises
iv
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Exercises Description Read the General Information section and the description of the lab tables before attempting the exercises. Feel free to consult with the instructor if you need a hint or encounter difficulties while you are formulating a query. Problem List — Contains a listing of the problems for that exercise. You may complete the lab problems from this listing. Expected Results — repeats the problem and shows the results from the completed query. In most cases, the complete result is shown. In cases where the result set is too large, an ellipsis (...) is shown to indicate that there are additional rows in the result. The number of rows for the result set is shown so you may verify your results. Solutions — contains the query that solves the problem. There are sometimes several possible solutions. Your solution may differ from the one shown. If you have any doubts about whether your solution is correct or if it is as good as the given solution from a performance point of view, please consult your instructor.
General Information This lab guide provides the information necessary to complete the required and optional labs for the DB2 SQL Workshop course. In countries that provide several query tools for each environment, this lab guide may be supplemented by additional documentation describing the basics of each query tool. The exercises for this course may be done in OS/390 or in a Workstation environment. Depending on the class location, any of the following Workstation platforms may be available: OS/2, AIX, Windows 95 or Windows NT. Read the instructions for your operating system before attempting the exercises. All Workstation platforms use the same instructions. You may use any of the query tools installed on the operating system. Please note that the emphasis of this course is on teaching SQL, not on tool use. Therefore, only the basics of the tool use is covered. For additional information about these tools, ask the instructor for recommendations on manuals to read or courses to take for this purpose. Although saving your queries is not required, feel free to comment out or save your solutions.
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercises Description
v
Student Exercises
Full solutions are provided for every question but if you wish to save your own solutions to diskette or print them, ask your instructor when you begin the first exercise. Diskettes or printers may not be available in every class.
vi
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
General Information for OS/390 Users There are a variety of query tools that can work with DB2 for OS/390, including SPUFI, QMF, and others. You may use any of the tools that are installed to do the labs. The following query tools are available for this course: __________________________ __________________________ __________________________ The instructor will tell you how to logon to the OS/390 system and what specific user ID and password to use. In addition, you will probably be given a team number: this number will be used occasionally during the exercises so that you can distinguish your files or objects from those of other students.
Your logon ID is __________________________________________. Your password is __________________________________________. Your team number is _______________________________________. The instructor will give you brief verbal or written instructions on how to use the query tool you choose. For more information about a tool, for example how to save a query, be sure to ask the instructor. For SPUFI, you will need a partitioned dataset in order to complete the exercises. This dataset already exists.
The dataset name is ________________________________________. Each of the tables described in the Table Relationships section of this document already exists and will be accessed through views owned by your lab ID.
Your lab ID is _______________. Within your SQL it is not necessary to prefix the table names with its qualifier. When you create TESTEMP table during the exercise 7, you may need to add an IN clause to your CREATE TABLE statements that identifies where the table is being created. The IN clause you need to add is:
IN DBCF12xx.TSxx (where the xx is your team number). You are not required to save your SQL but if you want to save it, you may put it in your partitioned dataset.
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercises Description
vii
Student Exercises
General Information for Workstation Users There are a variety of query tools that can work with DB2 on the workstation platforms including Command Center, CLP, QMF for Windows, and others. You may use any of the installed tools to do the labs. The following query tools are available for this course: __________________________ __________________________ __________________________ Students doing the exercises on workstation platforms do not need a team number. Each workstation student has a complete standalone environment and will not share any files or tables with other students. Each student will need to sign on with the following information:
USERID is _____________________ PASSWORD is ____________________ The instructor will give you brief verbal or written instructions on how to use the tool you choose so that you know enough to get started. If you want to know more about a tool, for example how to save a query, be sure to ask the instructor. A database named sample has already been created for you. to connect in a single-user environment, enter the following at a command prompt:
db2 connect to sample To connect in a client/server environment, enter the following at a command prompt, substituting the userid and password you were assigned:
db2 connect to sample user userid using password Each of the tables described in the Table Relationships section of this document already exists and has the same qualifier.
The qualifier is _________________. When you refer to these tables in your SQL, you will need to prefix the table names with the qualifier identified above. For example, if you want to display the complete contents of the EMPLOYEE table, enter the following, substituting the qualifier you were given:
SELECT * FROM qualifier.EMPLOYEE In cases where you are using your TESTEMP table you need to omit the qualifier or use your logon ID as qualifier. For example, if you are trying to read the TESTEMP table, you will need to write: viii
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
SELECT * FROM TESTEMP or SELECT * FROM logon ID.TESTEMP If you choose to do the labs in CLP, you may create your SQL in the \CF12 directory.
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercises Description
ix
Student Exercises
Figure
0-1. Table Relationships (CF12X010)
Notes: This diagram illustrates the relationships between the tables used in the exercises for this course. The lines show the connection between the tables.
x
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercises Description
xi
Student Exercises
xii
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Table Descriptions The tables are described in hierarchical order, as shown in the Table Relationships on the previous page. DEPARTMENT There is one row in the DEPARTMENT table for each department in the company. Column Name 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) CHAR(6)
NULLS allowed N N Y
CHAR(3)
N
CHAR(5)
Y
EMPLOYEE There is one row in the EMPLOYEE table for each of the employees in the company. Column Name EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE JOB EDLEVEL 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(20) CHAR(1) VARCHAR(15) CHAR(3)
NULLS allowed N N N N Y
CHAR(4) DATE CHAR(8) SMALLINT CHAR(1) DATE DECIMAL(9, 2) DECIMAL(9, 2) DECIMAL(9, 2)
Y Y Y Y Y Y Y Y Y
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Table Descriptions
xiii
Student Exercises
PROJECT There is one row in the PROJECT table for each project. Column Name PROJNO PROJNAME DEPTNO RESPEMP PRSTAFF PRSTDATE PRENDATE MAJPROJ
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) CHAR(3) CHAR(6)
NULLS allowed N N N N
DECIMAL(5, 2) DATE DATE CHAR(6)
Y Y Y Y
EMP_ACT There are many rows in the EMP_ACT table for any employee or any project. Column Name EMPNO PROJNO ACTNO EMPTIME EMSTDATE EMENDATE
xiv
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
DB2 SQL Workshop
Data Type CHAR(6)
NULLS allowed N
CHAR(6) SMALLINT DECIMAL(5, 2)
N N Y
DATE DATE
Y Y
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Table Content DEPARTMENT table DEPTNO -----A00 B01 C01 D01 D11 D21 E01 E11 E21
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
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Table Content
xv
Student Exercises
EMPLOYEE table 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
xvi
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
DB2 SQL Workshop
MIDINIT ------I L A B F D W Q G M A R J S H T K J M S V L R R X F V R
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
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
EMPLOYEE table (cont'd) JOB EDLEVEL SEX BIRTHDATE SALARY BONUS COMM --------- ------- --- ---------- --------- -------- -------PRES 18 F 1933-08-14 52750.00 1000.00 4220.00 MANAGER 18 M 1948-02-02 41250.00 800.00 3300.00 MANAGER 20 F 1941-05-11 38250.00 800.00 3060.00 MANAGER 16 M 1925-09-15 40175.00 800.00 3214.00 MANAGER 16 M 1945-07-07 32250.00 600.00 2580.00 MANAGER 16 F 1953-05-26 36170.00 700.00 2893.00 MANAGER 16 F 1941-05-15 29750.00 600.00 2380.00 MANAGER 14 M 1956-12-18 26150.00 500.00 2092.00 SALESREP 19 M 1929-11-05 46500.00 900.00 3720.00 CLERK 14 M 1942-10-18 29250.00 600.00 2340.00 ANALYST 16 F 1925-09-15 23800.00 500.00 1904.00 ANALYST 18 F 1946-01-19 28420.00 600.00 2274.00 DESIGNER 16 M 1947-05-17 25280.00 500.00 2022.00 DESIGNER 17 F 1955-04-12 22250.00 400.00 1780.00 DESIGNER 16 M 1951-01-05 24680.00 500.00 1974.00 DESIGNER 17 F 1949-02-21 21340.00 500.00 1707.00 DESIGNER 16 M 1952-06-25 20450.00 400.00 1636.00 DESIGNER 16 M 1941-05-29 27740.00 600.00 2217.00 DESIGNER 17 M 1953-02-23 18270.00 400.00 1462.00 DESIGNER 18 F 1948-03-19 29840.00 600.00 2387.00 CLERK 14 M 1935-05-30 22180.00 400.00 1774.00 CLERK 17 M 1954-03-31 28760.00 600.00 2301.00 CLERK 15 M 1939-11-12 19180.00 400.00 1534.00 CLERK 16 F 1936-10-05 17250.00 300.00 1380.00 CLERK 15 F 1953-05-26 27380.00 500.00 2190.00 OPERATOR 17 F 1936-03-28 26250.00 500.00 2100.00 OPERATOR 12 M 1946-07-09 15340.00 300.00 1227.00 OPERATOR 14 M 1936-10-27 17750.00 400.00 1420.00 OPERATOR 12 F 1931-04-21 15900.00 300.00 1272.00 FIELDREP 16 M 1932-08-11 19950.00 400.00 1596.00 FIELDREP 14 M 1941-07-18 25370.00 500.00 2030.00 FIELDREP 16 M 1926-05-17 23840.00 500.00 1907.00
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Table Content
xvii
Student Exercises
PROJECT table PROJNO -----AD3100 AD3110 AD3111 AD3112 AD3113 IF1000 IF2000 MA2100 MA2110 MA2111 MA2112 MA2113 OP1000 OP1010 OP2000 OP2010 OP2011 OP2012 OP2013 PL2100
xviii
DEPT PROJNAME NO RESPEMP --------------------------------ADMIN SERVICES D01 000010 GENERAL AD SYSTEMS D21 000070 PAYROLL PROGRAMMING D21 000230 PERSONNEL PROGRAMMG D21 000250 ACCOUNT.PROGRAMMING D21 000270 QUERY SERVICES C01 000030 USER EDUCATION C01 000030 WELD LINE AUTOMATION D01 000010 W L PROGRAMMING D11 000060 W L PROGRAM DESIGN D11 000220 W L ROBOT DESIGN D11 000150 W L PROD CONT PROGS D11 000160 OPERATION SUPPORT E01 000050 OPERATION E11 000090 GEN SYSTEMS SERVICES E01 000050 SYSTEMS SUPPORT E21 000100 SCP SYSTEMS SUPPORT E21 000320 APPLICATIONS SUPPORT E21 000330 DB/DC SUPPORT E21 000340 WELD LINE PLANNING B01 000020
DB2 SQL Workshop
PR STAFF ----6.50 6.00 2.00 1.00 2.00 2.00 1.00 12.00 9.00 2.00 3.00 3.00 6.00 5.00 5.00 4.00 1.00 1.00 1.00 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
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
EMP_ACT table 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
PROJNO ACTNO EMPTIME EMSTDATE EMENDATE ------ ------ -------- ---------- ---------AD3100 10 0.50 1982-01-01 1982-07-01 AD3110 10 1.00 1982-01-01 1983-02-01 AD3111 60 1.00 1982-01-01 1982-03-15 AD3111 60 0.50 1982-03-15 1982-04-15 AD3111 70 0.50 1982-03-15 1982-10-15 AD3111 80 0.50 1982-04-15 1982-10-15 AD3111 180 1.00 1982-10-15 1983-01-01 AD3111 70 1.00 1982-02-15 1982-09-15 AD3111 80 1.00 1982-09-15 1983-01-01 AD3112 60 0.50 1982-02-01 1982-03-15 AD3112 60 1.00 1982-01-01 1982-02-01 AD3112 60 1.00 1983-01-01 1983-02-01 AD3112 60 0.50 1982-12-01 1983-01-01 AD3112 70 1.00 1982-03-15 1982-08-15 AD3112 70 0.50 1982-02-01 1982-03-15 AD3112 70 0.25 1982-08-15 1982-10-15 AD3112 80 0.25 1982-08-15 1982-10-15 AD3112 80 0.50 1982-10-15 1982-12-01 AD3112 180 0.50 1982-08-15 1983-01-01 AD3113 70 0.50 1982-06-15 1982-07-01 AD3113 70 1.00 1982-07-01 1983-02-01 AD3113 80 1.00 1982-01-01 1982-03-01 AD3113 80 0.50 1982-03-01 1982-04-15 AD3113 180 0.50 1982-03-01 1982-04-15 AD3113 180 0.50 1982-06-01 1982-07-01 AD3113 180 1.00 1982-04-15 1982-06-01 AD3113 60 0.25 1982-09-01 1982-10-15 AD3113 60 1.00 1982-04-01 1982-09-01 AD3113 60 0.50 1982-03-01 1982-04-01 AD3113 70 0.75 1982-09-01 1982-10-15 AD3113 70 1.00 1982-10-15 1983-02-01 AD3113 80 1.00 1982-01-01 1982-03-01 AD3113 80 0.50 1982-03-01 1982-04-01 IF1000 10 0.50 1982-06-01 1983-01-01 IF1000 90 1.00 1982-01-01 1982-10-01 IF1000 100 0.50 1982-10-01 1983-01-01 IF1000 90 0.50 1982-10-01 1983-01-01
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Table Content
xix
Student Exercises
EMP_ACT table (cont'd) 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
xx
PROJNO ACTNO EMPTIME EMSTDATE EMENDATE ------ ------ -------- ---------- ---------IF2000 10 0.50 1982-01-01 1983-01-01 IF2000 100 1.00 1982-01-01 1982-03-01 IF2000 100 0.50 1982-03-01 1982-07-01 IF2000 110 0.50 1982-03-01 1982-07-01 IF2000 110 0.50 1982-10-01 1983-01-01 MA2100 10 0.50 1982-01-01 1982-11-01 MA2100 20 1.00 1982-01-01 1982-03-01 MA2110 10 1.00 1982-01-01 1983-02-01 MA2111 50 1.00 1982-01-01 1982-06-15 MA2111 60 1.00 1982-06-15 1983-02-01 MA2111 40 1.00 1982-01-01 1983-02-01 MA2112 60 1.00 1982-01-01 1982-07-15 MA2112 180 1.00 1982-07-15 1983-02-01 MA2112 60 1.00 1982-01-01 1983-06-01 MA2112 70 1.00 1982-06-01 1983-02-01 MA2112 70 1.00 1982-02-01 1982-10-01 MA2112 80 1.00 1982-10-01 1983-10-01 MA2113 60 1.00 1982-07-15 1983-02-01 MA2113 80 1.00 1982-01-01 1983-02-01 MA2113 70 1.00 1982-04-01 1982-06-15 MA2113 80 0.50 1982-10-01 1983-02-01 MA2113 180 0.50 1982-10-01 1983-02-01 OP1000 10 0.25 1982-01-01 1983-02-01 OP1010 10 1.00 1982-01-01 1983-02-01 OP1010 130 1.00 1982-01-01 1983-02-01 OP1010 130 1.00 1982-01-01 1983-02-01 OP1010 130 1.00 1982-01-01 1983-02-01 OP1010 130 1.00 1982-01-01 1983-02-01 OP2010 10 0.75 1982-01-01 1983-02-01 OP2010 10 1.00 1982-01-01 1983-02-01 OP2011 140 0.75 1982-01-01 1983-02-01 OP2011 150 0.25 1982-01-01 1983-02-01 OP2012 140 0.25 1982-01-01 1983-02-01 OP2012 160 0.75 1982-01-01 1983-02-01 OP2013 140 0.50 1982-01-01 1983-02-01 OP2013 170 0.50 1982-01-01 1983-02-01 PL2100 30 1.00 1982-01-01 1982-09-15
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Each exercise in this course is divided into sections as described below. Select the section that best fits your method of performing labs. You may elect to use a combination of these sections as appropriate. Problem List — This section contains what it is you are to accomplish. There are no definitive details on how to perform the tasks. You are given the opportunity to work through the exercise given what you learned in the unit presentation, utilizing the unit Student Notebook, your past experience and maybe a little intuition. Problem List With Expected Results — This section is an exact duplicate of the Problem List section except that it includes the expected result of the query you are to write. Feel free to use the Problem List or the Problem List with Expected Results when doing your lab problems. Solutions — Follow the Problem List With Expected Results.
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Table Content
xxi
Student Exercises
xxii
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Exercise 1. Simple SQL Queries What This Exercise is About This exercise provides an opportunity to write and execute basic SQL statements.
What You Should Be Able to Do At the end of the lab, you should be able to: Code SELECT statements using four clauses of an SQL SELECT statement. Use a SELECT statement to: − Retrieve all rows of a table − Retrieve specific columns − Retrieve rows based on comparisons to numeric or alphanumeric data − Retrieve rows based on specific columns containing NULL values Code SELECT statements using keywords BETWEEN, IN, LIKE and DISTINCT Produce a result table whose rows are sorted in a desired sequence
Introduction See the data model at the start of this exercise guide to get the column names and descriptions for each table.
Required Materials Student handout SQL Reference
Copyright IBM Corp. 1999, 2000 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 List employee number, last name, date of birth, and salary for all employees who make more than $30,000 a year. Sequence the results in descending order by salary.
Problem 2 List last name, first name, and the department number for all employees. The listing should be ordered by descending department numbers. Within the same department, the last names should be sorted in descending order.
Problem 3 List the different education levels in the company in descending order. List only one occurrence of duplicate result rows.
Problem 4 List employees, by employee number, and their assigned projects, by project number. Display only those employees with an employee number less than or equal to 100. List only one occurrence of duplicate rows. Sort the result rows by employee number. (Use the EMP_ACT table.)
Problem 5 List last name, salary, and bonus of all male employees.
Problem 6 List last name, salary, and commission for all employees with a salary greater than $20,000 and hired after 1979.
Problem 7 List last name, salary, bonus, and commission for all employees with a salary greater than $22,000 and a bonus of $400, or for all employees with a bonus of $500 and a commission lower than $1,900. The list should be ordered by last name.
Problem 8 List last name, salary, bonus, and commission for all employees with a salary greater than $22,000, a bonus of $400 or $500, and a commission less than $1,900. The list should be ordered by last name.
Problem 9 Using the EMP_ACT table, for all projects that have a project number beginning with AD and have activities 10, 80, and 180 associated with them, list the following: Project number Activity number Starting date for activity Ending date for activity Order the list by activity number within project number. 1-2
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 10 List manager number and department number for all departments to which a manager has been assigned. The list should be ordered by manager number.
Problem 11 List employee number, last name, salary, and bonus for all employees that have a bonus ranging from $800 to $1,000. Sort the report by employee number within bonus, lowest bonus first.
Problem 12 List employee number, last name, salary, and department number for all employees in departments A00 through C01 (inclusive). Order the results alphabetically by last name and employee number.
Problem 13 List all projects that have SUPPORT as part of the project name. Order the results by project number.
Problem 14 List all departments that have a 1 as the middle character in the department number. Order the results by department number.
Problem 15 List the last name, first name, middle initial, and salary of the five highest paid non-manager, non-president employees. Order the results by highest salary first.
END OF LAB
Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 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 List employee number, last name, date of birth, and salary for all employees who make more than $30,000 a year. Sequence the results in descending order by salary.
Result
EMPNO LASTNAME BIRTHDATE SALARY ---------+---------+---------+---------+-------000010 HAAS 1933-08-14 52750.00 000110 LUCCHESI 1929-11-05 46500.00 000020 THOMPSON 1948-02-02 41250.00 000050 GEYER 1925-09-15 40175.00 000030 KWAN 1941-05-11 38250.00 000070 PULASKI 1953-05-26 36170.00 000060 STERN 1945-07-07 32250.00 Problem 2 List last name, first name, and the department number for all employees. The listing should be ordered by descending department numbers. Within the same department, the last names should be sorted in descending order.
Result
LASTNAME FIRSTNME WORKDEPT ---------+---------+---------+---------+ SPENSER THEODORE E21 MEHTA RAMLAL E21 LEE WING E21 GOUNOT JASON E21 SMITH PHILIP E11 SETRIGHT MAUDE E11 SCHNEIDER ETHEL E11 PARKER JOHN E11 HENDERSON EILEEN E11 GEYER JOHN E01 ...... ...... ... The complete result set contains 32 rows.
1-4
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 3 List the different education levels in the company in descending order. List only one occurrence of duplicate result rows.
Result
EDLEVEL ------20 19 18 17 16 15 14 12 Problem 4 List employees, by employee number, and their assigned projects, by project number. Display only those employees with an employee number less than or equal to 100. List only one occurrence of duplicate rows. Sort the result rows by employee number. (Use the EMP_ACT table.)
Result
EMPNO PROJNO ---------+---000010 AD3100 000010 MA2100 000010 MA2110 000020 PL2100 000030 IF1000 000030 IF2000 000050 OP1000 000050 OP2010 000070 AD3110 000090 OP1010 000100 OP2010
Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 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
LASTNAME SALARY BONUS ---------+---------+---------+---------+THOMPSON 41250.00 800.00 GEYER 40175.00 800.00 STERN 32250.00 600.00 SPENSER 26150.00 500.00 LUCCHESI 46500.00 900.00 O'CONNELL 29250.00 600.00 ADAMSON 25280.00 500.00 YOSHIMURA 24680.00 500.00 WALKER 20450.00 400.00 BROWN 27740.00 600.00 JONES 18270.00 400.00 JEFFERSON 22180.00 400.00 MARINO 28760.00 600.00 SMITH 19180.00 400.00 PARKER 15340.00 300.00 SMITH 17750.00 400.00 MEHTA 19950.00 400.00 LEE 25370.00 500.00 GOUNOT 23840.00 500.00 Problem 6 List last name, salary, and commission for all employees with a salary greater than $20,000 and hired after 1979.
Result
LASTNAME SALARY COMM ---------+---------+---------+---------+PULASKI 36170.00 2893.00 SPENSER 26150.00 2092.00 PEREZ 27380.00 2190.00
1-6
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 7 List last name, salary, bonus, and commission for all employees with a salary greater than $22,000 and a bonus of $400, or for all employees with a bonus of $500 and a commission lower than $1,900. The list should be ordered by last name.
Result
LASTNAME SALARY BONUS COMM ---------+---------+---------+---------+---------+---JEFFERSON 22180,00 400,00 1774,00 PIANKA 22250,00 400,00 1780,00 SCOUTTEN 21340,00 500,00 1707,00 Problem 8 List last name, salary, bonus, and commission for all employees with a salary greater than $22,000, a bonus of $400 or $500, and a commission less than $1,900. The list should be ordered by last name.
Result
LASTNAME SALARY BONUS COMM ---------+---------+---------+---------+---------+---JEFFERSON 22180.00 400.00 1774.00 PIANKA 22250.00 400.00 1780.00
Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries 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, and 180 associated with them, list the following: Project number Activity number Starting date for activity Ending date for activity Order the list by activity number within project number.
Result
PROJNO ACTNO EMSTDATE EMENDATE ---------+---------+---------+-------AD3100 10 1982-01-01 1982-07-01 AD3110 10 1982-01-01 1983-02-01 AD3111 80 1982-04-15 1982-10-15 AD3111 80 1982-09-15 1983-01-01 AD3111 180 1982-10-15 1983-01-01 AD3112 80 1982-08-15 1982-10-15 AD3112 80 1982-10-15 1982-12-01 AD3112 180 1982-08-15 1983-01-01 AD3113 80 1982-01-01 1982-03-01 AD3113 80 1982-01-01 1982-03-01 AD3113 80 1982-03-01 1982-04-15 AD3113 80 1982-03-01 1982-04-01 AD3113 180 1982-03-01 1982-04-15 AD3113 180 1982-04-15 1982-06-01 AD3113 180 1982-06-01 1982-07-01
1-8
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 10 List manager number and department number for all departments to which a manager has been assigned. The list should be ordered by manager number.
Result
MGRNO DEPTNO ---------+---000010 A00 000020 B01 000030 C01 000050 E01 000060 D11 000070 D21 000090 E11 000100 E21 Problem 11 List employee number, last name, salary, and bonus for all employees that have a bonus ranging from $800 to $1,000. Sort the report by employee number within bonus, lowest bonus first.
Result
EMPNO -----000020 000030 000050 000110 000010
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
Copyright IBM Corp. 1999, 2000 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.
Result
EMPNO LASTNAME SALARY WORKDEPT ---------+---------+---------+---------+---------+ 000010 HAAS 52750.00 A00 000030 KWAN 38250.00 C01 000110 LUCCHESI 46500.00 A00 000140 NICHOLLS 28420.00 C01 000120 O'CONNELL 29250.00 A00 000130 QUINTANA 23800.00 C01 000020 THOMPSON 41250.00 B01 Problem 13 List all projects that have SUPPORT as part of the project name. Order the results by project number.
Result
PROJNO PROJNAME ---------+---------+---------+--------OP1000 OPERATION SUPPORT OP2010 SYSTEMS SUPPORT OP2011 SCP SYSTEMS SUPPORT OP2012 APPLICATIONS SUPPORT OP2013 DB/DC SUPPORT Problem 14 List all departments that have a 1 as the middle character in the department number. Order the results by department number.
Result
DEPTNO DEPTNAME ---------+---------+---------+---------+ D11 MANUFACTURING SYSTEMS E11 OPERATIONS
1-10
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 15 List the last name, first name, middle initial, and salary of the five highest paid non-manager, non-president employees. Order the results by highest salary first.
Result
LASTNAME --------------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
END OF Problem List With Expected Results
Copyright IBM Corp. 1999, 2000 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
Solution Problem 1
SELECT EMPNO, LASTNAME, BIRTHDATE, SALARY FROM EMPLOYEE WHERE SALARY > 30000 ORDER BY SALARY DESC Problem 2
SELECT LASTNAME, FIRSTNME, WORKDEPT FROM EMPLOYEE ORDER BY WORKDEPT DESC, LASTNAME DESC Problem 3
SELECT DISTINCT EDLEVEL FROM EMPLOYEE ORDER BY EDLEVEL DESC Problem 4
SELECT DISTINCT EMPNO, PROJNO FROM EMP_ACT WHERE EMPNO = '1980-01-01' AND SALARY > 20000 Problem 7
SELECT LASTNAME, SALARY, BONUS, COMM FROM EMPLOYEE WHERE SALARY > 22000 AND BONUS = 400 OR BONUS = 500 AND COMM < 1900 ORDER BY LASTNAME
1-12
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 8
SELECT LASTNAME, SALARY, BONUS, COMM FROM EMPLOYEE WHERE SALARY > 22000 AND ( BONUS = 400 OR BONUS = 500 ) AND COMM < 1900 ORDER BY LASTNAME Problem 9
SELECT PROJNO, ACTNO, EMSTDATE, EMENDATE FROM EMP_ACT WHERE ACTNO IN (10, 80, 180) AND PROJNO LIKE 'AD%' ORDER BY PROJNO, ACTNO Problem 10
SELECT MGRNO, DEPTNO FROM DEPARTMENT WHERE MGRNO IS NOT NULL ORDER BY MGRNO Problem 11
SELECT EMPNO, LASTNAME, SALARY, BONUS FROM EMPLOYEE WHERE BONUS BETWEEN 800 AND 1000 ORDER BY BONUS, EMPNO Problem 12
SELECT EMPNO, LASTNAME, SALARY, WORKDEPT FROM EMPLOYEE WHERE WORKDEPT BETWEEN 'A00' AND 'C01' ORDER BY LASTNAME, EMPNO Problem 13
SELECT PROJNO, PROJNAME FROM PROJECT WHERE PROJNAME LIKE '%SUPPORT%' ORDER BY PROJNO Problem 14
SELECT DEPTNO, DEPTNAME FROM DEPARTMENT WHERE DEPTNO LIKE '_1_' ORDER BY DEPTNO
Copyright IBM Corp. 1999, 2000 Exercise 1. Simple SQL Queries Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
1-13
Student Exercises
Problem 15
SELECT LASTNAME, FIRSTNME, MIDINIT, SALARY FROM EMPLOYEE WHERE JOB NOT IN ('PRES', 'MANAGER') ORDER BY SALARY DESC FETCH FIRST 5 ROWS ONLY END OF SOLUTION
1-14
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Exercise 2. Retrieving Data from Multiple Tables What This Exercise is About This exercise provides an opportunity to write queries that retrieve data from more than one table.
What You Should Be Able to Do At the end of the lab, you should be able to: Code SELECT statements referencing multiple tables or views.
Introduction See the data model at the start of this exercise guide to get the column names and descriptions for each table.
Required Materials Student handout SQL Reference
Copyright IBM Corp. 1999, 2000 Exercise 2. 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 Produce a report that lists employees' last names, first names, and department names. Sequence the report on first name within last name, within department name.
Problem 2 Modify the previous query to include job. Also, list data for only departments between A02 and D22, and exclude managers from the list. Sequence the report on first name within last name, within job, within department name.
Problem 3 List the name of each department and the lastname and first name of its manager. Sequence the list by department name. Use the EMPNO and MGRNO columns to relate the two tables. Sequence the result rows by department name.
Problem 4 Try the following: modify the previous query using WORKDEPT and DEPTNO as the join predicate. Include a local predicate that looks for people whose job is manager. Are the results from both queries the same? __________________ Why? ___________________________________________________________________ ______________________________________________________________________ _____________________________________________________________________
Problem 5 For all projects that have a project number beginning with AD, list project number, project name, and activity number. List identical rows once. Order the list by project number and then by activity number.
Problem 6 Which employees are assigned to project number AD3113? List employee number, last name, and project number. Order the list by employee number and then by project number. List only one occurrence of duplicate result rows.
2-2
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 7 Which activities began on October 1, 1982? For each of these activities, list the employee number of the person performing the activity, the project number,project name, activity number, and starting date of the activity. Order the list by project number, then by employee number, and then by activity number.
Problem 8 Display department number, last name, project name, and activity number for activities performed by the employees in department A00. Sequence the results first by project name and then by activity number.
Problem 9 List department number, last name, project name, and activity number for those employees in work departments A00 through C01. Suppress identical rows. Sort the list by department number, last name, and activity number.
Problem 10 The second line manager needs a list of activities which began on October 15, 1982 or thereafter. For these activities, list the activity number, the manager number of the manager of the department assigned to the project, the starting date for the activity, the project number, and the last name of the employee performing the activity. The list should be ordered by the activity number and then by the activity start date.
Problem 11 Which employees in department A00 were hired before their manager? List department number, the manager's last name, the employee's last name, and the hiring dates of both the manager and the employee. Order the list by the employee's last name.
END OF LAB
Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables 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 Produce a report that lists employees' last names, first names, and department names. Sequence the report on first name within last name, within department name.
RESULT
LASTNAME --------------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
DB2 SQL Workshop
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
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 2 Modify the previous query to include job. Also, list data for only departments between A02 and D22, and exclude managers from the list. Sequence the report on first name within last name, within job, within department name.
RESULT
LASTNAME --------------JEFFERSON JOHNSON MARINO PEREZ SMITH NICHOLLS QUINTANA ADAMSON BROWN JONES LUTZ PIANKA SCOUTTEN WALKER YOSHIMURA
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
Problem 3 List the name of each department and the lastname and first name of its manager. Sequence the list by department name. Use the EMPNO and MGRNO columns to relate the two tables. Sequence the result rows by department name.
RESULT
DEPTNAME ----------------------------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
Copyright IBM Corp. 1999, 2000 Exercise 2. 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? __________________ 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 For all projects that have a project number beginning with AD, list project number, project name, and activity number. List identical rows once. Order the list by project number and then by activity number.
Result
PROJNO PROJNAME ACTNO ---------+---------+---------+---------+AD3100 ADMIN SERVICES 10 AD3110 GENERAL AD SYSTEMS 10 AD3111 PAYROLL PROGRAMMING 60 AD3111 PAYROLL PROGRAMMING 70 AD3111 PAYROLL PROGRAMMING 80 AD3111 PAYROLL PROGRAMMING 180 AD3112 PERSONNEL PROGRAMMG 60 AD3112 PERSONNEL PROGRAMMG 70 AD3112 PERSONNEL PROGRAMMG 80 AD3112 PERSONNEL PROGRAMMG 180 AD3113 ACCOUNT.PROGRAMMING 60 AD3113 ACCOUNT.PROGRAMMING 70 AD3113 ACCOUNT.PROGRAMMING 80 AD3113 ACCOUNT.PROGRAMMING 180
2-6
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 6 Which employees are assigned to project number AD3113? List employee number, last name, and project number. Order the list by employee number and then by project number. List only one occurrence of duplicate result rows.
Result
EMPNO LASTNAME PROJNO ---------+---------+---------+-000260 JOHNSON AD3113 000270 PEREZ AD3113 Problem 7 Which activities began on October 1, 1982? For each of these activities, list the employee number of the person performing the activity, the project number,project name, activity number, and starting date of the activity. Order the list by project number, then by employee number, and then by activity number.
Result
EMPNO PROJNO PROJNAME ACTNO EMSTDATE ---------+---------+---------+---------+---------+---------+ 000130 IF1000 QUERY SERVICES 100 1982-10-01 000140 IF1000 QUERY SERVICES 90 1982-10-01 000140 IF2000 USER EDUCATION 110 1982-10-01 000190 MA2112 W L ROBOT DESIGN 80 1982-10-01 000210 MA2113 W L PROD CONT PROGS 80 1982-10-01 000210 MA2113 W L PROD CONT PROGS 180 1982-10-01
Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
2-7
Student Exercises
Problem 8 Display department number, last name, project name, and activity number for activities performed by the employees in department A00. Sequence the results first by project name and then by activity number.
Result
WORKDEPT LASTNAME PROJNAME ACTNO ---------+---------+---------+---------+---------+--------A00 HAAS ADMIN SERVICES 10 A00 HAAS W L PROGRAMMING 10 A00 HAAS WELD LINE AUTOMATION 10 A00 LUCCHESI WELD LINE AUTOMATION 20 Problem 9 List department number, last name, project name, and activity number for those employees in work departments A00 through C01. Suppress identical rows. Sort the list by department number, last name, and activity number.
Result
WORKDEPT LASTNAME PROJNAME ACTNO ---------+---------+---+---------+---------+--------A00 HAAS ADMIN SERVICES 10 A00 HAAS W L PROGRAMMING 10 A00 HAAS WELD LINE AUTOMATION 10 A00 LUCCHESI WELD LINE AUTOMATION 20 B01 THOMPSON WELD LINE PLANNING 30 C01 KWAN QUERY SERVICES 10 C01 KWAN USER EDUCATION 10 C01 NICHOLLS QUERY SERVICES 90 C01 NICHOLLS USER EDUCATION 100 C01 NICHOLLS USER EDUCATION 110 C01 QUINTANA QUERY SERVICES 90 C01 QUINTANA QUERY SERVICES 100
2-8
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 10 The second line manager needs a list of activities which began on October 15, 1982 or thereafter. For these activities, list the activity number, the manager number of the manager of the department assigned to the project, the starting date for the activity, the project number, and the last name of the employee performing the activity. The list should be ordered by the activity number and then by the activity start date.
Result
ACTNO MGRNO EMSTDATE PROJNO LASTNAME --------+---------+---------+---------+-----60 000070 1982-12-01 AD3112 SMITH 60 000070 1983-01-01 AD3112 SMITH 70 000070 1982-10-15 AD3113 PEREZ 80 000070 1982-10-15 AD3112 SMITH 180 000070 1982-10-15 AD3111 JEFFERSON Problem 11 Which employees in department A00 were hired before their manager? List department number, the manager's last name, the employee's last name, and the hiring dates of both the manager and the employee. Order the list by the employee's last name.
Result
DEPTNO MANAGER EMPLOYEE M_HIREDATE E_HIREDATE ---------+---------+---------+---------+---------+------A00 HAAS LUCCHESI 1965-01-01 1958-05-16 A00 HAAS O'CONNELL 1965-01-01 1963-12-05
END OF Problem List With Expected Results
Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
2-9
Student Exercises
Solution Problem 1
SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAME FROM EMPLOYEE E, DEPARTMENT D WHERE E.WORKDEPT = D.DEPTNO ORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME OR SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAME FROM EMPLOYEE E JOIN DEPARTMENT D ON E.WORKDEPT = D.DEPTNO ORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME Problem 2
SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAME, E.JOB FROM EMPLOYEE E, DEPARTMENT D WHERE E.WORKDEPT = D.DEPTNO AND E.WORKDEPT BETWEEN 'A02' AND 'D22' AND JOB 'MANAGER' ORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME OR SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAME, E.JOB FROM EMPLOYEE E JOIN DEPARTMENT D ON E.WORKDEPT = D.DEPTNO WHERE E.WORKDEPT BETWEEN 'A02' AND 'D22' AND JOB 'MANAGER' ORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME
2-10
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
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 OR SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNME FROM DEPARTMENT D JOIN EMPLOYEE E ON D.MGRNO = E.EMPNO ORDER BY D.DEPTNAME Problem 4
SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNME FROM DEPARTMENT D, EMPLOYEE E WHERE D.DEPTNO = E.WORKDEPT AND JOB = 'MANAGER' ORDER BY D.DEPTNAME OR SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNME FROM DEPARTMENT D JOIN EMPLOYEE E ON D.DEPTNO = E.WORKDEPT WHERE JOB = 'MANAGER' ORDER BY D.DEPTNAME The results of the queries in problem 3 and 4 are not the same. The department named Spiffy Computer Services Div. is not in the second report. The reason: the job title of that department's manager is PRES. Our WHERE clause only allows qualified persons with a job of MANAGER to be joined with their department rows. It is very important to learn the data within your tables!
Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
2-11
Student Exercises
Problem 5
SELECT DISTINCT A.PROJNO, PROJNAME, ACTNO FROM PROJECT P, EMP_ACT A WHERE P.PROJNO = A.PROJNO AND A.PROJNO LIKE 'AD%' ORDER BY A.PROJNO, ACTNO or SELECT DISTINCT A.PROJNO, PROJNAME, ACTNO FROM PROJECT AS P JOIN EMP_ACT AS A ON P.PROJNO = A.PROJNO WHERE A.PROJNO LIKE 'AD%' ORDER BY A.PROJNO, ACTNO Problem 6
SELECT DISTINCT A.EMPNO, LASTNAME, PROJNO FROM EMPLOYEE E, EMP_ACT A WHERE A.EMPNO = E.EMPNO AND A.PROJNO = 'AD3113' ORDER BY A.EMPNO, PROJNO or SELECT DISTINCT A.EMPNO,LASTNAME, PROJNO FROM EMPLOYEE AS E JOIN EMP_ACT AS A ON E.EMPNO = A.EMPNO WHERE A.PROJNO = 'AD3113' ORDER BY A.EMPNO, PROJNO
2-12
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 7
SELECT A.EMPNO, A.PROJNO, PROJNAME, ACTNO, EMSTDATE FROM PROJECT P, EMP_ACT A WHERE A.PROJNO = P.PROJNO AND EMSTDATE = '1982-10-01' ORDER BY A.PROJNO, A.EMPNO, ACTNO or SELECT A.EMPNO, A.PROJNO, PROJNAME, ACTNO, EMSTDATE FROM PROJECT AS P JOIN EMP_ACT AS A ON P.PROJNO = A.PROJNO WHERE A.EMSTDATE = '1982-10-01' ORDER BY A.PROJNO, A.EMPNO, ACTNO Problem 8
SELECT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNO FROM EMPLOYEE E, PROJECT P, EMP_ACT A WHERE E.EMPNO = A.EMPNO AND A.PROJNO = P.PROJNO AND E.WORKDEPT = 'A00' ORDER BY P.PROJNAME, A.ACTNO OR SELECT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNO FROM EMPLOYEE AS E JOIN EMP_ACT AS A ON E.EMPNO = A.EMPNO JOIN PROJECT AS P ON A.PROJNO = P.PROJNO WHERE E.WORKDEPT = 'A00' ORDER BY P.PROJNAME, A.ACTNO
Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
2-13
Student Exercises
Problem 9
SELECT DISTINCT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNO FROM EMPLOYEE E, PROJECT P, EMP_ACT A WHERE E.EMPNO = A.EMPNO AND A.PROJNO = P.PROJNO AND E.WORKDEPT BETWEEN 'A00' AND 'C01' ORDER BY E.WORKDEPT, E.LASTNAME, A.ACTNO OR SELECT DISTINCT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNO FROM EMPLOYEE E JOIN EMP_ACT A ON E.EMPNO = A.EMPNO JOIN PROJECT P ON A.PROJNO = P.PROJNO WHERE E.WORKDEPT BETWEEN 'A00' AND 'C01' ORDER BY E.WORKDEPT, E.LASTNAME, A.ACTNO Problem 10
SELECT A.ACTNO, D.MGRNO, A.EMSTDATE, P.PROJNO, E.LASTNAME FROM DEPARTMENT D, EMPLOYEE E, PROJECT P, EMP_ACT A WHERE E.EMPNO = A.EMPNO AND A.PROJNO = P.PROJNO AND E.WORKDEPT = D.DEPTNO AND A.EMSTDATE >= '1982-10-15' ORDER BY A.ACTNO, A.EMSTDATE OR SELECT A.ACTNO, D.MGRNO, A.EMSTDATE, E.LASTNAME FROM EMPLOYEE AS E JOIN EMP_ACT AS A ON E.EMPNO = A.EMPNO JOIN PROJECT AS P ON A.PROJNO = P.PROJNO JOIN DEPARTMENT AS D ON E.WORKDEPT = D.DEPTNO WHERE A.EMSTDATE >= '1982-10-15' ORDER BY A.ACTNO, A.EMSTDATE
2-14
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 11
SELECT D.DEPTNO, M.LASTNAME AS MANAGER, E.LASTNAME AS EMPLOYEE, M.HIREDATE AS M_HIREDATE, E.HIREDATE AS E_HIREDATE FROM EMPLOYEE M, EMPLOYEE E, DEPARTMENT D WHERE D.MGRNO = M.EMPNO AND E.WORKDEPT = D.DEPTNO AND M.HIREDATE > E.HIREDATE AND E.WORKDEPT = 'A00' ORDER BY EMPLOYEE OR SELECT D.DEPTNO, M.LASTNAME AS MANAGER, E.LASTNAME AS EMPLOYEE, M.HIREDATE AS M_HIREDATE, E.HIREDATE AS E_HIREDATE FROM EMPLOYEE AS E JOIN DEPARTMENT AS D ON E.WORKDEPT = D.DEPTNO JOIN EMPLOYEE AS M ON M.EMPNO = D.MGRNO WHERE M.HIREDATE > E.HIREDATE AND E.WORKDEPT = 'A00' ORDER BY E.LASTNAME END OF SOLUTION
Copyright IBM Corp. 1999, 2000 Exercise 2. Multiple Tables Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
2-15
Student Exercises
2-16
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Exercise 3. Scalar Functions and Arithmetic What This Exercise is About This exercise provides an opportunity to work with scalar functions and arithmetic expressions.
What You Should Be Able to Do At the end of the lab, you should be able to: Code queries that use scalar functions Code queries by using calculated expressions in the select list and in the WHERE clause Use basic scalar functions Use the CONCAT operator
Introduction See the data model at the start of this exercise guide to get the column names and descriptions for each table.
Required Materials Student handout SQL Reference
Copyright IBM Corp. 1999, 2000 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 Use the following column names for the two generated columns: INC-Y-SALARY and INC-M-SALARY Use the proper conversion function to display the increased salary and monthly salary with two of the digits to the right of the decimal point. Sort the results by annual salary.
Problem 2 All employees with an education level of 18 or 20 will receive a salary increase of $1,200 and their bonus will be cut in half. List last name, education level, new salary, and new bonus for these employees. Display the new bonus with two digits to the right of the decimal point. Use the column names NEW-SALARY and NEW-BONUS for the generated columns. Employees with an education level of 20 should be listed first. For employees with the same education level, sort the list by salary.
Problem 3 The salary will be decreased by $1,000 for all employees matching the following criteria: They belong to department D11 Their salary is more than or equal to 80 percent of $20,000 Their salary is less than or equal to 120 percent of $20,000 Use the name DECR-SALARY for the generated column. List department number, last name, salary, and decreased salary. Sort the result by salary.
Problem 4 Produce a list of all employees in department D11 that have an income (sum of salary, commission, and bonus) that is greater than their salary increased by 10 percent. Name the generated column INCOME. List department number, last name, and income. Sort the result in descending order by income. For this problem assume that all employees have non-null salaries, commissions, and bonuses.
3-2
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 5 List all departments that have no manager assigned. List department number, department name, and manager number. Replace unknown manager numbers with the word UNKNOWN and name the column MGRNO.
Problem 6 List the project number and major project number for all projects that have a project number beginning with MA. If the major project number is unknown, display the text 'MAIN PROJECT.' Name the derived column MAJOR PROJECT. Sequence the results by PROJNO.
Problem 7 List all employees who were younger than 25 when they joined the company. List their employee number, last name, and age when they joined the company. Name the derived column AGE. Sort the result by age and then by employee number.
Problem 8 Provide a list of all projects which ended on December 1, 1982. Display the year and month of the starting date and the project number. Sort the result by project number. Name the derived columns YEAR and MONTH.
Problem 9 List the project number and duration, in weeks, of all projects that have a project number beginning with MA. The duration should be rounded and displayed with one decimal position. Name the derived column WEEKS. Order the list by the project number.
Problem 10 For projects that have a project number beginning with MA, list the project number, project ending date, and a modified ending date assuming the projects will be delayed by 10 percent. Name the column containing PRENDATE, ESTIMATED. Name the derived column EXPECTED. Order the list by project number.
Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
3-3
Student Exercises
Problem 11 How many days are between the first manned landing on the moon (July 20, 1969) and the first day of the year 2000? Since no columns from a specific table are used in this problem, you can use any table in the FROM clause but you should indicate a WHERE condition that derives a single result row (unique key). You may also select from the SYSIBM.SYSDUMMY1 table which produces a one row result. Name the derived column DAYS.
END OF LAB
3-4
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem List With Expected Results Problem 1 For employees whose salary, increased by 5 percent, is less than or equal to $20,000, list the following: Last name Current Salary Salary increased by 5 percent Monthly salary increased by 5 percent Use the following column names for the two generated columns: INC-Y-SALARY and INC-M-SALARY Use the proper conversion function to display the increased salary and monthly salary with two of the digits to the right of the decimal point. Sort the results by annual salary.
Result
LASTNAME SALARY INC-Y-SALARY INC-M-SALARY ---------+---------+---------+---------+---------+---------+--PARKER 15340.00 16107.00 1342.25 SETRIGHT 15900.00 16695.00 1391.25 JOHNSON 17250.00 18112.50 1509.37 SMITH 17750.00 18637.50 1553.12 JONES 18270.00 19183.50 1598.62 Problem 2 All employees with an education level of 18 or 20 will receive a salary increase of $1,200 and their bonus will be cut in half. List last name, education level, new salary, and new bonus for these employees. Display the new bonus with two digits to the right of the decimal point. Use the column names NEW-SALARY and NEW-BONUS for the generated columns. Employees with an education level of 20 should be listed first. For employees with the same education level, sort the list by salary.
Result
LASTNAME EDLEVEL NEW-SALARY NEW-BONUS ---------+---------+---------+---------+---------+-KWAN 20 39450.00 400.00 NICHOLLS 18 29620.00 300.00 LUTZ 18 31040.00 300.00 THOMPSON 18 42450.00 400.00 HAAS 18 53950.00 500.00
Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic 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 the following criteria: They belong to department D11 Their salary is more than or equal to 80 percent of $20,000 Their salary is less than or equal to 120 percent of $20,000 Use the name DECR-SALARY for the generated column. List department number, last name, salary, and decreased salary. Sort the result by salary.
Result
WORKDEPT LASTNAME SALARY DECR-SALARY ---------+---------+---------+---------+---------+-D11 JONES 18270.00 17270.00 D11 WALKER 20450.00 19450.00 D11 SCOUTTEN 21340.00 20340.00 D11 PIANKA 22250.00 21250.00 Problem 4 Produce a list of all employees in department D11 that have an income (sum of salary, commission, and bonus) that is greater than their salary increased by 10 percent. Name the generated column INCOME. List department number, last name, and income. Sort the result in descending order by income. For this problem assume that all employees have non-null salaries, commissions, and bonuses.
Result
WORKDEPT LASTNAME INCOME ---------+---------+---------+---------+ D11 LUTZ 32827.00 D11 BROWN 30557.00 D11 YOSHIMURA 27154.00 D11 SCOUTTEN 23547.00 D11 JONES 20132.00 Problem 5 List all departments that have no manager assigned. List department number, department name, and manager number. Replace unknown manager numbers with the word UNKNOWN and name the column MGRNO.
Result
DEPTNO DEPTNAME MGRNO ------ ----------------------------- ------D01 DEVELOPMENT CENTER UNKNOWN
3-6
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 6 List the project number and major project number for all projects that have a project number beginning with MA. If the major project number is unknown, display the text 'MAIN PROJECT.' Name the derived column MAJOR PROJECT. Sequence the results by PROJNO.
Result
PROJNO MAJOR PROJECT ---------+---------+MA2100 MAIN PROJECT MA2110 MA2100 MA2111 MA2110 MA2112 MA2110 MA2113 MA2110 Problem 7 List all employees who were younger than 25 when they joined the company. List their employee number, last name, and age when they joined the company. Name the derived column AGE. Sort the result by age and then by employee number.
Result
EMPNO LASTNAME AGE ---------+---------+---------+-----000220 LUTZ 20 000340 GOUNOT 20 000120 O'CONNELL 21 000160 PIANKA 22 000190 WALKER 22 000050 GEYER 23 000100 SPENSER 23 000150 ADAMSON 24 000180 SCOUTTEN 24 000200 BROWN 24
Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
3-7
Student Exercises
Problem 8 Provide a list of all projects which ended on December 1, 1982. Display the year and month of the starting date and the project number. Sort the result by project number. Name the derived columns YEAR and MONTH.
Result
YEAR MONTH PROJNO ---------+---------+---------+-1982 1 MA2111 1982 1 MA2112 1982 2 MA2113 Problem 9 List the project number and duration, in weeks, of all projects that have a project number beginning with MA. The duration should be rounded and displayed with one decimal position. Name the derived column WEEKS. Order the list by the project number.
Result
PROJNO WEEKS ---------+-------MA2100 56.6 MA2110 56.6 MA2111 47.7 MA2112 47.7 MA2113 41.3 Problem 10 For projects that have a project number beginning with MA, list the project number, project ending date, and a modified ending date assuming the projects will be delayed by 10 percent. Name the column containing PRENDATE, ESTIMATED. Name the derived column EXPECTED. Order the list by project number.
Result
PROJNO ESTIMATED EXPECTED ---------+---------+---------+ MA2100 1983-02-01 1983-03-12 MA2110 1983-02-01 1983-03-12 MA2111 1982-12-01 1983-01-03 MA2112 1982-12-01 1983-01-03 MA2113 1982-12-01 1982-12-29
3-8
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 11 How many days are between the first manned landing on the moon (July 20, 1969) and the first day of the year 2000? Since no columns from a specific table are used in this problem, you can use any table in the FROM clause but you should indicate a WHERE condition that derives a single result row (unique key). You may also select from the SYSIBM.SYSDUMMY1 table which produces a one row result. Name the derived column DAYS.
Result
DAYS ---------+11122
END OF Problem List With Expected Results
Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
3-9
Student Exercises
Solution Problem 1
SELECT LASTNAME, SALARY, DECIMAL(SALARY*1.05,9,2) AS "INC-Y-SALARY", DECIMAL(SALARY*1.05/12,9,2) AS "INC-M-SALARY" FROM EMPLOYEE WHERE SALARY*1.05 1.1*SALARY AND WORKDEPT = 'D11' ORDER BY 3 DESC ALTERNATIVE SELECT WORKDEPT, LASTNAME, SALARY+COMM+BONUS AS INCOME FROM EMPLOYEE WHERE SALARY+COMM+BONUS > 1.1*SALARY AND WORKDEPT = 'D11' ORDER BY INCOME DESC Problem 5
SELECT DEPTNO, DEPTNAME , COALESCE(MGRNO, 'UNKNOWN') AS MGRNO FROM DEPARTMENT WHERE MGRNO IS NULL Problem 6
SELECT PROJNO, COALESCE(MAJPROJ,'MAIN PROJECT') AS "MAJOR PROJECT" FROM PROJECT WHERE SUBSTR(PROJNO,1,2) ='MA' ORDER BY PROJNO Problem 7
SELECT EMPNO, LASTNAME, YEAR(HIREDATE-BIRTHDATE) AS AGE FROM EMPLOYEE WHERE YEAR(HIREDATE-BIRTHDATE) < 25 ORDER BY AGE, EMPNO Problem 8
SELECT YEAR(PRSTDATE) AS YEAR, MONTH(PRSTDATE) AS MONTH, PROJNO FROM PROJECT WHERE PRENDATE = '1982-12-01' ORDER BY PROJNO Problem 9
SELECT PROJNO, DECIMAL((DAYS(PRENDATE)-DAYS(PRSTDATE))/7.+0.05,8,1) AS WEEKS FROM PROJECT WHERE PROJNO LIKE 'MA%' ORDER BY PROJNO
Copyright IBM Corp. 1999, 2000 Exercise 3. Scalar Functions and Arithmetic Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
3-11
Student Exercises
Problem 10
SELECT PROJNO, PRENDATE AS ESTIMATED, PRSTDATE + ((DAYS(PRENDATE) - DAYS(PRSTDATE))*1.1) DAYS AS EXPECTED FROM PROJECT WHERE PROJNO LIKE 'MA%' ORDER BY PROJNO Problem 11
SELECT DAYS('2000-01-01')-DAYS('1969-07-20') AS DAYS FROM EMPLOYEE WHERE EMPNO = '000010' ALTERNATIVE SELECT DAYS('2000-01-01')-DAYS('1969-07-20') AS DAYS FROM SYSIBM.SYSDUMMY1 END OF SOLUTION
3-12
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Exercise 4. Column Functions and Grouping What This Exercise is About This exercise provides an opportunity to work with column functions.
What You Should Be Able to Do At the end of the lab, you should be able to: Code queries that use column functions
Introduction See the data model at the start of this exercise guide to get the column names and descriptions for each table.
Required Materials Student handout SQL Reference
Copyright IBM Corp. 1999, 2000 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 each department. Name the derived column SUM_SALARY.
Problem 2 For all departments, display the department number and the number of employees. Name the derived column EMP_COUNT.
Problem 3 Display those departments which have more than 3 employees.
Problem 4 For all departments with at least one designer, display the number of designers and the department number. Name the derived column DESIGNER.
Problem 5 Show the average salary for men and the average salary for women for each department. Display the work department, the sex, the average salary, average bonus, average commission, and the number of people in each group. Include only those groups that have two or more people. Show only two decimal places in the averages. Use the following names for the derived columns: AVG-SALARY, AVG-BONUS, AVG-COMM, and COUNT.
Problem 6 Display the average bonus and average commission for all departments with an average bonus greater than $500 and an average commission greater than $2,000. Display all averages with two digits to the right of the decimal point. Use the column headings AVG-BONUS and AVG-COMM for the derived columns.
END OF LAB
4-2
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem List With Expected Results Problem 1 For all departments, display department number and the sum of all salaries for each department. Name the derived column SUM_SALARY.
Result
WORKDEPT SUM_SALARY -------- ---------A00 128500.00 B01 41250.00 C01 90470.00 D11 222100.00 D21 150920.00 E01 40175.00 E11 104990.00 E21 95310.00 Problem 2 For all departments, display the department number and the number of employees. Name the derived column EMP_COUNT.
Result
WORKDEPT EMP_COUNT ---------------A00 3 B01 1 C01 3 D11 9 D21 6 E01 1 E11 5 E21 4 Problem 3 Display those departments which have more than 3 employees.
Result
WORKDEPT -------D11 D21 E11 E21
Copyright IBM Corp. 1999, 2000 Exercise 4. Column Functions and Grouping Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
4-3
Student Exercises
Problem 4 For all departments with at least one designer, display the number of designers and the department number. Name the derived column DESIGNER.
Result
DESIGNER WORKDEPT ---------+---------+8 D11 Problem 5 Show the average salary for men and the average salary for women for each department. Display the work department, the sex, the average salary, average bonus, average commission, and the number of people in each group. Include only those groups that have two or more people. Show only two decimal places in the averages. Use the following names for the derived columns: AVG-SALARY, AVG-BONUS, AVG-COMM, and COUNT.
Result
WORKDEPT SEX AVG-SALARY AVG-BONUS AVG-COMM COUNT ---------+---------+---------+---------+---------+------A00 M 37875.00 750.00 3030.00 2 C01 F 30156.66 633.33 2412.66 3 D11 F 24476.66 500.00 1958.00 3 D11 M 24778.33 500.00 1981.83 6 D21 F 26933.33 500.00 2154.33 3 D21 M 23373.33 466.66 1869.66 3 E11 F 23966.66 466.66 1917.33 3 E11 M 16545.00 350.00 1323.50 2 E21 M 23827.50 475.00 1906.25 4 Problem 6 Display the average bonus and average commission for all departments with an average bonus greater than $500 and an average commission greater than $2,000. Display all averages with two digits to the right of the decimal point. Use the column headings AVG-BONUS and AVG-COMM for the derived columns.
Result
WORKDEPT AVG-BONUS AVG-COMM ---------+---------+---------+---------+-----A00 833.33 3426.66 B01 800.00 3300.00 C01 633.33 2412.66 E01 800.00 3214.00 END OF Problem List With Expected Results
4-4
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Solution Problem 1
SELECT WORKDEPT, SUM(SALARY) AS SUM_SALARY FROM EMPLOYEE GROUP BY WORKDEPT Problem 2
SELECT WORKDEPT, COUNT(*) AS EMP_COUNT FROM EMPLOYEE GROUP BY WORKDEPT Problem 3
SELECT WORKDEPT FROM EMPLOYEE GROUP BY WORKDEPT HAVING COUNT(*) > 3 Problem 4
SELECT COUNT(*) AS DESIGNER, WORKDEPT FROM EMPLOYEE WHERE JOB = 'DESIGNER' GROUP BY WORKDEPT Problem 5
SELECT WORKDEPT, SEX, DECIMAL(AVG(SALARY),8,2) AS "AVG-SALARY", DECIMAL(AVG(BONUS),8,2) AS "AVG-BONUS", DECIMAL(AVG(COMM),8,2) AS "AVG-COMM", COUNT(*) AS COUNT FROM EMPLOYEE GROUP BY WORKDEPT, SEX HAVING COUNT(*) > 1 Problem 6
SELECT WORKDEPT, DECIMAL(AVG(BONUS),9,2) AS "AVG-BONUS", DECIMAL(AVG(COMM),9,2) AS "AVG-COMM" FROM EMPLOYEE GROUP BY WORKDEPT HAVING AVG(BONUS) > 500 AND AVG(COMM) > 2000
END OF SOLUTION
Copyright IBM Corp. 1999, 2000 Exercise 4. Column Functions and Grouping Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
4-5
Student Exercises
4-6
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Exercise 5. UNION What This Exercise is About This exercise provides an opportunity to work with the UNION keyword.
What You Should Be Able to Do At the end of the lab you should be able to: Derive a final result by combining results from multiple SELECTs.
Introduction See the data model at the start of this exercise guide to get the column names and descriptions for each table.
Required Materials Student handout SQL Reference
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercise 5. UNION
5-1
Student Exercises
Problem List Problem 1 List the names and salaries for the non-managers working in department D21 showing the effects of a 10 percent raise. Use the following output as a guide. Apply and appropriate ORDER BY clause to achieve the required results. Use the column headings shown.
LASTNAME --------------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): Department number Text: DEPARTMENT Department name Second line(s) (if data available - one line per project): Department number Project number Project name Subsequent line(s) (if data available - one line per employee): Department number Employee number Last name
5-2
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
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 Subsequent line(s) (one per employee working on the project): Project number The employee number of the employee performing the activity Activity starting date Activity ending date Sequence the results by the project number, then by employee number, and finally by the starting date.
END OF LAB
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercise 5. UNION
5-3
Student Exercises
Problem List With Expected Results Problem 1 List the names and salaries for the non-managers working in department D21 showing the effects of a 10 percent raise. Use the following output as a guide. Apply and appropriate ORDER BY clause to achieve the required results. Use the column headings shown.
LASTNAME --------------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 ... ...
LASTNAME --------------JEFFERSON JEFFERSON JOHNSON JOHNSON MARINO MARINO PEREZ PEREZ SMITH SMITH
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
5-4
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 2 List the department number, employee number, and salaries of all employees in department A00. For the last line of the report, display the sum of all the salaries.
Result
WORKDEPT EMPNO SALARY ---------+---------+---------+-------A00 000120 29250.00 A00 000110 46500.00 A00 000010 52750.00 A00 SUM 128500.00
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercise 5. UNION
5-5
Student Exercises
Problem 3 For departments A00, B01, and C01, list the projects assigned to them and the employees in each department. The output should consist of up to three types of lines for each department as follows: See expected results for clarification of the following instructions. First line (one per department): Department number Text: DEPARTMENT Department name Second line(s) (if data available - one line per project): Department number Project number Project name Subsequent line(s) (if data available - one line per employee): Department number Employee number Last name
Result
DEPTNO INFO DEPTNAME ---------+---------+---------+---------+---------+----A00 DEPARTMENT SPIFFY COMPUTER SERVICE DIV. 1 A00 000010 HAAS 3 A00 000110 LUCCHESI 3 A00 000120 O'CONNELL 3 B01 DEPARTMENT PLANNING 1 B01 PL2100 WELD LINE PLANNING 2 B01 000020 THOMPSON 3 C01 DEPARTMENT INFORMATION CENTER 1 C01 IF1000 QUERY SERVICES 2 C01 IF2000 USER EDUCATION 2 C01 000030 KWAN 3 C01 000130 QUINTANA 3 C01 000140 NICHOLLS 3
5-6
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
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 Subsequent line(s) (one per employee working on the project): Project number The employee number of the employee performing the activity Activity starting date Activity ending date Sequence the results by the project number, then by employee number, and finally by the starting date.
Result
PROJNO RESPEMP PRSTDATE PRENDATE ---------+---------+---------+---------+-------PROJECT IF1000 000030 1982-01-01 1983-02-01 IF1000 000030 1982-06-01 1983-01-01 IF1000 000130 1982-01-01 1982-10-01 IF1000 000130 1982-10-01 1983-01-01 IF1000 000140 1982-10-01 1983-01-01 PROJECT IF2000 000030 1982-01-01 1983-02-01 IF2000 000030 1982-01-01 1983-01-01 IF2000 000140 1982-01-01 1982-03-01 IF2000 000140 1982-03-01 1982-07-01 IF2000 000140 1982-03-01 1982-07-01 IF2000 000140 1982-10-01 1983-01-01
END OF Problem List With Expected Results
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercise 5. UNION
5-7
Student Exercises
Solution Problem 1
SELECT LASTNAME, FIRSTNME, 'BEFORE A RAISE' AS WHEN, SALARY FROM EMPLOYEE WHERE WORKDEPT = 'D21' AND JOB 'MANAGER' UNION ALL SELECT LASTNAME, FIRSTNME, 'AFTER A RAISE' AS WHEN, SALARY * 1.1 AS SALARY FROM EMPLOYEE WHERE WORKDEPT = 'D21' AND JOB 'MANAGER' ORDER BY LASTNAME, FIRSTNME, WHEN DESC Problem 2
SELECT WORKDEPT, EMPNO, SALARY FROM EMPLOYEE WHERE WORKDEPT = 'A00' UNION ALL SELECT WORKDEPT, 'SUM', SUM(SALARY) FROM EMPLOYEE WHERE WORKDEPT = 'A00' GROUP BY WORKDEPT ORDER BY 3
5-8
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 3
SELECT DEPTNO, 'DEPARTMENT' AS INFO, DEPTNAME, 1 FROM DEPARTMENT WHERE DEPTNO IN ('A00' , 'B01' , 'C01') UNION ALL SELECT DEPTNO, PROJNO AS INFO, PROJNAME, 2 FROM PROJECT WHERE DEPTNO IN ('A00' , 'B01' , 'C01') UNION ALL SELECT WORKDEPT AS DEPTNO, EMPNO AS INFO, LASTNAME, 3 FROM EMPLOYEE WHERE WORKDEPT IN ('A00' , 'B01' , 'C01') ORDER BY DEPTNO, 4 Problem 4
SELECT 'PROJECT', PROJNO, RESPEMP, PRSTDATE, PRENDATE FROM PROJECT WHERE PROJNO LIKE 'IF%' UNION ALL SELECT ' ', PROJNO, EMPNO, EMSTDATE, EMENDATE FROM EMP_ACT WHERE PROJNO LIKE 'IF%' ORDER BY 2, 1 DESC, 3, 4
END OF SOLUTION
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Exercise 5. UNION
5-9
Student Exercises
5-10
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Exercise 6. Using Subqueries What This Exercise is About This exercise provides an opportunity to work with subqueries.
What You Should Be Able to Do At the end of the lab, you should be able to: Code basic subqueries Code more complex subqueries using the keyword IN
Introduction See the data model at the start of this exercise guide to get the column names and descriptions for each table.
Required Materials Student handout SQL Reference
Copyright IBM Corp. 1999, 2000 Exercise 6. 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. Display department number, employee number, last name, and salary. Sort the list by the department number and employee number.
Problem 2 List employee number and last name of all employees not assigned to any projects. This means that table EMP_ACT does not contain a row with their employee number.
Problem 3 List project number and duration (in days) of the project with the shortest duration. Name the derived column DAYS.
Problem 4 List department number, department name, last name, and first name of all those employees in departments that have only male employees.
Problem 5 We want to do a salary analysis for people that have the same job and education level as the employee Stern. Show the last name, job, edlevel, the number of years they've worked as of January 1, 2000, and their salary. Name the derived column YEARS. Sort the listing by highest salary first.
END OF LAB
6-2
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem List With Expected Results Problem 1 List those employees that have a salary which is greater than or equal to the average salary of all employees plus $5,000. Display department number, employee number, last name, and salary. Sort the list by the department number and employee number.
Result
WORKDEPT EMPNO LASTNAME SALARY ---------+---------+---------+-----A00 000010 HAAS 52750.00 A00 000110 LUCCHESI 46500.00 B01 000020 THOMPSON 41250.00 C01 000030 KWAN 38250.00 D21 000070 PULASKI 36170.00 E01 000050 GEYER 40175.00 Problem 2 List employee number and last name of all employees not assigned to any projects. This means that table EMP_ACT does not contain a row with their employee number.
Result
EMPNO LASTNAME ---------+------000060 STERN 000120 O'CONNELL
Copyright IBM Corp. 1999, 2000 Exercise 6. Subqueries 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
PROJNO DAYS ---------+--------PL2100 257 Problem 4 List department number, department name, last name, and first name of all those employees in departments that have only male employees.
Result
DEPTNO DEPTNAME LASTNAME FIRSTNME ---------+---------+---------+---------+---------+----B01 PLANNING THOMPSON MICHAEL E01 SUPPORT SERVICES GEYER JOHN E21 SOFTWARE SUPPORT SPENSER THEODORE E21 SOFTWARE SUPPORT MEHTA RAMLAL E21 SOFTWARE SUPPORT LEE WING E21 SOFTWARE SUPPORT GOUNOT JASON Problem 5 We want to do a salary analysis for people that have the same job and education level as the employee Stern. Show the last name, job, edlevel, the number of years they've worked as of January 1, 2000, and their salary. Name the derived column YEARS. Sort the listing by highest salary first.
Result
LASTNAME --------------GEYER PULASKI STERN HENDERSON
6-4
DB2 SQL Workshop
JOB EDLEVEL YEARS SALARY -------- ------- ----------- -----------MANAGER 16 50 40175.00 MANAGER 16 19 36170.00 MANAGER 16 26 32250.00 MANAGER 16 29 29750.00
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
END OF Problem List With Expected Results
Copyright IBM Corp. 1999, 2000 Exercise 6. Subqueries Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
6-5
Student Exercises
Solution Problem 1
SELECT WORKDEPT, EMPNO, LASTNAME, SALARY FROM EMPLOYEE WHERE SALARY >=(SELECT AVG(SALARY) + 5000 FROM EMPLOYEE) ORDER BY WORKDEPT, EMPNO Problem 2
SELECT EMPNO, LASTNAME FROM EMPLOYEE WHERE EMPNO NOT IN (SELECT EMPNO FROM EMP_ACT) Problem 3
SELECT PROJNO, DAYS(PRENDATE)-DAYS(PRSTDATE) AS DAYS FROM PROJECT WHERE DAYS(PRENDATE)-DAYS(PRSTDATE) = (SELECT MIN(DAYS(PRENDATE)-DAYS(PRSTDATE)) FROM PROJECT) Problem 4
SELECT DEPTNO, DEPTNAME, LASTNAME, FIRSTNME FROM DEPARTMENT, EMPLOYEE WHERE DEPTNO = WORKDEPT AND DEPTNO NOT IN(SELECT WORKDEPT FROM EMPLOYEE WHERE SEX = 'F') Problem 5
SELECT LASTNAME, JOB, EDLEVEL, YEAR('2000-01-01' - HIREDATE) AS YEARS, SALARY FROM EMPLOYEE WHERE (JOB, EDLEVEL) IN (SELECT JOB, EDLEVEL FROM EMPLOYEE WHERE LASTNAME = 'STERN') ORDER BY SALARY DESC
END OF SOLUTION
6-6
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Exercise 7. Maintaining Data What This Exercise is About This exercise provides an opportunity to code SQL statements in order to manipulate data.
What You Should Be Able to Do At the end of the lab, you should be able to: Code UPDATE and DELETE statements with (or without) conditions. Code INSERT statements using a VALUE or a SELECT clause.
Introduction See the data model at the start of this exercise guide to get the column names and descriptions for each table.
Required Materials Student handout SQL Reference
Copyright IBM Corp. 1999, 2000 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 Create table TESTEMP with the following columns: EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS The data types and null characteristics for these columns should be the same as the corresponding columns in the EMPLOYEE table described at the beginning of the lab exercises. For the OS/390 environment, use the following IN clause at the end of your statement: IN DBCF12xx.TSxx (where xx is your assigned lab number).
Problem 2 Mr. Smith, Ms Baker, and Ms Thomas joined the company. Their data is as follows: Employee numbers: 000111, 000222, 000333 Last name: SMITH, BAKER, THOMAS Department number: C01, A00, D11 Date hired: June 6, 2000 Salary: $25,000, $28,000, $33,000 Bonus: $0, NULL, $0 Add the new employees to the TESTEMP table.
Problem 3 Insert data into the TESTEMP table by copying the appropriate columns in the EMPLOYEE table for those employees that have an employee number less than or equal to 50.
Problem 4 Mr. Smith receives a bonus of $500. Make the appropriate data change.
Problem 5 All employees in department C01 have done a good job. Therefore, they receive a salary increase of $1,000.
Problem 6 After a short time Mr. Smith leaves the company. Make the appropriate changes.
7-2
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 7 Insert data into the TESTEMP table by copying the columns in the EMPLOYEE table where the employees have an employee number greater than 50.
Problem 8 Make the appropriate changes for the following situation: Theodore Spenser is assigned to department E01.
Problem 9 Mrs. Brown joined the company. For Mrs. Brown enter the following data: Employee number: 360 Last name: BROWN Department number: D01 Date hired: Date when data is entered Salary: $45,000 Bonus: unknown
Problem 10 Delete all rows from the TESTEMP table and examine the contents. Then drop the TESTEMP table and try to examine the contents.
END OF LAB
Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data 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 Create table TESTEMP with the following columns: EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS The data types and null characteristics for these columns should be the same as the corresponding columns in the EMPLOYEE table described at the beginning of the lab exercises. For the OS/390 environment, use the following IN clause at the end of your statement: IN DBCF12xx.TSxx (where xx is your assigned lab number).
Result If running from SPUFI:
---------+---------+---------+---------+---------+-------DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+-------DSNE617I COMMIT PERFORMED, SQLCODE IS 0 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 If running from QMF:
OK, running your query changed the database. COMMAND ===> Problem 2 Mr. Smith, Ms Baker, and Ms Thomas joined the company. Their data is as follows: Employee numbers: 000111, 000222, 000333 Last name: SMITH, BAKER, THOMAS Department number: C01, A00, D11 Date hired: June 6, 2000 Salary: $25,000, $28,000, $33,000 Bonus: $0, NULL, $0 Add the new employees to the TESTEMP table.
Result Confirmation that your rows were added to the table. In SPUFI:
7-4
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
If rows were inserted one at a time, the following message should appear after each INSERT statement; ---------+---------+---------+---------+---------+---------+ DSNE615I NUMBER OF ROWS AFFECTED IS 1 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+---------+ If all three rows were inserted through a single INSERT statement then a message similar to the following should be observed: ---------+---------+---------+---------+---------+---------+ DSNE615I NUMBER OF ROWS AFFECTED IS 3 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+---------+ From QMF:
OK, number of rows modified in the database: 3. COMMAND ===> or, if each row was inserted separately, the following message should be received for each row inserted. OK, number of rows modified in the database: 1. COMMAND ===> Problem 3 Insert data into the TESTEMP table by copying the appropriate columns in the EMPLOYEE table for those employees that have an employee number less than or equal to 50.
Result In SPUFI:
---------+---------+---------+---------+---------+-------DSNE615I NUMBER OF ROWS AFFECTED IS 4 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+-------DSNE617I COMMIT PERFORMED, SQLCODE IS 0 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+-------In QMF: OK, number of rows modified in the database: 4. COMMAND ===>
Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
7-5
Student Exercises
Problem 4 Mr. Smith receives a bonus of $500. Make the appropriate data change.
Result
In SPUFI: ---------+---------+---------+---------+---------+------DSNE615I NUMBER OF ROWS AFFECTED IS 1 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+------DSNE617I COMMIT PERFORMED, SQLCODE IS 0 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+------In QMF: OK, number of rows modified in the database: 1 COMMAND ===> Problem 5 All employees in department C01 have done a good job. Therefore, they receive a salary increase of $1,000.
Result
In SPUFI: ---------+---------+---------+---------+---------+---------+DSNE615I NUMBER OF ROWS AFFECTED IS 2 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+---------+In QMF: OK, number of rows modified in the database: 2. COMMAND ===>
7-6
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 6 After a short time Mr. Smith leaves the company. Make the appropriate changes.
Result
In SPUFI: ---------+---------+---------+---------+---------+------DSNE615I NUMBER OF ROWS AFFECTED IS 1 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+------In QMF: OK, number of rows modified in the database: 1. COMMAND ===> Problem 7 Insert data into the TESTEMP table by copying the columns in the EMPLOYEE table where the employees have an employee number greater than 50.
Result
In SPUFI: ---------+---------+---------+---------+---------+-------DSNE615I NUMBER OF ROWS AFFECTED IS 28 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+-------In QMF: OK, number of rows modified in the database: 28. COMMAND ===>
Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
7-7
Student Exercises
Problem 8 Make the appropriate changes for the following situation: Theodore Spenser is assigned to department E01.
Result
In SPUFI: ---------+---------+---------+---------+---------+-------DSNE615I NUMBER OF ROWS AFFECTED IS 1 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+-------In QMF: OK, number of rows modified in the database: 1. COMMAND ===> Problem 9 Mrs. Brown joined the company. For Mrs. Brown enter the following data: Employee number: 360 Last name: BROWN Department number: D01 Date hired: Date when data is entered Salary: $45,000 Bonus: unknown
Result
In SPUFI: ---------+---------+---------+---------+---------+-------DSNE615I NUMBER OF ROWS AFFECTED IS 1 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+-------In QMF: OK, number of rows modified in the database: 1. COMMAND ===> Problem 10 Delete all rows from the TESTEMP table and examine the contents. Then drop the TESTEMP table and try to examine the contents.
7-8
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Result
In SPUFI, after deleting the rows: DSNE615I NUMBER OF ROWS AFFECTED IS 35 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+---------+---------+-SELECT * FROM TESTEMP; ---------+---------+---------+---------+---------+---------+---------+-EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS ---------+---------+---------+---------+---------+---------+---------+-DSNE610I NUMBER OF ROWS DISPLAYED IS 0 DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100 After dropping the table: ---------+---------+---------+---------+---------+---------+---------+-DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0 ---------+---------+---------+---------+---------+---------+---------+-SELECT * FROM TESTEMP; ---------+---------+---------+---------+---------+---------+---------+-DSNT408I SQLCODE = -204, ERROR: USERID.TESTEMP IS AN UNDEFINED NAME DSNT418I SQLSTATE = 42704 SQLSTATE RETURN CODE In QMF, after deleting the rows: OK, number of rows modified in the database: 35. COMMAND ===> SELECT * FROM TESTEMP; EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS ------ --------------- -------- ---------- ------------ -----------After dropping the table: OK, running your query changed the database. COMMAND ===> SELECT * FROM TESTEMP; QUERY MESSAGES: USERID.TESTEMP could not be found. END OF LAB (with hints)
Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
7-9
Student Exercises
Solutions Problem 1
CREATE TABLE TESTEMP (EMPNO CHAR(6) NOT NULL, LASTNAME VARCHAR(15) NOT NULL, WORKDEPT CHAR(3) , HIREDATE DATE , SALARY DECIMAL(9,2) , BONUS DECIMAL(9,2) ) IN DBCF12xx.TSxx -- This IN clause is for OS/390 classes -- classes taught in other environments may or may not need an IN -- clause. See your instructor for additional information. Problem 2 Version 7 INSERT syntax follows:
INSERT INTO TESTEMP VALUES ('000111', 'SMITH', 'C01', '06/06/2000', 25000, 0), ('000222', 'BAKER', 'A00', '06/06/2000', 28000, NULL), ('000333', 'THOMAS', 'D11', '06/06/2000', 33000, 0) or INSERT INTO TESTEMP (EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUS) VALUES ('000111', 'SMITH', 'C01', '06/06/2000', 25000, 0), ('000333', 'THOMAS', 'D11', '06/06/2000', 33000, 0) INSERT INTO TESTEMP (EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY) VALUES ('000222', 'BAKER', 'A00', '06/06/2000', 28000) Note that the stand alone insert for Baker's row does not list the BONUS column in the column list, nor is its value specified in the VALUES clause. As a result, the BONUS for Baker's row will be marked as unknown (NULL). Rows inserted: 3 Prior to version 7, each row must be added via a separate INSERT statement (only data for one row allowed per VALUES clause) as shown below:
7-10
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
INSERT INTO TESTEMP VALUES ('000111', 'SMITH', 'C01', '06/06/2000', 25000, 0); INSERT INTO TESTEMP VALUES ('000222', 'BAKER', 'A00', '06/06/2000', 28000, NULL); INSERT INTO TESTEMP VALUES ('000333', 'THOMAS', 'D11', '06/06/2000', 33000, 0); Problem 3
INSERT INTO TESTEMP (EMPNO,LASTNAME,WORKDEPT,HIREDATE,SALARY,BONUS ) SELECT EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUS FROM EMPLOYEE WHERE EMPNO '000050' Rows inserted: 28 Problem 8
UPDATE TESTEMP SET WORKDEPT = 'E01' WHERE EMPNO = '000100' Rows updated: 1 Problem 9
INSERT INTO TESTEMP VALUES ('000360', 'BROWN ', 'D01', CURRENT DATE, 45000, NULL ) -- For the above syntax to work a value must be provided for every -- column, in the sequence into which the values must go or INSERT INTO TESTEMP (EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY ) VALUES ('000360', 'BROWN ', 'D01', CURRENT DATE, 45000 ) -----7-12
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.
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Student Exercises
Problem 10
DELETE FROM TESTEMP; SELECT * FROM TESTEMP; DROP TABLE TESTEMP; SELECT * FROM TESTEMP;
END OF SOLUTION
Copyright IBM Corp. 1999, 2000 Exercise 7. Maintaining Data Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
7-13
Student Exercises
7-14
DB2 SQL Workshop
Copyright IBM Corp. 1999, 2000 Course materials may not be reproduced in whole or in part without the prior written permission of IBM.