The University of Texas at Austin CE 395 R 2 - Project Information Management Systems Spring 2006 SQL Exercises
The University of Texas at Austin CE 395 R 2 - Project Information Management Systems Spring 2006 SQL Exercises
The University of Texas at Austin CE 395 R 2 - Project Information Management Systems Spring 2006 SQL Exercises
SQL Exercises
Given the Ch06_Review database’s structure and contents shown in Figure Q6.1 (available
ate the last page), use SQL commands to answer questions 1-25.
1. Write the SQL code that will create the table structure for a table named EMP_1. This
table is a subset of the EMPLOYEE table. The basic EMP_1 table structure is
summarized in Table Q6.1. (Note that the JOB_CODE is the FK to JOB.)
INSERT INTO EMP_1 VALUES (‘101’, ‘News’, ‘John’, ‘G’, ’08-Nov-98’, ‘502’);
INSERT INTO EMP_1 VALUES (‘102’, ‘Senior’, ‘David’, ‘H’, ’12-Jul-87’, ‘501’);
3. Assuming that the data shown in the EMP_1 table have been entered, write the SQL
code that will list all attributes for a job code of 502.
SELECT *
FROM EMP_1
WHERE JOB_CODE = ‘502’;
4. Write the SQL code that will save the changes made to the EMP_1 table.
COMMIT;
5. Write the SQL code to change the job code to 501 for the person whose personnel
number is 106. After you have completed the task, examine the results, and then reset
the job code to its original value.
UPDATE EMP_1
SET JOB_CODE = ‘501’
WHERE EMP_NUM = ‘106’;
To reset, use
ROLLBACK;
6. Write the SQL code to delete the row for the person named William Smithfield, who
was hired on June 22, 2002 and whose job code classification is 500. (Hint: Use logical
operators to include all the information given in this problem.)
7. Write the SQL code that will restore the data to its original status; that is, the table
should contain the data that existed before you made the changes in Questions 5 and 6.
ROLLBACK;
8. Write the SQL code to create a copy of EMP_1, naming the copy EMP_2. Then write
the SQL code that will add the attributes EMP_PCT and PROJ_NUM to its structure.
The EMP_PCT is the bonus percentage to be paid to each employee. The new attribute
characteristics are shown next:
EMP_PCT NUMBER(4,2)
PROJ_NUM CHAR(3)
(Note: If your SQL implementation allows it, you may use DECIMAL(4,2), rather than
NUMBER(4,2).)
There are two way to get this job done. The two possible solutions are shown next.
Solution A:
9. Write the SQL code to enter an EMP_PCT value of 3.85 for the person whose employee
number (EMP_NUM) is 103. Next, enter the remaining EMP_PCT values shown in
Figure Q6.9:
UPDATE EMP_2
SET EMP_PCT = 3.85
WHERE EMP_NUM = '103';
To enter the remaining EMP_PCT values, use the following SQL statements:
UPDATE EMP_2
SET EMP_PCT = 5.00
WHERE EMP_NUM = ‘101’;
UPDATE EMP_2
SET EMP_PCT = 8.00
WHERE EMP_NUM = ‘102’;
UPDATE EMP_2
SET PROJ_NUM = '18'
WHERE JOB_CODE = '500';
11. Using a single command sequence, write the SQL code that will enter the project
number (PROJ_NUM) = 25 for all employees whose job classification (JOB_CODE) is
502 or higher. When you are done with questions 10 and 11, the EMP_2 table will
contain the data shown in Figure Q6.11:
(You may assume that the table has been saved again at this point!)
UPDATE EMP_2
SET PROJ_NUM = '25'
WHERE JOB_CODE > = '502'
12. Write the SQL code that will enter a PROJ_NUM of 14 for those employees who were
hired before January 1, 1992 and whose job code is at least 501. (You may assume that
the table will be restored to the condition it was in following Question 11.)
UPDATE EMP_2
SET PROJ_NUM = '14'
WHERE EMP_HIREDATE <= ' 01-Jan-92'
AND JOB_CODE >= '501';
13. Write the two SQL command sequences required to:
There are many ways to accomplish both tasks. We are illustrating the shortest way to do the
job next.
The SQL code shown in problem 13b contains the solution for problem 13a.
14. Write the SQL command that will delete the newly created TEMP_1 table from the
database.
15. Write the SQL code required to list all employees whose last names start with ‘Smith’.
In other words, the rows for both Smith and Smithfield should be included in the
listing.
SELECT *
FROM EMP_2
WHERE EMP_LNAME LIKE 'Smith%';
16. Using the EMPLOYEE, JOB, and PROJECT tables in the Ch06_Review database (see
Figure Q6.1), write the SQL code that will produce the results shown in Figure Q6.16.
17. Write the SQL code that will produce a virtual table named REP_1, containing the
same information that was shown in Question 16.
18. Write the SQL code to find the average bonus percentage in the EMP_2 table you
created in question 8.
SELECT AVG(EMP_PCT)
FROM EMP_2;
19. Write the SQL code that will produce a listing for the data in the EMP_2 table in
ascending order by the bonus percentage.
SELECT *
FROM EMP_2
ORDER BY EMP_PCT;
20. Write the SQL code that will list only the different project numbers found in the
EMP_2 table.
21. Write the SQL code to calculate the ASSIGN_CHARGE values in the ASSIGNMENT
table in the Ch06_Review database. (See Figure Q6.1.) Note that ASSIGN_CHARGE is
a derived attribute that is calculated by multiplying the ASSIGN_CHG_HR and the
ASSIGN_HOURS.
UPDATE ASSIGNMENT
SET ASSIGN_CHARGE = ASSIGN_CHG_HR * ASSIGN_HOURS;
22. Using the data in the ASSIGNMENT table, write the SQL code that will, for each
employee, yield the total number of hours worked and the total charges stemming from
those hours worked. The results of running this query are shown in Figure Q6.22.
SELECT ASSIGNMENT.PROJ_NUM,
Sum(ASSIGNMENT.ASSIGN_HOURS) AS SumOfASSIGN_HOURS,
Sum(ASSIGNMENT.ASSIGN_CHARGE) AS
SumOfASSIGN_CHARGE
FROM ASSIGNMENT
GROUP BY ASSIGNMENT.PROJ_NUM
24. Write the SQL code to generate the total hours worked and the total. The results are
shown in Figure Q6.24.