102
DB2 SQL Workshop (Course Code CF12) Student Exercises ERC3.0 IBM Learning Services Worldwide Certified Material

Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

DB2 SQL Workshop(Course Code CF12)

Student ExercisesERC3.0

IBM Learning ServicesWorldwide Certified Material

Page 2: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Publishing Information

This publication has been produced using BookMaster (Program Number5668-015), the Document Composition Facility (Program Number 5748-XX9), andFreelance 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 theUnited States, or other countries, or both:

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

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

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

BookMaster DB2 OS/390QMF

December, 2000

The information contained in this document has not been submitted to any formal IBM test and isdistributed on an “as is” basis without any warranty either express or implied. The use of thisinformation or the implementation of any of these techniques is a customer responsibility and dependson 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 noguarantee that the same or similar results will result elsewhere. Customers attempting to adapt thesetechniques to their own environments do so at their own risk. The original repository material for thiscourse 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 ordisclosure is subject to restrictions set forth in GSA ADP Schedule Contract with IBM Corp.

Page 3: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Contents

Exercises Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v

Table Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

Table Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Exercises

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

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

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

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

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

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

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

Contents iii Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 4: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

iv DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 5: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Exercises Description

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

Feel free to consult with the instructor if you need a hint orencounter 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 resultsfrom the completed query. In most cases, the complete result isshown. In cases where the result set is too large, an ellipsis (...) isshown to indicate that there are additional rows in the result. Thenumber of rows for the result set is shown so you may verify yourresults.

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

General Information

This lab guide provides the information necessary to complete therequired 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 documentationdescribing the basics of each query tool.

The exercises for this course may be done in OS/390 or in aWorkstation environment. Depending on the class location, any ofthe following Workstation platforms may be available: OS/2, AIX,Windows 95 or Windows NT. Read the instructions for youroperating system before attempting the exercises. All Workstationplatforms use the same instructions.

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

Although saving your queries is not required, feel free to commentout or save your solutions.

Exercises Description v Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 6: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

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

vi DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 7: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

General Information for OS/390 Users

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

__________________________

__________________________

__________________________

The instructor will tell you how to logon to the OS/390 system andwhat specific user ID and password to use. In addition, you willprobably be given a team number: this number will be usedoccasionally during the exercises so that you can distinguish yourfiles 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 onhow to use the query tool you choose. For more information abouta tool, for example how to save a query, be sure to ask theinstructor.

For SPUFI, you will need a partitioned dataset in order tocomplete the exercises. This dataset already exists.

The dataset name is ________________________________________.

Each of the tables described in the Table Relationships section ofthis document already exists and will be accessed through viewsowned by your lab ID.

Your lab ID is _______________.

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

When you create TESTEMP table during the exercise 7, you mayneed to add an IN clause to your CREATE TABLE statements thatidentifies where the table is being created. The IN clause youneed 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.

Exercises Description vii Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 8: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

General Information for Workstation Users

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

__________________________

__________________________

__________________________

Students doing the exercises on workstation platforms do not needa team number. Each workstation student has a completestandalone environment and will not share any files or tables withother students. Each student will need to sign on with thefollowing information:

USERID is _____________________

PASSWORD is ____________________

The instructor will give you brief verbal or written instructions onhow to use the tool you choose so that you know enough to getstarted. If you want to know more about a tool, for example howto 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 ata command prompt:

db2 connect to sample

To connect in a client/server environment, enter the followingat a command prompt, substituting the userid and passwordyou were assigned:

db2 connect to sample user userid using password

Each of the tables described in the Table Relationships section ofthis document already exists and has the same qualifier.

The qualifier is _________________.

When you refer to these tables in your SQL, you will need to prefixthe table names with the qualifier identified above. For example, ifyou 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 toomit the qualifier or use your logon ID as qualifier. For example, ifyou are trying to read the TESTEMP table, you will need to write:

viii DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 9: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

SELECT * FROM TESTEMPor SELECT * FROM logon ID .TESTEMP

If you choose to do the labs in CLP, you may create your SQL inthe \CF12 directory.

Exercises Description ix Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 10: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Figure 0-1. Table Relationships (CF12X010)

Notes:

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

x DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 11: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Exercises Description xi Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 12: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

xii DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 13: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Table Descriptions

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

DEPARTMENT

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

EMPLOYEE

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

Column Name Meaning Data Type NULLS allowedDEPTNO Department Number CHAR(3) NDEPTNAME Department Name VARCHAR(36) NMGRNO Employee Number of the Responsible

ManagerCHAR(6) Y

ADMRDEPT Department Number of theDepartment to which the Departmentreports

CHAR(3) N

LOCATION Location Number CHAR(5) Y

Column Name Meaning Data Type NULLS allowedEMPNO Employee Number CHAR(6) NFIRSTNME First Name VARCHAR(20) NMIDINIT Middle Initial CHAR(1) NLASTNAME Last Name VARCHAR(15) NWORKDEPT Department in which the Employee

WorksCHAR(3) Y

PHONENO Phone Number CHAR(4) YHIREDATE Date of Hire DATE YJOB Job CHAR(8) YEDLEVEL Number of Years of Formal Education SMALLINT YSEX Sex (M male, F female) CHAR(1) YBIRTHDATE Date of Birth DATE YSALARY Yearly Salary DECIMAL(9, 2) YBONUS Yearly Bonus DECIMAL(9, 2) YCOMM Yearly Commission DECIMAL(9, 2) Y

Table Descriptions xiii Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 14: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

PROJECT

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

EMP_ACT

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

Column Name Meaning Data Type NULLS allowedPROJNO Project Number CHAR(6) NPROJNAME Project Name VARCHAR(24) NDEPTNO Responsible Department CHAR(3) NRESPEMP Employee Number of the Responsible

EmployeeCHAR(6) N

PRSTAFF Estimated Mean Staffing DECIMAL(5, 2) YPRSTDATE Estimated Start Date DATE YPRENDATE Estimated End Date DATE YMAJPROJ Major Project for a Subproject CHAR(6) Y

Column Name Meaning Data Type NULLS allowedEMPNO Employee Number of Employee

Performing the ActivityCHAR(6) N

PROJNO Project Number CHAR(6) NACTNO Activity Number SMALLINT NEMPTIME Proportion of Employee's Time Spent

on ProjectDECIMAL(5, 2) Y

EMSTDATE Date Activity Starts DATE YEMENDATE Date Activity Ends DATE Y

xiv DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 15: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Table Content

DEPARTMENT table

DEPTNO DEPTNAME MGRNO ADMRDEPT LOCATION------ ------------------------------ ------ -------- --------A00 SPIFFY COMPUTER SERVICE DIV. 000010 A00B01 PLANNING 000020 A00C01 INFORMATION CENTER 000030 A00D01 DEVELOPMENT CENTER - A00D11 MANUFACTURING SYSTEMS 000060 D01D21 ADMINISTRATION SYSTEMS 000070 D01E01 SUPPORT SERVICES 000050 A00E11 OPERATIONS 000090 E01E21 SOFTWARE SUPPORT 000100 E01

Table Content xv Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 16: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

EMPLOYEE table

EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE------ ---------- ------- ---------- -------- ------- ----------000010 CHRISTINE I HAAS A00 3978 1965-01-01000020 MICHAEL L THOMPSON B01 3476 1973-10-10000030 SALLY A KWAN C01 4738 1975-04-05000050 JOHN B GEYER E01 6789 1949-08-17000060 IRVING F STERN D11 6423 1973-09-14000070 EVA D PULASKI D21 7831 1980-09-30000090 EILEEN W HENDERSON E11 5498 1970-08-15000100 THEODORE Q SPENSER E21 0972 1980-06-19000110 VINCENZO G LUCCHESI A00 3490 1958-05-16000120 SEAN O'CONNELL A00 2167 1963-12-05000130 DOLORES M QUINTANA C01 4578 1971-07-28000140 HEATHER A NICHOLLS C01 1793 1976-12-15000150 BRUCE ADAMSON D11 4510 1972-02-12000160 ELIZABETH R PIANKA D11 3782 1977-10-11000170 MASATOSHI J YOSHIMURA D11 2890 1978-09-15000180 MARILYN S SCOUTTEN D11 1682 1973-07-07000190 JAMES H WALKER D11 2986 1974-07-26000200 DAVID BROWN D11 4501 1966-03-03000210 WILLIAM T JONES D11 0942 1979-04-11000220 JENNIFER K LUTZ D11 0672 1968-08-29000230 JAMES J JEFFERSON D21 4265 1966-11-21000240 SALVATORE M MARINO D21 3780 1979-12-05000250 DANIEL S SMITH D21 0961 1969-10-30000260 SYBIL V JOHNSON D21 8953 1975-09-11000270 MARIA L PEREZ D21 9001 1980-09-30000280 ETHEL R SCHNEIDER E11 8997 1967-03-24000290 JOHN R PARKER E11 4502 1980-05-30000300 PHILIP X SMITH E11 2095 1972-06-19000310 MAUDE F SETRIGHT E11 3332 1964-09-12000320 RAMLAL V MEHTA E21 9990 1965-07-07000330 WING LEE E21 2103 1976-02-23000340 JASON R GOUNOT E21 5698 1947-05-05

xvi DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 17: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

