Anda di halaman 1dari 7

INF3707/202/2/2014

Tutorial Letter 202/2/2014


Database Design and Implementation


Semester 1

School of Computing
This tutorial letter contains important
information about your module.








Bar code
INF3707

2

3. Assignment 2 Solutions
Question 1 (Lesson 4)
a. The Human Resources department needs a report that displays the job ID and the average monthly
salary for each job that has an average payroll that is between 5000 and 10000. The report should not
include clerks. Sort the list in descending order of average salary. Sample results are shown below.
[6]
Solution
sel ect j ob_i d, avg( sal ar y) " Aver age sal ar y"
f r omempl oyees
wher e j ob_i d not l i ke ' %CLER%'
gr oup by j ob_i d
havi ng avg( sal ar y) bet ween 5000 and 10000
or der by avg( sal ar y) desc

b. Create a query that displays the minimum, maximum, and total salary for each job type and the date
the query was run. Sample results are shown below. [4]
Solution
sel ect j ob_i d,
mi n( sal ar y) Mi ni mum,
max( sal ar y) Maxi mum,
sum( sal ar y) Sum,
sysdat e Dat e
f r omempl oyees
gr oup by j ob_i d

Question 2 (Lesson 6)
The human Resources department needs to find the names and hire dates for all employees who were hired
before 29 J anuary 2000, along with their managers names. Sample results are shown in the table below.
[6]
Solution
sel ect w. l ast _name " Wor ker Name" ,
w. hi r e_dat e " Hi r e Dat e" ,
m. l ast _name " Manager Name"
f r omempl oyees w j oi n empl oyees m
on w. manager _i d = m. empl oyee_i d
wher e w. hi r e_dat e <= ' 29- J AN- 2000'








INF3707/201



3

Question 3 (Lesson 6)
The Human Resources department requires you to find details of employees who earn salary that is more
than the average salary for each department. Sample results are shown in the table below.
[6]
Solution
sel ect l ast _name, sal ar y, depar t ment _i d
f r omempl oyees
wher e sal ar y >= any ( sel ect r ound( avg( sal ar y) , 2)
f r omempl oyees
gr oup by depar t ment _i d)

//alternative answer

sel ect a. l ast _name, a. depar t ment _i d f r omempl oyees a
wher e sal ar y > ( sel ect avg( b. sal ar y) f r omempl oyees b
wher e b. depar t ment _i d = a. depar t ment _i d
gr oup by b. depar t ment _i d)
or der by a. sal ar y desc

Question 4 (Lesson 7)

a. The job history table keeps record of the employees that filled other positions (job_ids) within the
organization. Use one of the set operators to list the employee information (name and last name) for
all those employees that have never filled any other position within the organization .The report must
list the employee_id and employee last name and first name. Order the list by the employee_id.
[4]

b. Use one of the set operators to list the names of all the employees that are in the Shipping
Department and reports to Steven King. Sort the report alphabetically according to the last name of
the employee. [4]

Solution
a.
sel ect empl oyee_i d, l ast _name, f i r st _name f r omempl oyees
MI NUS
sel ect empl oyee_i d, TO_CHAR( NULL) , TO_CHAR( NULL) f r om
j ob_hi st or y
or der by empl oyee_i d;

// alternative
sel ect empl oyee_i d, l ast _name, f i r st _name f r omempl oyees
wher e empl oyee_i d i n ( sel ect empl oyee_i d f r omempl oyees
MI NUS
sel ect di st i nct empl oyee_i d f r omj ob_hi st or y)
or der by 1 asc

b.
sel ect l ast _name, f i r st _name f r omempl oyees wher e manager _i d =
100
i nt er sect

4

sel ect l ast _name, f i r st _name f r omempl oyees wher e depar t ment _i d =
90
or der by l ast _name;

Question 5 (lesson 8)

a. Run the statement in the lab_08_01.sql scripts to build the MY_EMPLOYEE table to be used for the
lab. [2]
b. Describe the structure of the table to identify the column names. [2]
c. Write an insert statement in a dynamic reusable script file to load rows into the MY_EMPLOYEE
table. [2]

Solution
a. run the script
b. describe My_EMPLOEE
c.
i nser t i nt o my_empl oyee
val ues( : i d, : f i r st _name, : l ast _name, : user i d, : sal r y)

Question 6(Lesson 9)
Create the EMPLOYEES_2 table based on the structure of the EMPLOYEES table. Include only the
employees_id, initial of first name, last name, the last four digits of the phone number, salary dived by 2,
and department id. Name the columns in the new table as ID, INITIAL, SURNAME, PHONE
EXTENSION, SALARY, DEPRTMENT. [8]

Solution
cr eat e t abl e empl oyees_2 as
sel ect empl oyee_i d I D,
subst r ( f i r st _name, 1, 1) I ni t i al ,
l ast _name Sur name,
subst r ( phone_number , 9, 12) " Phone Ext ensi on" ,
( sal ar y/ 2) Sal ar y,
depar t ment _i d Depar t ment
f r omempl oyees

Question 7(Lesson 10)
a. Create the EMPLOYEES_3 table based on the structure of the EMPLOYEES table. Include only the
employees_id, first name and last name. Delete all the data from the new table that you have just
created if there is any. Check if all the data has been deleted from the table. [3]

