Sunteți pe pagina 1din 17

Universitas Semarang

Additional Practices

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

These exercises can be used for extra practice after you have discussed the following topics:
basic SQL SELECT statement and SQL functions.
1. The HR department needs to find data for all the clerks who were hired after 1997.

2. The HR department needs a report of employees who earn commission. Show the last name,
job, salary, and commission of these employees. Sort the data by salary in descending order.

ce
i
l
le

to
e
ns

3. For budgeting purposes, the HR department needs a report on projected raises. The report
should display those employees who have no commission but who have a 10% raise in salary
(round off the salaries).

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
e e th
l
c
us
ora

9
0
0
40

1
4
2
(g

Oracle Database 10g: SQL Fundamentals I Additional Practices - 2

Universitas Semarang

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

Additional Practices (continued)


4. Create a report of employees and their duration of employment. Show the last names of all
employees together with the number of years and the number of completed months that they
have been employed. Order the report by the duration of their employment. The employee
who has been employed the longest should appear at the top of the list.

5. Show those employees who have a last name starting with the letters J, K, L, or M.

ce
i
l
le

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
a anddindicate
Aall employees
en with the words Yes or No whether
R
m
6. Create a report that displays
s
T
u
uthe DECODE
St expression in your query.
CI ftiUse
they receive a commission.
k
s
i
e e th
l
c
ra
us
o
9@
0
0
40
1
1
4

(g2

Oracle Database 10g: SQL Fundamentals I Additional Practices - 3

to
e
ns

Universitas Semarang

Additional Practices (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

These exercises can be used for extra practice after you have discussed the following topics:
basic SQL SELECT statement, SQL functions, joins, and group functions.
7. Create a report that displays the department name, location, name, job title, and salary of
those employees who work in a specific location. Prompt the user for the location. For
example, if the user enters 1800, the following are the results:

8. Find the number of employees who have a last name that ends with the letter n. Create two
possible solutions.

ce
i
l
le

to
e
ns

9. Create a report that shows the name, location, and number of employees for each department.
Make sure that the report also includes departments without employees.

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
e e th
l
c
us
ora

9
0
0
40

10. The HR department needs to find the job titles in departments 10 and 20. Create a report to
display the job IDs for those departments.

1
4
2
(g

11. Create a report that displays the jobs that are found in the Administration and Executive
departments. Also display the number of employees for these jobs. Show the job with the
highest number of employees first.

Oracle Database 10g: SQL Fundamentals I Additional Practices - 4

Universitas Semarang

Additional Practices (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

These exercises can be used for extra practice after you have discussed the following topics:
basic SQL SELECT statements, SQL functions, joins, group functions, and subqueries.
12. Show all employees who were hired in the first half of the month (before the 16th of the
month).

ce
i
l
le

to
e
ns

13. Create a report that displays the following for all employees: last name, salary, and salary
expressed in terms of thousands of dollars.

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
e e th
l
c
us
ora

9
0
0
40

11
4
2
(g 14. Show all employees who have managers with a salary higher than $15,000. Show the

following data: employee name, manager name, manager salary, and salary grade of the
manager.

Oracle Database 10g: SQL Fundamentals I Additional Practices - 5

Universitas Semarang

Additional Practices (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

15. Show the department number, name, number of employees, and average salary of all
departments along with the names, salaries, and jobs of the employees working in each
department.

to
e
ns

ce
i
l
le

(g2

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
the department
16. Create a report to display
th number and the lowest salary of the department
e
l
c
e
with the highest raverage
a salary.
us
o
9@
0
0
40
1
1
4

17. Create a report that displays the departments where no sales representatives work. Include the
department number, department name, and location in the output.

Oracle Database 10g: SQL Fundamentals I Additional Practices - 6

Universitas Semarang

Additional Practices (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

18. Create the following statistical reports for the HR department: Include the department
number, department name, and the number of employees working in each department that:
a. Employs fewer than three employees:

b. Has the highest number of employees:

c. Has the lowest number of employees:

ce
i
l
le

to
e
ns

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
e e th
l
c
us
ora

19. Create a report that displays the employee number, last name, salary, department number,
and the average salary in their departments for all employees.

9
0
0
40

1
4
2
(g

Oracle Database 10g: SQL Fundamentals I Additional Practices - 7

Universitas Semarang

Additional Practices (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

20. Show all employees who were hired on the day of the week on which the highest number of
employees were hired.

21. Create an anniversary overview based on the hire date of the employees. Sort the
anniversaries in ascending order.

to
e
ns

ce
i
l
le

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
e e th
l
c
us
ora

9
0
0
40

1
4
2
(g

Oracle Database 10g: SQL Fundamentals I Additional Practices - 8

Universitas Semarang

Additional Practices: Case Study

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

In this case study, you build a set of database tables for a video application. After you create the
tables, you insert, update, and delete records in a video store database and generate a report. The
database contains only the essential tables.
The following is a diagram of the entities and attributes for the video application:
for
RESERVATION
#* res date
set up for

the subject
of

TITLE
#* id
* title
* description
o rating
o category
o release date

to
e
ns

available as

AS ferab
Y
T ns
G
tra
NINTITLE_COPY
n
A
M a n#*oid
MEMBER
U

#* id
US has id*estatus
K
)
R
* last name
U cid t Gu
the subject of
N
o first name
a
n

A
e
o address ITR responsible
sm Stud
u

C
k
o city
tifor
is
f

made against
h
t
e
o phone
l
c
e
ra
* join date
us
o
RENTAL
@
9
#* book date
created
0
0
o act ret date
for
40

responsible
for

1
4
2
(g

ce
i
l
le

a copy

o exp ret date

Note: If you want to build the tables, you can execute the commands in the
buildtab.sql script in SQL Developer. If you want to drop the tables, you can execute
the commands in the dropvid.sql script in SQL Developer. Then you can execute the
commands in the buildvid.sql script in SQL Developer to create and populate the
tables.

If you use the buildtab.sql script to build the tables, start with step 4.

If you use the dropvid.sql script to remove the video tables, start with step 1.

If you use the buildvid.sql script to build and populate the tables, start with step
6(b).

Oracle Database 10g: SQL Fundamentals I Additional Practices - 9

Universitas Semarang

Additional Practices: Case Study (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

1. Create the tables based on the following table instance charts. Choose the appropriate data
types and ensure that you add integrity constraints.
a. Table name: MEMBER
Column_
Name

MEMBER_
ID

Key
Type
Null/
Unique
Default
Value

PK

Data
Type
Length

NN,U

LAST_
NAME

FIRST_NAME

ADDRESS

CITY

PHONE

JOIN
_
DATE

NN

NN

NUMBER

VARCHAR2

VARCHAR2

VARCHAR2

VARCHAR2

VARCHAR2

System
Date
DATE

10

25

25

100

30

15

to
e
ns

ce
i
l
le

(g2

AS ferab
Y
T ns
b. Table name: TITLE
G
IN -tra
N
A
TITLE_ID TITLE
DESCRIPTION RATING on CATEGORY
RELEASE_
Column_
M an
DATE
U
Name
S as

U
K ) h uide
PK
Key
R
U cid t G
Type
N
NN,U
NN A
NN
Null/
a den
R
m
s
T
Unique
CI ftiku is Stu
G, PG, R,
DRAMA,
Check

h
t
e
NC17,
NR
COMEDY,
l
c
e
s
a
ACTION,
r
u
o
CHILD,
@
9
0
SCIFI,
0
0
DOCUMEN
4
1
1
TARY
4
Data Type

NUMBER

VARCHAR2

VARCHAR2

VARCHAR2

VARCHAR2

Length

10

60

400

20

DATE

Oracle Database 10g: SQL Fundamentals I Additional Practices - 10

Universitas Semarang

Additional Practices: Case Study (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

c. Table name: TITLE_COPY


Column
Name
Key
Type
Null/
Unique
Check

FK Ref
Table
FK Ref
Col
Data
Type
Length

(g2

COPY_ID

TITLE_ID

PK

PK,FK

NN,U

NN,U

STATUS

NN
AVAILABLE,
DESTROYED,
RENTED,
RESERVED

TITLE
TITLE_ID
NUMBER

NUMBER

to
e
ns

VARCHAR2

ce
i
l
le

AS ferab
Y
T ns
G
NIN on-tra
A
d. Table name: RENTAL
UM s a n
S
e
KU )ACT_RET_
ha uidEXP_RET_
BOOK_
MEMBER_ R
COPY_
TITLE_
Column
d
U
i
DATE
ID
ID
DATE
DATE
ID
G

Name
N ac
t
n
A
PK
PK,FK1
PK,FK2
Key
R usmPK,FK2tude
T
I
Type
C
ik his S
System eft
System Date
Default
t
l
e
Daterac
+
2 days
Value
s
u
o
MEMBER
TITLE_
TITLE_
FK Ref @
9
COPY
COPY
0
Table
0
0
MEMBER_I COPY_
TITLE_ID
114FK Ref
D
ID
Col
Data
Type
Length

10

DATE

10

NUMBER

NUMBER

10

10

15

DATE

DATE

NUMBER
10

Oracle Database 10g: SQL Fundamentals I Additional Practices - 11

Universitas Semarang

Additional Practices: Case Study (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

e. Table name: RESERVATION


Column
Name
Key
Type
Null/
Unique
FK Ref
Table
FK Ref
Column
Data Type

RES_
DATE

MEMBER_
ID

TITLE_
ID

PK

PK,FK1

PK,FK2

NN,U

NN,U

NN

MEMBER

TITLE

MEMBER_ID

TITLE_ID

NUMBER

NUMBER

Length

10

10

DATE

ce
i
l
le

to
e
ns

2. Verify that the tables and constraints were created properly by checking the data dictionary.

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
e e th
l
c
us
ora

9
0
0
40

1
4
2
(g

Oracle Database 10g: SQL Fundamentals I Additional Practices - 12

Universitas Semarang

Additional Practices: Case Study (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

3. Create sequences to uniquely identify each row in the MEMBER table and the TITLE table.
a. Member number for the MEMBER table: Start with 101; do not allow caching of values.
Name the sequence MEMBER_ID_SEQ.
b. Title number for the TITLE table: Start with 92; do not allow caching of values. Name
the sequence TITLE_ID_SEQ.
c. Verify the existence of the sequences in the data dictionary.

4. Add data to the tables. Create a script for each set of data to be added.
a. Add movie titles to the TITLE table. Write a script to enter the movie information.
Save the statements in a script named lab_apcs_4a.sql. Use the sequences to
uniquely identify each title. Enter the release dates in the DD-MON-YYYY format.
Remember that single quotation marks in a character field must be specially handled.
Verify your additions.

ce
i
l
le

to
e
ns

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
e e th
l
c
us
ora

9
0
0
40

1
4
2
(g

Oracle Database 10g: SQL Fundamentals I Additional Practices - 13

Universitas Semarang

Additional Practices: Case Study (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

Title
Willie and
Christmas Too
Alien Again

The Glob
My Day Off
Miracles on Ice

Description
All of Willies friends make a
Christmas list for Santa, but
Willie is yet to add his own
wish list.
Yet another installation of
science fiction history. Can
the heroine save the planet
from the alien life form?
A meteor crashes near a small
American town and unleashes
carnivorous goo in this classic.
With a little luck and a lot of
ingenuity, a teenager skips
school for a day in New York.
A six-year-old has doubts
about Santa Claus, but she
discovers that miracles really
do exist.
After discovering a cache of
drugs, a young couple find
themselves pitted against a
vicious gang.

Rating
G

Category
CHILD

Release_date
05-OCT-1995

SCIFI

19-MAY-1995

NR

SCIFI

12-AUG-1995

PG

COMEDY

12-JUL-1995

PG

DRAMA

12-SEP-1995

ce
i
l
le

(g2

AS f01-JUN-1995
rab
Y
e
T
G rans
N
I
N on-t
A
UM s a n
S
U INSERT
b. Add data to the MEMBER table. Place
a script named
Kthe
ha statements
deBeinsure
i
)
R
u
d
lab_apcs_4b.sql. Execute
the
commands
in
the
script.
to use the sequence
U ci
G
N
t
to add the member numbers.
A ma den
R
T
First_
CI ftikus is Stu
Name
Last_Name
City
Phone
Join_Date
th
e eAddress
l
c
Carmen
Velasquez
283 King
Seattle
206-899-6666 08-MAR-1990
ra
us Street
o
9@ Ngao
0
LaDoris
5 Modrany
Bratislava
586-355-8882 08-MAR-1990
0
0
4
411
Soda Gang

Midori

Nagayama

Ropeburn

68 Via
Centrale
6921 King
Way
86 Chu Street

Mark

Quick-to-See

Audry
Molly

Urguhart

3035 Laurier

NR

to
e
ns

ACTION

Sao Paolo

254-852-5764

17-JUN-1991

Lagos

63-559-7777

07-APR-1990

Hong Kong

41-559-87

18-JAN-1991

Quebec

418-542-9988

18-JAN-1991

Oracle Database 10g: SQL Fundamentals I Additional Practices - 14

Universitas Semarang

Additional Practices: Case Study (continued)

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

c. Add the following movie copies in the TITLE_COPY table:


Note: Have the TITLE_ID numbers available for this exercise.
Title

Copy_Id

Status

Title

Copy_Id

Willie and
Christmas Too
Alien Again

AVAILABLE

AVAILABLE

Willie and Christmas


Too
Alien Again

RENTED

The Glob

AVAILABLE

The Glob

My Day Off

AVAILABLE

My Day Off

AVAILABLE

RENTED

1
2

(g2

AS1 ferab
Y
T ns1
G
Soda Gang
1
AVAILABLE
Soda Gang
NIN on-tra
A
UM s a n
S
d. Add the following rentals to the RENTAL
table:
e
KU depending
ha onutheidsequence
Note: The title number may be different
number.
)
R
d
U ci
G
N
t
Title_ Id
Copy_
Member_Ida
A
en
R
m
d
Id
Book_date
Exp_Ret_Date
s
T
u
I 101
t 3 days ago
u

S
k
92
1 C
1 day ago
i
t this
f

e
cl u101se
93
2ora
1 day ago
1 day from now
@
9 3
95 00
102
2 days ago
Today
0
4
411

ce
i
l
le

Miracles on Ice

97

AVAILABLE

106

Miracles on Ice

4 days ago

2 days ago

Oracle Database 10g: SQL Fundamentals I Additional Practices - 15

to
e
ns

Universitas Semarang

Additional Practices: Case Study (continued)


5. Create a view named TITLE_AVAIL to show the movie titles, the availability of each copy,
and its expected return date if rented. Query all rows from the view. Order the results by title.

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

Note: Your results may be different.

to
e
ns

6. Make changes to the data in the tables.

ce
i
l
le

a. Add a new title. The movie is Interstellar Wars, which is rated PG and classified as a
science fiction movie. The release date is 07-JUL-77. The description is Futuristic
interstellar action movie. Can the rebels save the humans from the evil empire? Be sure
to add a title copy record for two copies.

AS ferab
Y
T ns
G
N -tra
NIVelasquez,
A
b. Enter two reservations. One reservation is for Carmen
on who wants to rent
M
n
U
Interstellar Wars. The other is for MarkSQuick-to-See,
a whowants to rent Soda Gang.
s
U
a
K ) h uide
7. Make a modification to one of the tables.
R
U cid t G
N
a. Run the script in lab_apcs_7a.sql
A ma to daddena PRICE column to the TITLE table to
R
T
record the purchase
price
Verify
tu your modifications.
S
CI ftikofuthes video.
is

h
t
e
l
rac use
o
9@
0
0
40
1
1
4

(g2

Title
Willie and Christmas Too
Alien Again
The Glob
My Day Off
Miracles on Ice
Soda Gang
Interstellar Wars

Price
25
35
35
35
30
35
29

Oracle Database 10g: SQL Fundamentals I Additional Practices - 16

Universitas Semarang

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

b. Create a script named lab_apcs_7b.sql that contains UPDATE statements that


update each video with a price according to the preceding list. Run the commands in the
script.
Note: Have the TITLE_ID numbers available for this exercise.
8. Create a report that contains each customers history of renting videos. Be sure to include the
customer name, movie rented, dates of the rental, and duration of rentals. Total the number of
rentals for all customers for the reporting period. Save the commands that generate the report
in a script file named lab_apcs_8.sql.
Note: Your results may be different.

to
e
ns

ce
i
l
le

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
e e th
l
c
us
ora

9
0
0
40

1
4
2
(g

Oracle Database 10g: SQL Fundamentals I Additional Practices - 17

Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates

Universitas Semarang

to
e
ns

ce
i
l
le

AS ferab
Y
T ns
G
NIN on-tra
A
UM s a n
S
KU ) ha uide
R
U cid t G
N
A ma den
R
T
CI ftikus is Stu
e e th
l
c
us
ora

1
4
2
(g

9
0
0
40

S-ar putea să vă placă și