EMPLOYEE table (cont'd)

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

Table Content xvii Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 18: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

PROJECT table

DEPT PRPROJNO PROJNAME NO RESPEMP STAFF PRSTDATE PRENDATE MAJPROJ------ --------------------------------- ----- ---------- ---------- -------AD3100 ADMIN SERVICES D01 000010 6.50 1982-01-01 1983-02-01 -AD3110 GENERAL AD SYSTEMS D21 000070 6.00 1982-01-01 1983-02-01 AD3100AD3111 PAYROLL PROGRAMMING D21 000230 2.00 1982-01-01 1983-02-01 AD3110AD3112 PERSONNEL PROGRAMMG D21 000250 1.00 1982-01-01 1983-02-01 AD3110AD3113 ACCOUNT.PROGRAMMING D21 000270 2.00 1982-01-01 1983-02-01 AD3110IF1000 QUERY SERVICES C01 000030 2.00 1982-01-01 1983-02-01 -IF2000 USER EDUCATION C01 000030 1.00 1982-01-01 1983-02-01 -MA2100 WELD LINE AUTOMATION D01 000010 12.00 1982-01-01 1983-02-01 -MA2110 W L PROGRAMMING D11 000060 9.00 1982-01-01 1983-02-01 MA2100MA2111 W L PROGRAM DESIGN D11 000220 2.00 1982-01-01 1982-12-01 MA2110MA2112 W L ROBOT DESIGN D11 000150 3.00 1982-01-01 1982-12-01 MA2110MA2113 W L PROD CONT PROGS D11 000160 3.00 1982-02-15 1982-12-01 MA2110OP1000 OPERATION SUPPORT E01 000050 6.00 1982-01-01 1983-02-01 -OP1010 OPERATION E11 000090 5.00 1982-01-01 1983-02-01 OP1000OP2000 GEN SYSTEMS SERVICES E01 000050 5.00 1982-01-01 1983-02-01 -OP2010 SYSTEMS SUPPORT E21 000100 4.00 1982-01-01 1983-02-01 OP2000OP2011 SCP SYSTEMS SUPPORT E21 000320 1.00 1982-01-01 1983-02-01 OP2010OP2012 APPLICATIONS SUPPORT E21 000330 1.00 1982-01-01 1983-02-01 OP2010OP2013 DB/DC SUPPORT E21 000340 1.00 1982-01-01 1983-02-01 OP2010PL2100 WELD LINE PLANNING B01 000020 1.00 1982-01-01 1982-09-15 MA2100

xviii DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 19: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

EMP_ACT table

EMPNO PROJNO ACTNO EMPTIME EMSTDATE EMENDATE------ ------ ------ -------- ---------- ----------000010 AD3100 10 0.50 1982-01-01 1982-07-01000070 AD3110 10 1.00 1982-01-01 1983-02-01000230 AD3111 60 1.00 1982-01-01 1982-03-15000230 AD3111 60 0.50 1982-03-15 1982-04-15000230 AD3111 70 0.50 1982-03-15 1982-10-15000230 AD3111 80 0.50 1982-04-15 1982-10-15000230 AD3111 180 1.00 1982-10-15 1983-01-01000240 AD3111 70 1.00 1982-02-15 1982-09-15000240 AD3111 80 1.00 1982-09-15 1983-01-01000250 AD3112 60 0.50 1982-02-01 1982-03-15000250 AD3112 60 1.00 1982-01-01 1982-02-01000250 AD3112 60 1.00 1983-01-01 1983-02-01000250 AD3112 60 0.50 1982-12-01 1983-01-01000250 AD3112 70 1.00 1982-03-15 1982-08-15000250 AD3112 70 0.50 1982-02-01 1982-03-15000250 AD3112 70 0.25 1982-08-15 1982-10-15000250 AD3112 80 0.25 1982-08-15 1982-10-15000250 AD3112 80 0.50 1982-10-15 1982-12-01000250 AD3112 180 0.50 1982-08-15 1983-01-01000260 AD3113 70 0.50 1982-06-15 1982-07-01000260 AD3113 70 1.00 1982-07-01 1983-02-01000260 AD3113 80 1.00 1982-01-01 1982-03-01000260 AD3113 80 0.50 1982-03-01 1982-04-15000260 AD3113 180 0.50 1982-03-01 1982-04-15000260 AD3113 180 0.50 1982-06-01 1982-07-01000260 AD3113 180 1.00 1982-04-15 1982-06-01000270 AD3113 60 0.25 1982-09-01 1982-10-15000270 AD3113 60 1.00 1982-04-01 1982-09-01000270 AD3113 60 0.50 1982-03-01 1982-04-01000270 AD3113 70 0.75 1982-09-01 1982-10-15000270 AD3113 70 1.00 1982-10-15 1983-02-01000270 AD3113 80 1.00 1982-01-01 1982-03-01000270 AD3113 80 0.50 1982-03-01 1982-04-01000030 IF1000 10 0.50 1982-06-01 1983-01-01000130 IF1000 90 1.00 1982-01-01 1982-10-01000130 IF1000 100 0.50 1982-10-01 1983-01-01000140 IF1000 90 0.50 1982-10-01 1983-01-01

Table Content xix Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 20: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

EMP_ACT table (cont'd)

EMPNO PROJNO ACTNO EMPTIME EMSTDATE EMENDATE------ ------ ------ -------- ---------- ----------000030 IF2000 10 0.50 1982-01-01 1983-01-01000140 IF2000 100 1.00 1982-01-01 1982-03-01000140 IF2000 100 0.50 1982-03-01 1982-07-01000140 IF2000 110 0.50 1982-03-01 1982-07-01000140 IF2000 110 0.50 1982-10-01 1983-01-01000010 MA2100 10 0.50 1982-01-01 1982-11-01000110 MA2100 20 1.00 1982-01-01 1982-03-01000010 MA2110 10 1.00 1982-01-01 1983-02-01000200 MA2111 50 1.00 1982-01-01 1982-06-15000200 MA2111 60 1.00 1982-06-15 1983-02-01000220 MA2111 40 1.00 1982-01-01 1983-02-01000150 MA2112 60 1.00 1982-01-01 1982-07-15000150 MA2112 180 1.00 1982-07-15 1983-02-01000170 MA2112 60 1.00 1982-01-01 1983-06-01000170 MA2112 70 1.00 1982-06-01 1983-02-01000190 MA2112 70 1.00 1982-02-01 1982-10-01000190 MA2112 80 1.00 1982-10-01 1983-10-01000160 MA2113 60 1.00 1982-07-15 1983-02-01000170 MA2113 80 1.00 1982-01-01 1983-02-01000180 MA2113 70 1.00 1982-04-01 1982-06-15000210 MA2113 80 0.50 1982-10-01 1983-02-01000210 MA2113 180 0.50 1982-10-01 1983-02-01000050 OP1000 10 0.25 1982-01-01 1983-02-01000090 OP1010 10 1.00 1982-01-01 1983-02-01000280 OP1010 130 1.00 1982-01-01 1983-02-01000290 OP1010 130 1.00 1982-01-01 1983-02-01000300 OP1010 130 1.00 1982-01-01 1983-02-01000310 OP1010 130 1.00 1982-01-01 1983-02-01000050 OP2010 10 0.75 1982-01-01 1983-02-01000100 OP2010 10 1.00 1982-01-01 1983-02-01000320 OP2011 140 0.75 1982-01-01 1983-02-01000320 OP2011 150 0.25 1982-01-01 1983-02-01000330 OP2012 140 0.25 1982-01-01 1983-02-01000330 OP2012 160 0.75 1982-01-01 1983-02-01000340 OP2013 140 0.50 1982-01-01 1983-02-01000340 OP2013 170 0.50 1982-01-01 1983-02-01000020 PL2100 30 1.00 1982-01-01 1982-09-15

xx DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 21: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

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

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

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

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

Solutions — Follow the Problem List With Expected Results.

Table Content xxi Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 22: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

xxii DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 23: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Exercise 1. Simple SQL Queries

What This Exercise is About

This exercise provides an opportunity to write and execute basicSQL 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 SELECTstatement.Use a SELECT statement to:

− Retrieve all rows of a table

− Retrieve specific columns

− Retrieve rows based on comparisons to numeric oralphanumeric data

− Retrieve rows based on specific columns containing NULLvalues

Code SELECT statements using keywords BETWEEN, IN, LIKEand DISTINCTProduce a result table whose rows are sorted in a desiredsequence

Introduction

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

Required Materials

Student handoutSQL Reference

Exercise 1. Simple SQL Queries 1-1 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 24: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List

Problem 1

List employee number, last name, date of birth, and salary for allemployees who make more than $30,000 a year. Sequence the resultsin descending order by salary.

Problem 2

List last name, first name, and the department number for allemployees. The listing should be ordered by descending departmentnumbers. Within the same department, the last names should be sortedin descending order.

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

Problem 4List employees, by employee number, and their assigned projects, byproject number. Display only those employees with an employeenumber less than or equal to 100. List only one occurrence of duplicaterows. Sort the result rows by employee number.

(Use the EMP_ACT table.)

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

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

Problem 7List last name, salary, bonus, and commission for all employees with asalary greater than $22,000 and a bonus of $400, or for all employeeswith a bonus of $500 and a commission lower than $1,900. The listshould be ordered by last name.

Problem 8List last name, salary, bonus, and commission for all employees with asalary greater than $22,000, a bonus of $400 or $500, and a commissionless than $1,900. The list should be ordered by last name.

Problem 9Using the EMP_ACT table, for all projects that have a project numberbeginning with AD and have activities 10, 80, and 180 associated withthem, list the following:

Project numberActivity numberStarting date for activityEnding date for activity

Order the list by activity number within project number.

1-2 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 25: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 10List manager number and department number for all departments towhich a manager has been assigned.

The list should be ordered by manager number.

Problem 11List employee number, last name, salary, and bonus for all employeesthat have a bonus ranging from $800 to $1,000.

Sort the report by employee number within bonus, lowest bonus first.

Problem 12List employee number, last name, salary, and department number for allemployees in departments A00 through C01 (inclusive).

Order the results alphabetically by last name and employee number.

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

Problem 14List all departments that have a 1 as the middle character in thedepartment number.

Order the results by department number.

Problem 15List the last name, first name, middle initial, and salary of the fivehighest paid non-manager, non-president employees.

Order the results by highest salary first.

END OF LAB

Exercise 1. Simple SQL Queries 1-3 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 26: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List With Expected Results

Problem 1

List employee number, last name, date of birth, and salary for allemployees who make more than $30,000 a year. Sequence the resultsin descending order by salary.

Result

EMPNO LASTNAME BIRTHDATE SALARY---------+---------+---------+---------+--------000010 HAAS 1933-08-14 52750.00000110 LUCCHESI 1929-11-05 46500.00000020 THOMPSON 1948-02-02 41250.00000050 GEYER 1925-09-15 40175.00000030 KWAN 1941-05-11 38250.00000070 PULASKI 1953-05-26 36170.00000060 STERN 1945-07-07 32250.00

Problem 2

List last name, first name, and the department number for allemployees. The listing should be ordered by descending departmentnumbers. Within the same department, the last names should be sortedin descending order.

Result

LASTNAME FIRSTNME WORKDEPT---------+---------+---------+---------+SPENSER THEODORE E21MEHTA RAMLAL E21LEE WING E21GOUNOT JASON E21SMITH PHILIP E11SETRIGHT MAUDE E11SCHNEIDER ETHEL E11PARKER JOHN E11HENDERSON EILEEN E11GEYER JOHN E01...... ...... ...

The complete result set contains 32 rows.

1-4 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 27: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

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

Result

EDLEVEL-------

2019181716151412

Problem 4List employees, by employee number, and their assigned projects, byproject number. Display only those employees with an employeenumber less than or equal to 100. List only one occurrence of duplicaterows. Sort the result rows by employee number.

(Use the EMP_ACT table.)Result

EMPNO PROJNO---------+----000010 AD3100000010 MA2100000010 MA2110000020 PL2100000030 IF1000000030 IF2000000050 OP1000000050 OP2010000070 AD3110000090 OP1010000100 OP2010

Exercise 1. Simple SQL Queries 1-5 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 28: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

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

Result

LASTNAME SALARY BONUS---------+---------+---------+---------+-THOMPSON 41250.00 800.00GEYER 40175.00 800.00STERN 32250.00 600.00SPENSER 26150.00 500.00LUCCHESI 46500.00 900.00O'CONNELL 29250.00 600.00ADAMSON 25280.00 500.00YOSHIMURA 24680.00 500.00WALKER 20450.00 400.00BROWN 27740.00 600.00JONES 18270.00 400.00JEFFERSON 22180.00 400.00MARINO 28760.00 600.00SMITH 19180.00 400.00PARKER 15340.00 300.00SMITH 17750.00 400.00MEHTA 19950.00 400.00LEE 25370.00 500.00GOUNOT 23840.00 500.00

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

Result

LASTNAME SALARY COMM---------+---------+---------+---------+-PULASKI 36170.00 2893.00SPENSER 26150.00 2092.00PEREZ 27380.00 2190.00

1-6 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 29: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 7List last name, salary, bonus, and commission for all employees with asalary greater than $22,000 and a bonus of $400, or for all employeeswith a bonus of $500 and a commission lower than $1,900. The listshould be ordered by last name.

Result

LASTNAME SALARY BONUS COMM---------+---------+---------+---------+---------+----JEFFERSON 22180,00 400,00 1774,00PIANKA 22250,00 400,00 1780,00SCOUTTEN 21340,00 500,00 1707,00

Problem 8List last name, salary, bonus, and commission for all employees with asalary greater than $22,000, a bonus of $400 or $500, and a commissionless than $1,900. The list should be ordered by last name.

Result

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

Exercise 1. Simple SQL Queries 1-7 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 30: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 9Using the EMP_ACT table, for all projects that have a project numberbeginning with AD and have activities 10, 80, and 180 associated withthem, list the following:

Project numberActivity numberStarting date for activityEnding date for activity

Order the list by activity number within project number.Result

PROJNO ACTNO EMSTDATE EMENDATE---------+---------+---------+--------AD3100 10 1982-01-01 1982-07-01AD3110 10 1982-01-01 1983-02-01AD3111 80 1982-04-15 1982-10-15AD3111 80 1982-09-15 1983-01-01AD3111 180 1982-10-15 1983-01-01AD3112 80 1982-08-15 1982-10-15AD3112 80 1982-10-15 1982-12-01AD3112 180 1982-08-15 1983-01-01AD3113 80 1982-01-01 1982-03-01AD3113 80 1982-01-01 1982-03-01AD3113 80 1982-03-01 1982-04-15AD3113 80 1982-03-01 1982-04-01AD3113 180 1982-03-01 1982-04-15AD3113 180 1982-04-15 1982-06-01AD3113 180 1982-06-01 1982-07-01

1-8 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 31: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 10List manager number and department number for all departments towhich a manager has been assigned.

The list should be ordered by manager number.Result

MGRNO DEPTNO---------+----000010 A00000020 B01000030 C01000050 E01000060 D11000070 D21000090 E11000100 E21

Problem 11List employee number, last name, salary, and bonus for all employeesthat have a bonus ranging from $800 to $1,000.

Sort the report by employee number within bonus, lowest bonus first.Result

EMPNO LASTNAME SALARY BONUS------ --------------- ------------ ------------000020 THOMPSON 41250.00 800.00000030 KWAN 38250.00 800.00000050 GEYER 40175.00 800.00000110 LUCCHESSI 46500.00 900.00000010 HAAS 52750.00 1000.00

Exercise 1. Simple SQL Queries 1-9 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 32: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 12List employee number, last name, salary, and department number for allemployees 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 A00000030 KWAN 38250.00 C01000110 LUCCHESI 46500.00 A00000140 NICHOLLS 28420.00 C01000120 O'CONNELL 29250.00 A00000130 QUINTANA 23800.00 C01000020 THOMPSON 41250.00 B01

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

Result

PROJNO PROJNAME---------+---------+---------+---------OP1000 OPERATION SUPPORTOP2010 SYSTEMS SUPPORTOP2011 SCP SYSTEMS SUPPORTOP2012 APPLICATIONS SUPPORTOP2013 DB/DC SUPPORT

Problem 14List all departments that have a 1 as the middle character in thedepartment number.

Order the results by department number.Result

DEPTNO DEPTNAME---------+---------+---------+---------+D11 MANUFACTURING SYSTEMSE11 OPERATIONS

1-10 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 33: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 15List the last name, first name, middle initial, and salary of the fivehighest paid non-manager, non-president employees.

Order the results by highest salary first.Result

LASTNAME FIRSTNME MIDINIT SALARY--------------- ------------ ------- ------------LUCCHESSI VINCENZO G 46500.00LUTZ JENNIFER K 29840.00O'CONNELL SEAN 29250.00MARINO SALVATORE M 28760.00NICHOLLS HEATHER A 28420.00

END OF Problem List With Expected Results

Exercise 1. Simple SQL Queries 1-11 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 34: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Solution

Problem 1

SELECT EMPNO, LASTNAME, BIRTHDATE, SALARYFROM EMPLOYEEWHERE SALARY > 30000ORDER BY SALARY DESC

Problem 2

SELECT LASTNAME, FIRSTNME, WORKDEPTFROM EMPLOYEEORDER BY WORKDEPT DESC, LASTNAME DESC

Problem 3

SELECT DISTINCT EDLEVELFROM EMPLOYEEORDER BY EDLEVEL DESC

Problem 4

SELECT DISTINCT EMPNO, PROJNOFROM EMP_ACTWHERE EMPNO <= '000100'ORDER BY EMPNO

Problem 5

SELECT LASTNAME, SALARY, BONUSFROM EMPLOYEEWHERE SEX = 'M'

Problem 6

SELECT LASTNAME, SALARY, COMMFROM EMPLOYEEWHERE HIREDATE >= '1980-01-01'

AND SALARY > 20000

Problem 7

SELECT LASTNAME, SALARY, BONUS, COMMFROM EMPLOYEEWHERE SALARY > 22000 AND BONUS = 400

OR BONUS = 500 AND COMM < 1900ORDER BY LASTNAME

1-12 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 35: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 8

SELECT LASTNAME, SALARY, BONUS, COMMFROM EMPLOYEEWHERE SALARY > 22000

AND ( BONUS = 400 OR BONUS = 500 )AND COMM < 1900

ORDER BY LASTNAME

Problem 9

SELECT PROJNO, ACTNO, EMSTDATE, EMENDATEFROM EMP_ACTWHERE ACTNO IN (10, 80, 180)

AND PROJNO LIKE 'AD%'ORDER BY PROJNO, ACTNO

Problem 10

SELECT MGRNO, DEPTNOFROM DEPARTMENTWHERE MGRNO IS NOT NULLORDER BY MGRNO

Problem 11

SELECT EMPNO, LASTNAME, SALARY, BONUSFROM EMPLOYEEWHERE BONUS BETWEEN 800 AND 1000ORDER BY BONUS, EMPNO

Problem 12

SELECT EMPNO, LASTNAME, SALARY, WORKDEPTFROM EMPLOYEEWHERE WORKDEPT BETWEEN 'A00' AND 'C01'ORDER BY LASTNAME, EMPNO

Problem 13

SELECT PROJNO, PROJNAMEFROM PROJECTWHERE PROJNAME LIKE '%SUPPORT%'ORDER BY PROJNO

Problem 14

SELECT DEPTNO, DEPTNAMEFROM DEPARTMENTWHERE DEPTNO LIKE '_1_'ORDER BY DEPTNO

Exercise 1. Simple SQL Queries 1-13 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 36: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 15

SELECT LASTNAME, FIRSTNME, MIDINIT, SALARYFROM EMPLOYEEWHERE JOB NOT IN ('PRES', 'MANAGER')ORDER BY SALARY DESCFETCH FIRST 5 ROWS ONLY

END OF SOLUTION

1-14 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 37: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Exercise 2. Retrieving Data from Multiple Tables

What This Exercise is About

This exercise provides an opportunity to write queries that retrievedata 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 thecolumn names and descriptions for each table.

Required Materials

Student handoutSQL Reference

Exercise 2. Multiple Tables 2-1 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 38: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List

Problem 1Produce a report that lists employees' last names, first names, anddepartment names. Sequence the report on first name within last name,within department name.

Problem 2Modify the previous query to include job. Also, list data for onlydepartments between A02 and D22, and exclude managers from the list.Sequence the report on first name within last name, within job, withindepartment name.

Problem 3List the name of each department and the lastname and first name of itsmanager. Sequence the list by department name. Use the EMPNO andMGRNO columns to relate the two tables. Sequence the result rows bydepartment name.

Problem 4Try the following: modify the previous query using WORKDEPT andDEPTNO as the join predicate. Include a local predicate that looks forpeople whose job is manager.

Are the results from both queries the same? __________________

Why?

______________________________________________________________________________________________________________________________________________________________________________________________________________

Problem 5For all projects that have a project number beginning with AD, listproject number, project name, and activity number. List identical rowsonce. Order the list by project number and then by activity number.

Problem 6Which employees are assigned to project number AD3113? Listemployee number, last name, and project number. Order the list byemployee number and then by project number. List only oneoccurrence of duplicate result rows.

2-2 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 39: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 7Which activities began on October 1, 1982? For each of these activities,list the employee number of the person performing the activity, theproject number,project name, activity number, and starting date of theactivity. Order the list by project number, then by employee number,and then by activity number.

Problem 8Display department number, last name, project name, and activitynumber for activities performed by the employees in department A00.

Sequence the results first by project name and then by activity number.

Problem 9List department number, last name, project name, and activity numberfor those employees in work departments A00 through C01. Suppressidentical rows.

Sort the list by department number, last name, and activity number.

Problem 10The second line manager needs a list of activities which began onOctober 15, 1982 or thereafter.

For these activities, list the activity number, the manager number of themanager of the department assigned to the project, the starting date forthe activity, the project number, and the last name of the employeeperforming the activity.

The list should be ordered by the activity number and then by theactivity start date.

Problem 11Which employees in department A00 were hired before their manager?

List department number, the manager's last name, the employee's lastname, and the hiring dates of both the manager and the employee.

Order the list by the employee's last name.

END OF LAB

Exercise 2. Multiple Tables 2-3 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 40: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List With Expected Results

Problem 1Produce a report that lists employees' last names, first names, anddepartment names. Sequence the report on first name within last name,within department name.

RESULT

LASTNAME FIRSTNME DEPTNAME--------------- ------------ -----------------------------JEFFERSON JAMES ADMINISTRATION SYSTEMSJOHNSON SYBIL ADMINISTRATION SYSTEMSMARINO SALVATORE ADMINISTRATION SYSTEMSPEREZ MARIA ADMINISTRATION SYSTEMSPULASKI EVA ADMINISTRATION SYSTEMSSMITH DANIEL ADMINISTRATION SYSTEMSKWAN SALLY INFORMATION CENTERNICHOLLS HEATHER INFORMATION CENTERQUINTANA DOLORES INFORMATION CENTERADAMSON BRUCE MANUFACTURING SYSTEMSBROWN DAVID MANUFACTURING SYSTEMSJONES WILLIAM MANUFACTURING SYSTEMSLUTZ JENNIFER MANUFACTURING SYSTEMSPIANKA ELIZABETH MANUFACTURING SYSTEMSSCOUTTEN MARILYN MANUFACTURING SYSTEMSSTERN IRVING MANUFACTURING SYSTEMSWALKER JAMES MANUFACTURING SYSTEMSYOSHIMURA MASATOSHI MANUFACTURING SYSTEMSHENDERSON EILEEN OPERATIONSPARKER JOHN OPERATIONSSCHNEIDER ETHEL OPERATIONSSETRIGHT MAUDE OPERATIONSSMITH PHILIP OPERATIONSTHOMPSON MICHAEL PLANNINGGOUNOT JASON SOFTWARE SUPPORTLEE WING SOFTWARE SUPPORTMEHTA RAMLAL SOFTWARE SUPPORTSPENSER THEODORE SOFTWARE SUPPORTHAAS CHRISTINE SPIFFY COMPUTER SERVICE DIV.LUCCHESSI VINCENZO SPIFFY COMPUTER SERVICE DIV.O'CONNELL SEAN SPIFFY COMPUTER SERVICE DIV.GEYER JOHN SUPPORT SERVICES

2-4 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 41: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 2Modify the previous query to include job. Also, list data for onlydepartments between A02 and D22, and exclude managers from the list.Sequence the report on first name within last name, within job, withindepartment name.

RESULT

LASTNAME FIRSTNME DEPTNAME JOB--------------- ------------ ----------------------------- --------JEFFERSON JAMES ADMINISTRATION SYSTEMS CLERKJOHNSON SYBIL ADMINISTRATION SYSTEMS CLERKMARINO SALVATORE ADMINISTRATION SYSTEMS CLERKPEREZ MARIA ADMINISTRATION SYSTEMS CLERKSMITH DANIEL ADMINISTRATION SYSTEMS CLERKNICHOLLS HEATHER INFORMATION CENTER ANALYSTQUINTANA DOLORES INFORMATION CENTER ANALYSTADAMSON BRUCE MANUFACTURING SYSTEMS DESIGNERBROWN DAVID MANUFACTURING SYSTEMS DESIGNERJONES WILLIAM MANUFACTURING SYSTEMS DESIGNERLUTZ JENNIFER MANUFACTURING SYSTEMS DESIGNERPIANKA ELIZABETH MANUFACTURING SYSTEMS DESIGNERSCOUTTEN MARILYN MANUFACTURING SYSTEMS DESIGNERWALKER JAMES MANUFACTURING SYSTEMS DESIGNERYOSHIMURA MASATOSHI MANUFACTURING SYSTEMS DESIGNER

Problem 3List the name of each department and the lastname and first name of itsmanager. Sequence the list by department name. Use the EMPNO andMGRNO columns to relate the two tables. Sequence the result rows bydepartment name.

RESULT

DEPTNAME LASTNAME FIRSTNME----------------------------- --------------- ------------ADMINISTRATION SYSTEMS PULASKI EVAINFORMATION CENTER KWAN SALLYMANUFACTURING SYSTEMS STERN IRVINGOPERATIONS HENDERSON EILEENPLANNING THOMPSON MICHAELSOFTWARE SUPPORT SPENSER THEODORESPIFFY COMPUTER SERVICE DIV. HAAS CHRISTINESUPPORT SERVICES GEYER JOHN

Exercise 2. Multiple Tables 2-5 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 42: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 4Try the following: modify the previous query using WORKDEPT andDEPTNO as the join predicate. Include a local predicate that looks forpeople whose job is manager.

Are the results from both queries the same? __________________

Why?

______________________________________________________________________________________________________________________________________________________________________________________________________________

RESULT

DEPTNAME LASTNAME FIRSTNME----------------------------- --------------- ------------ADMINISTRATION SYSTEMS PULASKI EVAINFORMATION CENTER KWAN SALLYMANUFACTURING SYSTEMS STERN IRVINGOPERATIONS HENDERSON EILEENPLANNING THOMPSON MICHAELSOFTWARE SUPPORT SPENSER THEODORESUPPORT SERVICES GEYER JOHN

Problem 5For all projects that have a project number beginning with AD, listproject number, project name, and activity number. List identical rowsonce. Order the list by project number and then by activity number.

Result

PROJNO PROJNAME ACTNO---------+---------+---------+---------+-AD3100 ADMIN SERVICES 10AD3110 GENERAL AD SYSTEMS 10AD3111 PAYROLL PROGRAMMING 60AD3111 PAYROLL PROGRAMMING 70AD3111 PAYROLL PROGRAMMING 80AD3111 PAYROLL PROGRAMMING 180AD3112 PERSONNEL PROGRAMMG 60AD3112 PERSONNEL PROGRAMMG 70AD3112 PERSONNEL PROGRAMMG 80AD3112 PERSONNEL PROGRAMMG 180AD3113 ACCOUNT.PROGRAMMING 60AD3113 ACCOUNT.PROGRAMMING 70AD3113 ACCOUNT.PROGRAMMING 80AD3113 ACCOUNT.PROGRAMMING 180

2-6 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 43: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 6Which employees are assigned to project number AD3113? Listemployee number, last name, and project number. Order the list byemployee number and then by project number. List only oneoccurrence of duplicate result rows.

Result

EMPNO LASTNAME PROJNO---------+---------+---------+--000260 JOHNSON AD3113000270 PEREZ AD3113

Problem 7Which activities began on October 1, 1982? For each of these activities,list the employee number of the person performing the activity, theproject number,project name, activity number, and starting date of theactivity. 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-01000140 IF1000 QUERY SERVICES 90 1982-10-01000140 IF2000 USER EDUCATION 110 1982-10-01000190 MA2112 W L ROBOT DESIGN 80 1982-10-01000210 MA2113 W L PROD CONT PROGS 80 1982-10-01000210 MA2113 W L PROD CONT PROGS 180 1982-10-01

Exercise 2. Multiple Tables 2-7 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 44: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 8Display department number, last name, project name, and activitynumber 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 10A00 HAAS W L PROGRAMMING 10A00 HAAS WELD LINE AUTOMATION 10A00 LUCCHESI WELD LINE AUTOMATION 20

Problem 9List department number, last name, project name, and activity numberfor those employees in work departments A00 through C01. Suppressidentical rows.

Sort the list by department number, last name, and activity number.Result

WORKDEPT LASTNAME PROJNAME ACTNO---------+---------+---+---------+---------+---------A00 HAAS ADMIN SERVICES 10A00 HAAS W L PROGRAMMING 10A00 HAAS WELD LINE AUTOMATION 10A00 LUCCHESI WELD LINE AUTOMATION 20B01 THOMPSON WELD LINE PLANNING 30C01 KWAN QUERY SERVICES 10C01 KWAN USER EDUCATION 10C01 NICHOLLS QUERY SERVICES 90C01 NICHOLLS USER EDUCATION 100C01 NICHOLLS USER EDUCATION 110C01 QUINTANA QUERY SERVICES 90C01 QUINTANA QUERY SERVICES 100

2-8 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 45: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 10The second line manager needs a list of activities which began onOctober 15, 1982 or thereafter.

For these activities, list the activity number, the manager number of themanager of the department assigned to the project, the starting date forthe activity, the project number, and the last name of the employeeperforming the activity.

The list should be ordered by the activity number and then by theactivity start date.

Result

ACTNO MGRNO EMSTDATE PROJNO LASTNAME--------+---------+---------+---------+------

60 000070 1982-12-01 AD3112 SMITH60 000070 1983-01-01 AD3112 SMITH70 000070 1982-10-15 AD3113 PEREZ80 000070 1982-10-15 AD3112 SMITH

180 000070 1982-10-15 AD3111 JEFFERSON

Problem 11Which employees in department A00 were hired before their manager?

List department number, the manager's last name, the employee's lastname, 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-16A00 HAAS O'CONNELL 1965-01-01 1963-12-05

END OF Problem List With Expected Results

Exercise 2. Multiple Tables 2-9 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 46: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Solution

Problem 1

SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAMEFROM EMPLOYEE E, DEPARTMENT DWHERE E.WORKDEPT = D.DEPTNOORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME

OR

SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAMEFROM EMPLOYEE E JOIN DEPARTMENT D

ON E.WORKDEPT = D.DEPTNOORDER BY D.DEPTNAME, E.LASTNAME, E.FIRSTNME

Problem 2

SELECT E.LASTNAME, E.FIRSTNME, D.DEPTNAME, E.JOBFROM EMPLOYEE E, DEPARTMENT DWHERE 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.JOBFROM EMPLOYEE E JOIN DEPARTMENT D

ON E.WORKDEPT = D.DEPTNOWHERE 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, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 47: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 3

SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNMEFROM DEPARTMENT D, EMPLOYEE EWHERE D.MGRNO = E.EMPNOORDER BY D.DEPTNAME

OR

SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNMEFROM DEPARTMENT D JOIN EMPLOYEE E

ON D.MGRNO = E.EMPNOORDER BY D.DEPTNAME

Problem 4

SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNMEFROM DEPARTMENT D, EMPLOYEE EWHERE D.DEPTNO = E.WORKDEPT

AND JOB = 'MANAGER'ORDER BY D.DEPTNAME

OR

SELECT D.DEPTNAME, E.LASTNAME, E.FIRSTNMEFROM DEPARTMENT D JOIN EMPLOYEE E

ON D.DEPTNO = E.WORKDEPTWHERE JOB = 'MANAGER'ORDER BY D.DEPTNAME

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

Exercise 2. Multiple Tables 2-11 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 48: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 5

SELECT DISTINCT A.PROJNO, PROJNAME, ACTNOFROM PROJECT P,

EMP_ACT AWHERE P.PROJNO = A.PROJNO

AND A.PROJNO LIKE 'AD%'ORDER BY A.PROJNO, ACTNO

or

SELECT DISTINCT A.PROJNO, PROJNAME, ACTNOFROM PROJECT AS P JOIN EMP_ACT AS A

ON P.PROJNO = A.PROJNOWHERE A.PROJNO LIKE 'AD%'ORDER BY A.PROJNO, ACTNO

Problem 6

SELECT DISTINCT A.EMPNO, LASTNAME, PROJNOFROM EMPLOYEE E,

EMP_ACT AWHERE A.EMPNO = E.EMPNO

AND A.PROJNO = 'AD3113'ORDER BY A.EMPNO, PROJNO

or

SELECT DISTINCT A.EMPNO,LASTNAME, PROJNOFROM EMPLOYEE AS E JOIN EMP_ACT AS A

ON E.EMPNO = A.EMPNOWHERE A.PROJNO = 'AD3113'ORDER BY A.EMPNO, PROJNO

2-12 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 49: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 7

SELECT A.EMPNO, A.PROJNO, PROJNAME, ACTNO, EMSTDATEFROM PROJECT P,

EMP_ACT AWHERE A.PROJNO = P.PROJNO

AND EMSTDATE = '1982-10-01'ORDER BY A.PROJNO, A.EMPNO, ACTNO

or

SELECT A.EMPNO, A.PROJNO, PROJNAME, ACTNO, EMSTDATEFROM PROJECT AS P JOIN EMP_ACT AS A

ON P.PROJNO = A.PROJNOWHERE A.EMSTDATE = '1982-10-01'ORDER BY A.PROJNO, A.EMPNO, ACTNO

Problem 8

SELECT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNOFROM EMPLOYEE E, PROJECT P, EMP_ACT AWHERE E.EMPNO = A.EMPNO

AND A.PROJNO = P.PROJNOAND E.WORKDEPT = 'A00'

ORDER BY P.PROJNAME, A.ACTNO

OR

SELECT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNOFROM EMPLOYEE AS E JOIN EMP_ACT AS A

ON E.EMPNO = A.EMPNOJOIN PROJECT AS P

ON A.PROJNO = P.PROJNOWHERE E.WORKDEPT = 'A00'ORDER BY P.PROJNAME, A.ACTNO

Exercise 2. Multiple Tables 2-13 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 50: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 9

SELECT DISTINCT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNOFROM EMPLOYEE E, PROJECT P, EMP_ACT AWHERE E.EMPNO = A.EMPNO

AND A.PROJNO = P.PROJNOAND E.WORKDEPT BETWEEN 'A00' AND 'C01'

ORDER BY E.WORKDEPT, E.LASTNAME, A.ACTNO

OR

SELECT DISTINCT E.WORKDEPT, E.LASTNAME, P.PROJNAME, A.ACTNOFROM EMPLOYEE E JOIN EMP_ACT A

ON E.EMPNO = A.EMPNOJOIN PROJECT P

ON A.PROJNO = P.PROJNOWHERE 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.LASTNAMEFROM DEPARTMENT D, EMPLOYEE E, PROJECT P, EMP_ACT AWHERE E.EMPNO = A.EMPNO

AND A.PROJNO = P.PROJNOAND E.WORKDEPT = D.DEPTNOAND A.EMSTDATE >= '1982-10-15'

ORDER BY A.ACTNO, A.EMSTDATE

OR

SELECT A.ACTNO, D.MGRNO, A.EMSTDATE, E.LASTNAMEFROM EMPLOYEE AS E

JOIN EMP_ACT AS AON E.EMPNO = A.EMPNO

JOIN PROJECT AS PON A.PROJNO = P.PROJNO

JOIN DEPARTMENT AS DON 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, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 51: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

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 DWHERE D.MGRNO = M.EMPNO

AND E.WORKDEPT = D.DEPTNOAND M.HIREDATE > E.HIREDATEAND 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 EJOIN DEPARTMENT AS D

ON E.WORKDEPT = D.DEPTNOJOIN EMPLOYEE AS M

ON M.EMPNO = D.MGRNOWHERE M.HIREDATE > E.HIREDATE

AND E.WORKDEPT = 'A00'ORDER BY E.LASTNAME

END OF SOLUTION

Exercise 2. Multiple Tables 2-15 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 52: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

2-16 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 53: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Exercise 3. Scalar Functions and Arithmetic

What This Exercise is About

This exercise provides an opportunity to work with scalar functionsand 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 functionsCode queries by using calculated expressions in the select listand in the WHERE clauseUse basic scalar functionsUse the CONCAT operator

Introduction

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

Required Materials

Student handoutSQL Reference

Exercise 3. Scalar Functions and Arithmetic 3-1 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 54: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List

Problem 1For employees whose salary, increased by 5 percent, is less than orequal to $20,000, list the following:

Last nameCurrent SalarySalary increased by 5 percentMonthly 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 functionto display the increased salary and monthly salary with two of the digitsto the right of the decimal point. Sort the results by annual salary.

Problem 2All employees with an education level of 18 or 20 will receive a salaryincrease 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 thegenerated columns.

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

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

They belong to department D11Their salary is more than or equal to 80 percent of $20,000Their 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. Sortthe result by salary.

Problem 4Produce a list of all employees in department D11 that have an income(sum of salary, commission, and bonus) that is greater than their salaryincreased by 10 percent.

Name the generated column INCOME.

List department number, last name, and income. Sort the result indescending 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, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 55: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 5List all departments that have no manager assigned. List departmentnumber, department name, and manager number. Replace unknownmanager numbers with the word UNKNOWN and name the columnMGRNO.

Problem 6List the project number and major project number for all projects thathave a project number beginning with MA. If the major project numberis unknown, display the text 'MAIN PROJECT.'

Name the derived column MAJOR PROJECT.

Sequence the results by PROJNO.

Problem 7List all employees who were younger than 25 when they joined thecompany.

List their employee number, last name, and age when they joined thecompany.

Name the derived column AGE.

Sort the result by age and then by employee number.

Problem 8Provide a list of all projects which ended on December 1, 1982. Displaythe year and month of the starting date and the project number. Sort theresult by project number.

Name the derived columns YEAR and MONTH.

Problem 9List the project number and duration, in weeks, of all projects that havea project number beginning with MA. The duration should be roundedand displayed with one decimal position.

Name the derived column WEEKS.

Order the list by the project number.

Problem 10For projects that have a project number beginning with MA, list theproject number, project ending date, and a modified ending dateassuming the projects will be delayed by 10 percent.

Name the column containing PRENDATE, ESTIMATED. Name thederived column EXPECTED.

Order the list by project number.

Exercise 3. Scalar Functions and Arithmetic 3-3 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 56: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

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

Since no columns from a specific table are used in this problem, youcan use any table in the FROM clause but you should indicate a WHEREcondition that derives a single result row (unique key). You may alsoselect from the SYSIBM.SYSDUMMY1 table which produces a one rowresult.

Name the derived column DAYS.

END OF LAB

3-4 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 57: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List With Expected Results

Problem 1For employees whose salary, increased by 5 percent, is less than orequal to $20,000, list the following:

Last nameCurrent SalarySalary increased by 5 percentMonthly 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 functionto display the increased salary and monthly salary with two of the digitsto 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.25SETRIGHT 15900.00 16695.00 1391.25JOHNSON 17250.00 18112.50 1509.37SMITH 17750.00 18637.50 1553.12JONES 18270.00 19183.50 1598.62

Problem 2All employees with an education level of 18 or 20 will receive a salaryincrease 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 thegenerated columns.

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

Result

LASTNAME EDLEVEL NEW-SALARY NEW-BONUS---------+---------+---------+---------+---------+--KWAN 20 39450.00 400.00NICHOLLS 18 29620.00 300.00LUTZ 18 31040.00 300.00THOMPSON 18 42450.00 400.00HAAS 18 53950.00 500.00

Exercise 3. Scalar Functions and Arithmetic 3-5 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 58: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

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

They belong to department D11Their salary is more than or equal to 80 percent of $20,000Their 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. Sortthe result by salary.

Result

WORKDEPT LASTNAME SALARY DECR-SALARY---------+---------+---------+---------+---------+--D11 JONES 18270.00 17270.00D11 WALKER 20450.00 19450.00D11 SCOUTTEN 21340.00 20340.00D11 PIANKA 22250.00 21250.00

Problem 4Produce a list of all employees in department D11 that have an income(sum of salary, commission, and bonus) that is greater than their salaryincreased by 10 percent.

Name the generated column INCOME.

List department number, last name, and income. Sort the result indescending order by income.

For this problem assume that all employees have non-null salaries,commissions, and bonuses.

Result

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

Problem 5List all departments that have no manager assigned. List departmentnumber, department name, and manager number. Replace unknownmanager numbers with the word UNKNOWN and name the columnMGRNO.

Result

DEPTNO DEPTNAME MGRNO------ ----------------------------- -------D01 DEVELOPMENT CENTER UNKNOWN

3-6 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 59: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 6List the project number and major project number for all projects thathave a project number beginning with MA. If the major project numberis unknown, display the text 'MAIN PROJECT.'

Name the derived column MAJOR PROJECT.

Sequence the results by PROJNO.Result

PROJNO MAJOR PROJECT---------+---------+-MA2100 MAIN PROJECTMA2110 MA2100MA2111 MA2110MA2112 MA2110MA2113 MA2110

Problem 7List all employees who were younger than 25 when they joined thecompany.

List their employee number, last name, and age when they joined thecompany.

Name the derived column AGE.

Sort the result by age and then by employee number.Result

EMPNO LASTNAME AGE---------+---------+---------+------000220 LUTZ 20000340 GOUNOT 20000120 O'CONNELL 21000160 PIANKA 22000190 WALKER 22000050 GEYER 23000100 SPENSER 23000150 ADAMSON 24000180 SCOUTTEN 24000200 BROWN 24

Exercise 3. Scalar Functions and Arithmetic 3-7 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 60: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 8Provide a list of all projects which ended on December 1, 1982. Displaythe year and month of the starting date and the project number. Sort theresult by project number.

Name the derived columns YEAR and MONTH.Result

YEAR MONTH PROJNO---------+---------+---------+--

1982 1 MA21111982 1 MA21121982 2 MA2113

Problem 9List the project number and duration, in weeks, of all projects that havea project number beginning with MA. The duration should be roundedand displayed with one decimal position.

Name the derived column WEEKS.

Order the list by the project number.Result

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

Problem 10For projects that have a project number beginning with MA, list theproject number, project ending date, and a modified ending dateassuming the projects will be delayed by 10 percent.

Name the column containing PRENDATE, ESTIMATED. Name thederived column EXPECTED.

Order the list by project number.Result

PROJNO ESTIMATED EXPECTED---------+---------+---------+MA2100 1983-02-01 1983-03-12MA2110 1983-02-01 1983-03-12MA2111 1982-12-01 1983-01-03MA2112 1982-12-01 1983-01-03MA2113 1982-12-01 1982-12-29

3-8 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 61: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

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

Since no columns from a specific table are used in this problem, youcan use any table in the FROM clause but you should indicate a WHEREcondition that derives a single result row (unique key). You may alsoselect from the SYSIBM.SYSDUMMY1 table which produces a one rowresult.

Name the derived column DAYS.Result

DAYS---------+-

11122

END OF Problem List With Expected Results

Exercise 3. Scalar Functions and Arithmetic 3-9 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 62: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

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 EMPLOYEEWHERE SALARY*1.05 <= 20000ORDER BY SALARY

Problem 2

SELECT LASTNAME, EDLEVEL,SALARY+1200 AS "NEW-SALARY",DECIMAL(BONUS*0.5,9,2) AS "NEW-BONUS"

FROM EMPLOYEEWHERE EDLEVEL = 18 OR EDLEVEL = 20ORDER BY EDLEVEL DESC, 3

ALTERNATIVE

SELECT LASTNAME, EDLEVEL,SALARY+1200 AS "NEW-SALARY",DECIMAL(BONUS*0.5,9,2) AS "NEW-BONUS"

FROM EMPLOYEEWHERE EDLEVEL IN (18, 20)ORDER BY EDLEVEL DESC, "NEW-BONUS"

Problem 3

SELECT WORKDEPT, LASTNAME, SALARY,SALARY-1000 AS "DECR-SALARY"

FROM EMPLOYEEWHERE SALARY BETWEEN 20000*0.80 AND 20000*1.20

AND WORKDEPT = 'D11'ORDER BY SALARY

3-10 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 63: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 4

SELECT WORKDEPT, LASTNAME, SALARY+COMM+BONUS AS INCOMEFROM EMPLOYEEWHERE SALARY+COMM+BONUS > 1.1*SALARY

AND WORKDEPT = 'D11'ORDER BY 3 DESC

ALTERNATIVE

SELECT WORKDEPT, LASTNAME, SALARY+COMM+BONUS AS INCOMEFROM EMPLOYEEWHERE SALARY+COMM+BONUS > 1.1*SALARY

AND WORKDEPT = 'D11'ORDER BY INCOME DESC

Problem 5

SELECT DEPTNO, DEPTNAME , COALESCE(MGRNO, 'UNKNOWN') AS MGRNOFROM DEPARTMENTWHERE MGRNO IS NULL

Problem 6

SELECT PROJNO,COALESCE(MAJPROJ,'MAIN PROJECT') AS "MAJOR PROJECT"

FROM PROJECTWHERE SUBSTR(PROJNO,1,2) ='MA'ORDER BY PROJNO

Problem 7

SELECT EMPNO, LASTNAME, YEAR(HIREDATE-BIRTHDATE) AS AGEFROM EMPLOYEEWHERE YEAR(HIREDATE-BIRTHDATE) < 25ORDER BY AGE, EMPNO

Problem 8

SELECT YEAR(PRSTDATE) AS YEAR, MONTH(PRSTDATE) AS MONTH, PROJNOFROM PROJECTWHERE PRENDATE = '1982-12-01'ORDER BY PROJNO

Problem 9

SELECT PROJNO,DECIMAL((DAYS(PRENDATE)-DAYS(PRSTDATE))/7.+0.05,8,1) AS WEEKS

FROM PROJECTWHERE PROJNO LIKE 'MA%'ORDER BY PROJNO

Exercise 3. Scalar Functions and Arithmetic 3-11 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 64: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 10

SELECT PROJNO, PRENDATE AS ESTIMATED,PRSTDATE + ((DAYS(PRENDATE) - DAYS(PRSTDATE))*1.1) DAYSAS EXPECTED

FROM PROJECTWHERE PROJNO LIKE 'MA%'ORDER BY PROJNO

Problem 11

SELECT DAYS('2000-01-01')-DAYS('1969-07-20') AS DAYSFROM EMPLOYEEWHERE EMPNO = '000010'

ALTERNATIVE

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

END OF SOLUTION

3-12 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 65: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Exercise 4. Column Functions and Grouping

What This Exercise is About

This exercise provides an opportunity to work with columnfunctions.

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 thecolumn names and descriptions for each table.

Required Materials

Student handoutSQL Reference

Exercise 4. Column Functions and Grouping 4-1 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 66: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List

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

Problem 2For all departments, display the department number and the number ofemployees. Name the derived column EMP_COUNT.

Problem 3Display those departments which have more than 3 employees.

Problem 4For all departments with at least one designer, display the number ofdesigners and the department number. Name the derived columnDESIGNER.

Problem 5Show the average salary for men and the average salary for women foreach department. Display the work department, the sex, the averagesalary, average bonus, average commission, and the number of peoplein 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 6Display the average bonus and average commission for all departmentswith an average bonus greater than $500 and an average commissiongreater than $2,000. Display all averages with two digits to the right ofthe decimal point. Use the column headings AVG-BONUS andAVG-COMM for the derived columns.

END OF LAB

4-2 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 67: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List With Expected Results

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

Result

WORKDEPT SUM_SALARY-------- ----------A00 128500.00B01 41250.00C01 90470.00D11 222100.00D21 150920.00E01 40175.00E11 104990.00E21 95310.00

Problem 2For all departments, display the department number and the number ofemployees. Name the derived column EMP_COUNT.

Result

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

Problem 3Display those departments which have more than 3 employees.

Result

WORKDEPT--------D11D21E11E21

Exercise 4. Column Functions and Grouping 4-3 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 68: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 4For all departments with at least one designer, display the number ofdesigners and the department number. Name the derived columnDESIGNER.

Result

DESIGNER WORKDEPT---------+---------+-

8 D11

Problem 5Show the average salary for men and the average salary for women foreach department. Display the work department, the sex, the averagesalary, average bonus, average commission, and the number of peoplein 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 2C01 F 30156.66 633.33 2412.66 3D11 F 24476.66 500.00 1958.00 3D11 M 24778.33 500.00 1981.83 6D21 F 26933.33 500.00 2154.33 3D21 M 23373.33 466.66 1869.66 3E11 F 23966.66 466.66 1917.33 3E11 M 16545.00 350.00 1323.50 2E21 M 23827.50 475.00 1906.25 4

Problem 6Display the average bonus and average commission for all departmentswith an average bonus greater than $500 and an average commissiongreater than $2,000. Display all averages with two digits to the right ofthe decimal point. Use the column headings AVG-BONUS andAVG-COMM for the derived columns.

Result

WORKDEPT AVG-BONUS AVG-COMM---------+---------+---------+---------+------A00 833.33 3426.66B01 800.00 3300.00C01 633.33 2412.66E01 800.00 3214.00

END OF Problem List With Expected Results

4-4 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 69: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Solution

Problem 1

SELECT WORKDEPT, SUM(SALARY) AS SUM_SALARYFROM EMPLOYEEGROUP BY WORKDEPT

Problem 2

SELECT WORKDEPT, COUNT(*) AS EMP_COUNTFROM EMPLOYEEGROUP BY WORKDEPT

Problem 3

SELECT WORKDEPTFROM EMPLOYEEGROUP BY WORKDEPTHAVING COUNT(*) > 3

Problem 4

SELECT COUNT(*) AS DESIGNER, WORKDEPTFROM EMPLOYEEWHERE 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 EMPLOYEEGROUP BY WORKDEPT, SEXHAVING COUNT(*) > 1

Problem 6

SELECT WORKDEPT, DECIMAL(AVG(BONUS),9,2) AS "AVG-BONUS",DECIMAL(AVG(COMM),9,2) AS "AVG-COMM"

FROM EMPLOYEEGROUP BY WORKDEPTHAVING AVG(BONUS) > 500 AND AVG(COMM) > 2000

END OF SOLUTION

Exercise 4. Column Functions and Grouping 4-5 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 70: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

4-6 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 71: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Exercise 5. UNION

What This Exercise is About

This exercise provides an opportunity to work with the UNIONkeyword.

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

Introduction

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

Required Materials

Student handoutSQL Reference

Exercise 5. UNION 5-1 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 72: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List

Problem 1List the names and salaries for the non-managers working indepartment D21 showing the effects of a 10 percent raise. Use thefollowing output as a guide. Apply and appropriate ORDER BY clause toachieve the required results. Use the column headings shown.

LASTNAME FIRSTNME WHEN SALARY--------------- ------------ -------------- --------------JEFFERSON JAMES BEFORE A RAISE 22180.000JEFFERSON JAMES AFTER A RAISE 24398.000JOHNSON SYBIL BEFORE A RAISE 17250.000JOHNSON SYBIL AFTER A RAISE 18975.000

... ... ... ...

Problem 2List the department number, employee number, and salaries of allemployees in department A00.

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

Problem 3For departments A00, B01, and C01, list the projects assigned to themand the employees in each department. The output should consist of upto three types of lines for each department as follows:

See expected results for clarification of the following instructions.

First line (one per department):

Department numberText: DEPARTMENTDepartment name

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

Department numberProject numberProject name

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

Department numberEmployee numberLast name

5-2 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 73: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 4For all projects that have a project number that begins with IF, displaythe following:

First line:

Text: PROJECTProject numberThe employee number of the employee responsible for the projectEstimated starting dateEstimated ending date

Subsequent line(s) (one per employee working on the project):

Project numberThe employee number of the employee performing the activityActivity starting dateActivity ending date

Sequence the results by the project number, then by employee number,and finally by the starting date.

END OF LAB

Exercise 5. UNION 5-3 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 74: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List With Expected Results

Problem 1List the names and salaries for the non-managers working indepartment D21 showing the effects of a 10 percent raise. Use thefollowing output as a guide. Apply and appropriate ORDER BY clause toachieve the required results. Use the column headings shown.

LASTNAME FIRSTNME WHEN SALARY--------------- ------------ -------------- --------------JEFFERSON JAMES BEFORE A RAISE 22180.000JEFFERSON JAMES AFTER A RAISE 24398.000JOHNSON SYBIL BEFORE A RAISE 17250.000JOHNSON SYBIL AFTER A RAISE 18975.000

... ... ... ...

Result

LASTNAME FIRSTNME WHEN SALARY--------------- ------------ -------------- --------------JEFFERSON JAMES BEFORE A RAISE 22180.000JEFFERSON JAMES AFTER A RAISE 24398.000JOHNSON SYBIL BEFORE A RAISE 17250.000JOHNSON SYBIL AFTER A RAISE 18975.000MARINO SALVATORE BEFORE A RAISE 28760.000MARINO SALVATORE AFTER A RAISE 31636.000PEREZ MARIA BEFORE A RAISE 27380.000PEREZ MARIA AFTER A RAISE 30118.000SMITH DANIEL BEFORE A RAISE 19180.000SMITH DANIEL AFTER A RAISE 21098.000

5-4 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 75: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 2List the department number, employee number, and salaries of allemployees in department A00.

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

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

Exercise 5. UNION 5-5 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 76: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 3For departments A00, B01, and C01, list the projects assigned to themand the employees in each department. The output should consist of upto three types of lines for each department as follows:

See expected results for clarification of the following instructions.

First line (one per department):

Department numberText: DEPARTMENTDepartment name

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

Department numberProject numberProject name

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

Department numberEmployee numberLast name

Result

DEPTNO INFO DEPTNAME---------+---------+---------+---------+---------+-----A00 DEPARTMENT SPIFFY COMPUTER SERVICE DIV. 1A00 000010 HAAS 3A00 000110 LUCCHESI 3A00 000120 O'CONNELL 3B01 DEPARTMENT PLANNING 1B01 PL2100 WELD LINE PLANNING 2B01 000020 THOMPSON 3C01 DEPARTMENT INFORMATION CENTER 1C01 IF1000 QUERY SERVICES 2C01 IF2000 USER EDUCATION 2C01 000030 KWAN 3C01 000130 QUINTANA 3C01 000140 NICHOLLS 3

5-6 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 77: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 4For all projects that have a project number that begins with IF, displaythe following:

First line:

Text: PROJECTProject numberThe employee number of the employee responsible for the projectEstimated starting dateEstimated ending date

Subsequent line(s) (one per employee working on the project):

Project numberThe employee number of the employee performing the activityActivity starting dateActivity 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-01IF1000 000130 1982-01-01 1982-10-01IF1000 000130 1982-10-01 1983-01-01IF1000 000140 1982-10-01 1983-01-01

PROJECT IF2000 000030 1982-01-01 1983-02-01IF2000 000030 1982-01-01 1983-01-01IF2000 000140 1982-01-01 1982-03-01IF2000 000140 1982-03-01 1982-07-01IF2000 000140 1982-03-01 1982-07-01IF2000 000140 1982-10-01 1983-01-01

END OF Problem List With Expected Results

Exercise 5. UNION 5-7 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 78: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Solution

Problem 1

SELECT LASTNAME, FIRSTNME, 'BEFORE A RAISE' AS WHEN, SALARYFROM EMPLOYEEWHERE WORKDEPT = 'D21'

AND JOB <> 'MANAGER'

UNION ALL

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

FROM EMPLOYEEWHERE WORKDEPT = 'D21'

AND JOB <> 'MANAGER'ORDER BY LASTNAME, FIRSTNME, WHEN DESC

Problem 2

SELECT WORKDEPT, EMPNO, SALARYFROM EMPLOYEEWHERE WORKDEPT = 'A00'

UNION ALL

SELECT WORKDEPT, 'SUM', SUM(SALARY)FROM EMPLOYEEWHERE WORKDEPT = 'A00'GROUP BY WORKDEPTORDER BY 3

5-8 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 79: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 3

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

UNION ALL

SELECT DEPTNO, PROJNO AS INFO, PROJNAME, 2FROM PROJECTWHERE DEPTNO IN ('A00' , 'B01' , 'C01')

UNION ALL

SELECT WORKDEPT AS DEPTNO, EMPNO AS INFO, LASTNAME, 3FROM EMPLOYEEWHERE WORKDEPT IN ('A00' , 'B01' , 'C01')ORDER BY DEPTNO, 4

Problem 4

SELECT 'PROJECT', PROJNO, RESPEMP, PRSTDATE, PRENDATEFROM PROJECTWHERE PROJNO LIKE 'IF%'

UNION ALL

SELECT ' ', PROJNO, EMPNO, EMSTDATE, EMENDATEFROM EMP_ACTWHERE PROJNO LIKE 'IF%'ORDER BY 2, 1 DESC, 3, 4

END OF SOLUTION

Exercise 5. UNION 5-9 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 80: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

5-10 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 81: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

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 subqueriesCode more complex subqueries using the keyword IN

Introduction

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

Required Materials

Student handoutSQL Reference

Exercise 6. Subqueries 6-1 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 82: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List

Problem 1List those employees that have a salary which is greater than or equalto 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 2List employee number and last name of all employees not assigned toany projects. This means that table EMP_ACT does not contain a rowwith their employee number.

Problem 3List project number and duration (in days) of the project with theshortest duration.

Name the derived column DAYS.

Problem 4List department number, department name, last name, and first name ofall those employees in departments that have only male employees.

Problem 5We want to do a salary analysis for people that have the same job andeducation level as the employee Stern. Show the last name, job,edlevel, the number of years they've worked as of January 1, 2000, andtheir 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, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 83: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List With Expected Results

Problem 1List those employees that have a salary which is greater than or equalto 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.00A00 000110 LUCCHESI 46500.00B01 000020 THOMPSON 41250.00C01 000030 KWAN 38250.00D21 000070 PULASKI 36170.00E01 000050 GEYER 40175.00

Problem 2List employee number and last name of all employees not assigned toany projects. This means that table EMP_ACT does not contain a rowwith their employee number.

Result

EMPNO LASTNAME---------+-------000060 STERN000120 O'CONNELL

Exercise 6. Subqueries 6-3 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 84: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 3List project number and duration (in days) of the project with theshortest duration.

Name the derived column DAYS.Result

PROJNO DAYS---------+---------PL2100 257

Problem 4List department number, department name, last name, and first name ofall those employees in departments that have only male employees.

Result

DEPTNO DEPTNAME LASTNAME FIRSTNME---------+---------+---------+---------+---------+-----B01 PLANNING THOMPSON MICHAELE01 SUPPORT SERVICES GEYER JOHNE21 SOFTWARE SUPPORT SPENSER THEODOREE21 SOFTWARE SUPPORT MEHTA RAMLALE21 SOFTWARE SUPPORT LEE WINGE21 SOFTWARE SUPPORT GOUNOT JASON

Problem 5We want to do a salary analysis for people that have the same job andeducation level as the employee Stern. Show the last name, job,edlevel, the number of years they've worked as of January 1, 2000, andtheir salary.

Name the derived column YEARS.

Sort the listing by highest salary first.Result

LASTNAME JOB EDLEVEL YEARS SALARY--------------- -------- ------- ----------- ------------GEYER MANAGER 16 50 40175.00PULASKI MANAGER 16 19 36170.00STERN MANAGER 16 26 32250.00HENDERSON MANAGER 16 29 29750.00

6-4 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 85: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

END OF Problem List With Expected Results

Exercise 6. Subqueries 6-5 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 86: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Solution

Problem 1

SELECT WORKDEPT, EMPNO, LASTNAME, SALARYFROM EMPLOYEEWHERE SALARY >=(SELECT AVG(SALARY) + 5000

FROM EMPLOYEE)ORDER BY WORKDEPT, EMPNO

Problem 2

SELECT EMPNO, LASTNAMEFROM EMPLOYEEWHERE EMPNO NOT IN (SELECT EMPNO

FROM EMP_ACT)

Problem 3

SELECT PROJNO, DAYS(PRENDATE)-DAYS(PRSTDATE) AS DAYSFROM PROJECTWHERE DAYS(PRENDATE)-DAYS(PRSTDATE) =

(SELECT MIN(DAYS(PRENDATE)-DAYS(PRSTDATE))FROM PROJECT)

Problem 4

SELECT DEPTNO, DEPTNAME, LASTNAME, FIRSTNMEFROM DEPARTMENT, EMPLOYEEWHERE DEPTNO = WORKDEPTAND DEPTNO NOT IN(SELECT WORKDEPT

FROM EMPLOYEEWHERE SEX = 'F')

Problem 5

SELECT LASTNAME, JOB, EDLEVEL,YEAR('2000-01-01' - HIREDATE) AS YEARS, SALARYFROM EMPLOYEEWHERE (JOB, EDLEVEL) IN (SELECT JOB, EDLEVEL

FROM EMPLOYEEWHERE LASTNAME = 'STERN')

ORDER BY SALARY DESC

END OF SOLUTION

6-6 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 87: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Exercise 7. Maintaining Data

What This Exercise is About

This exercise provides an opportunity to code SQL statements inorder 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 thecolumn names and descriptions for each table.

Required Materials

Student handoutSQL Reference

Exercise 7. Maintaining Data 7-1 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 88: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List

Feel free to run a SELECT * FROM TESTEMP between problems to verify the successof your previous queries.

Problem 1Create table TESTEMP with the following columns:

EMPNO

LASTNAME

WORKDEPT

HIREDATE

SALARY

BONUS

The data types and null characteristics for these columns should be thesame as the corresponding columns in the EMPLOYEE table describedat the beginning of the lab exercises. For the OS/390 environment, usethe following IN clause at the end of your statement: IN DBCF12xx.TSxx(where xx is your assigned lab number).

Problem 2Mr. Smith, Ms Baker, and Ms Thomas joined the company. Their data isas follows:

Employee numbers: 000111, 000222, 000333Last name: SMITH, BAKER, THOMASDepartment number: C01, A00, D11Date hired: June 6, 2000Salary: $25,000, $28,000, $33,000Bonus: $0, NULL, $0

Add the new employees to the TESTEMP table.

Problem 3Insert data into the TESTEMP table by copying the appropriate columnsin the EMPLOYEE table for those employees that have an employeenumber less than or equal to 50.

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

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

Problem 6After a short time Mr. Smith leaves the company. Make the appropriatechanges.

7-2 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 89: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 7Insert data into the TESTEMP table by copying the columns in theEMPLOYEE table where the employees have an employee numbergreater than 50.

Problem 8Make the appropriate changes for the following situation:

Theodore Spenser is assigned to department E01.

Problem 9Mrs. Brown joined the company.

For Mrs. Brown enter the following data:

Employee number: 360Last name: BROWNDepartment number: D01Date hired: Date when data is enteredSalary: $45,000Bonus: unknown

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

END OF LAB

Exercise 7. Maintaining Data 7-3 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 90: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem List With Expected Results

Problem 1Create table TESTEMP with the following columns:

EMPNO

LASTNAME

WORKDEPT

HIREDATE

SALARY

BONUS

The data types and null characteristics for these columns should be thesame as the corresponding columns in the EMPLOYEE table describedat the beginning of the lab exercises. For the OS/390 environment, usethe following IN clause at the end of your statement: IN DBCF12xx.TSxx(where xx is your assigned lab number).

ResultIf running from SPUFI:

---------+---------+---------+---------+---------+--------DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0---------+---------+---------+---------+---------+--------DSNE617I COMMIT PERFORMED, SQLCODE IS 0DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0

If running from QMF:

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

Problem 2Mr. Smith, Ms Baker, and Ms Thomas joined the company. Their data isas follows:

Employee numbers: 000111, 000222, 000333Last name: SMITH, BAKER, THOMASDepartment number: C01, A00, D11Date hired: June 6, 2000Salary: $25,000, $28,000, $33,000Bonus: $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, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 91: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

If rows were inserted one at a time, the following messageshould appear after each INSERT statement;---------+---------+---------+---------+---------+---------+DSNE615I NUMBER OF ROWS AFFECTED IS 1DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0---------+---------+---------+---------+---------+---------+

If all three rows were inserted through a single INSERT statementthen a message similar to the following should be observed:

---------+---------+---------+---------+---------+---------+DSNE615I NUMBER OF ROWS AFFECTED IS 3DSNE616I 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 messageshould be received for each row inserted.

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

Problem 3Insert data into the TESTEMP table by copying the appropriate columnsin the EMPLOYEE table for those employees that have an employeenumber less than or equal to 50.

Result

In SPUFI:

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

In QMF:

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

Exercise 7. Maintaining Data 7-5 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 92: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

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

Result

In SPUFI:

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

In QMF:

OK, number of rows modified in the database: 1COMMAND ===>

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

Result

In SPUFI:

---------+---------+---------+---------+---------+---------+-DSNE615I NUMBER OF ROWS AFFECTED IS 2DSNE616I 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, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 93: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 6After a short time Mr. Smith leaves the company. Make the appropriatechanges.

Result

In SPUFI:

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

In QMF:

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

Problem 7Insert data into the TESTEMP table by copying the columns in theEMPLOYEE table where the employees have an employee numbergreater than 50.

Result

In SPUFI:

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

In QMF:

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

Exercise 7. Maintaining Data 7-7 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 94: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 8Make the appropriate changes for the following situation:

Theodore Spenser is assigned to department E01.

Result

In SPUFI:

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

In QMF:

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

Problem 9Mrs. Brown joined the company.

For Mrs. Brown enter the following data:

Employee number: 360Last name: BROWNDepartment number: D01Date hired: Date when data is enteredSalary: $45,000Bonus: unknown

Result

In SPUFI:

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

In QMF:

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

Problem 10Delete 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, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 95: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Result

In SPUFI, after deleting the rows:

DSNE615I NUMBER OF ROWS AFFECTED IS 35DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0---------+---------+---------+---------+---------+---------+---------+--SELECT * FROM TESTEMP;---------+---------+---------+---------+---------+---------+---------+--EMPNO LASTNAME WORKDEPT HIREDATE SALARY BONUS---------+---------+---------+---------+---------+---------+---------+--DSNE610I NUMBER OF ROWS DISPLAYED IS 0DSNE616I 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 NAMEDSNT418I 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)

Exercise 7. Maintaining Data 7-9 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 96: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

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 2Version 7 INSERT syntax follows:

INSERT INTO TESTEMPVALUES ('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 BONUScolumn in the column list, nor is its value specified in the VALUESclause. As a result, theBONUS 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 INSERTstatement (only data for one row allowed per VALUES clause) as shownbelow:

7-10 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 97: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

INSERT INTO TESTEMPVALUES ('000111', 'SMITH', 'C01', '06/06/2000', 25000, 0);

INSERT INTO TESTEMPVALUES ('000222', 'BAKER', 'A00', '06/06/2000', 28000, NULL);

INSERT INTO TESTEMPVALUES ('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, BONUSFROM EMPLOYEEWHERE EMPNO <= '000050'

ALTERNATIVE

INSERT INTO TESTEMPSELECT EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUSFROM EMPLOYEEWHERE EMPNO <= '000050'

Rows inserted: 4

Problem 4

UPDATE TESTEMPSET BONUS = 500

WHERE EMPNO = '000111'

Rows updated: 1

Problem 5

UPDATE TESTEMPSET SALARY = SALARY + 1000

WHERE WORKDEPT = 'C01'

Rows updated: 2

Exercise 7. Maintaining Data 7-11 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 98: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 6

DELETE FROM TESTEMPWHERE EMPNO = '000111'

Rows deleted: 1

Problem 7

INSERT INTO TESTEMP(EMPNO,LASTNAME,WORKDEPT,HIREDATE,SALARY,BONUS)SELECT EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUSFROM EMPLOYEEWHERE EMPNO > '000050'

ALTERNATIVE

INSERT INTO TESTEMPSELECT EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY, BONUSFROM EMPLOYEEWHERE EMPNO > '000050'

Rows inserted: 28

Problem 8

UPDATE TESTEMPSET WORKDEPT = 'E01'

WHERE EMPNO = '000100'

Rows updated: 1

Problem 9

INSERT INTO TESTEMPVALUES ('000360', 'BROWN ', 'D01', CURRENT DATE, 45000, NULL )

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

or

INSERT INTO TESTEMP(EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY )

VALUES ('000360', 'BROWN ', 'D01', CURRENT DATE, 45000 )

-- Column names and data values have a one to one correspondence,-- therefore, the first value with go into the first column named,-- the second value will go into the second column named, etc.-- Columns receiving their default values may be omitted from-- this syntax.

7-12 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 99: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

Problem 10

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

END OF SOLUTION

Exercise 7. Maintaining Data 7-13 Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 100: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2

Student Exercises

7-14 DB2 SQL Workshop Copyright IBM Corp. 1999, 2000Course materials may not be reproduced in whole or in part

without the prior written permission of IBM.

Page 101: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2
Page 102: Student Exercises - pudn.comread.pudn.com/downloads166/doc/765872/ibm db2techst...This lab guide provides the informationnecessary to complete the required and optional labs for theDB2