b. Provide the SQL to create a sequence EMP_SEQ_01. This sequence will be used to insert data into
the EMPLOYEES_3 table. The sequence should start at 2010 and have a maximum value of 2999.
Have your sequence increment by 2. [4]
c. Provide the SQL to dynamically insert values into the EMPLOYEES_3 table. Remember to use the
sequence you have created in the previous question. [2]

Solution

a.
i . cr eat e t abl e empl oyee_3 as
sel ect empl oyee_i d, f i r st _name, l ast _name
f r omempl oyees

INF3707/201



5

i i . del et e f r omempl oyee_3

//alternative answer
t r uncat e t abl e empl oyee_3

i i i . sel ect * f r omempl oyee_3

//alternative answer
Sel ect count ( *) f r omempl oyees_3

b. cr eat e sequence empl oyee_seq_03
i ncr ement by 2
st ar t wi t h 2010
maxval ue 2999
nocache
nocycl e

c. i nser t i nt o empl oyee_03
val ues ( empl oyee_seq_01. next val , : f i r st _name, : l ast _name)

Question 8 (Lesson 11)
Provide the SQL, that will prompt the user for the name of the table. This SQL will then provide a report
that will list the column names, data types, data types lengths, data precision, indicate whether NULLS are
allowed for this specific table. Provide suitable aliases / headings for the columns. Keep in mind that the
table name must be in capital letters use a suitable converter to convert the text that the user enter into
capital letters. [5]

Solution
sel ect col umn_name COLNAME, dat a_t ype TYPE, dat a_l engt h LENGTH,
dat a_pr eci si on PRECI SI ON, NULLABLE
f r omuser _t ab_col umns
wher e t abl e_name = upper ( : TABLENAME) ;


Question 9 (Lesson 1 Fundamentals 11)
J ohn is a new employee who works as an Application Developer who has recently joined your company. As
a DBA create a user name and password for J ohn to have access to your oracle database, initialize his
password to his name. J ohns role is a developer and has privileges to create session, create table, create
sequence and create view. Confirm that the role has been created by querying the data dictionary. [10]

Solution

cr eat e user J ohn
i dent i f i ed by j ohn;

cr eat e r ol e devel oper ;

gr ant cr eat e sessi on, cr eat e t abl e, cr eat e sequence, cr eat e vi ew
t o devel oper ;


6

gr ant devel oper t o J ohn;

sel ect * f r omdba_r ol es
wher e r ol e = upper ( ' devel oper ' ) ;

Question 10 (Lesson 3 Fundamentals 11)
a. Provide the SQL that will create the table EMPDATA based on the structure of the EMPLOYEES
and DEPARTMENTS tables. Include only the EMPLOYEE_ID, FIRST_NAME, LAST_NAME,
SALARY and DEPARTMENT_NAME columns. Name the columns in the new table as ID, NAME
(concatenate the FIRST_NAME and LAST_NAME), SALARY, DEPTNAME. (5)
b. Provide the SQL that will alter the table EMPDATA and add a PRIMARY KEY on the ID column.
(2)
c. Provide the SQL that will display the object names and types from the USER_OBJ ECTS data
dictionary view for the EMPLOYEES and EMPDATA tables. (4)
d. Provide the SQL that will drop EMPDATA in such a way that it cannot be restored. (1)

Solution
a.
cr eat e t abl e EMPDATA as
sel ect e. empl oyee_i d I D, e. f i r st _name | | ' ' | | e. l ast _name NAME,
e. sal ar y SALARY, d. depar t ment _name
f r omempl oyees e, depar t ment s d
wher e e. depar t ment _i d = d. depar t ment _i d;

b.
al t er t abl e EMPDATA
add const r ai nt empdat e_i d PRI MARY KEY ( I D) ;

c.
sel ect obj ect _name, obj ect _t ype
f r omuser _obj ect s
wher e obj ect _name i n ( ' EMPDATA' , ' EMPLOYEES' ) ;
d.
dr op t abl e empdat a pur ge;

Question 11(Lesson 4 Fundamentals 11)
Write a query to display the following for those employees whose manageID is less than 120:
Manager ID
J ob and total salaries for every job for employees who report to the same manager
Total salary for those mangers
Cross-tabulation values to display the total salary for every job, irrespective of the manager
Total salary irrespective of all job titles (10)
Solution

SELECT manager _i d, j ob_i d, sum( sal ar y)
FROM empl oyees
WHERE manager _i d < 120
GROUP BY ROLLUP( manager _i d, j ob_i d) ;

Question 12 (lesson 6 Fundamentals 11)
INF3707/201



7

Provide the SQL, which will dynamically request an employee id. The report will then display the details of
all the employees that are managed by the same manager as the employee_id that was read and work in the
same department. Use a non-pair wise comparison sub query. (10)

Solution
sel ect empl oyee_i d, manager _i d, depar t ment _i d
f r omempl oyees
wher e manager _i d I N
( sel ect manager _i d f r omempl oyees
wher e empl oyee_i d =: empi d1)
AND depar t ment _i d I N
( sel ect depar t ment _i d f r omempl oyees
wher e empl oyee_i d = : empi d1)
AND empl oyee_i d <> : empi d1






2014

Anda mungkin juga menyukai