Professional Documents
Culture Documents
SQL Questions and Answers
SQL Questions and Answers
l
b
a
fer
ns
e
c
li
an
r
t
n
Oracle Database
10g: SQL
no
a
Fundamentals
e I
has
t
a
y
da
) Guid
m
Guide
t Volume 3
coStudent
n
o
e
o tud
h
a
y is S
@
07 se th
0
t
ya to u
a
d
(
ur H
D17108GC30
Edition 3.0
January 2009
D57872
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Authors
Salome Clement
Chaitanya Koratamaddi
Nancy Greenberg
Disclaimer
Technical Contributors
and Reviewers
Wayne Abbott
Christian Bauwens
Claire Bennett
Perry Benson
Brian Boxx
Zarko Cesljas
Dairy Chan
Laszlo Czinkoczki
Joel Goodman
Matthew Gregory
Sushma Jagannath
Yash Jain
Angelika Krupp
Isabelle Marchand
Malika Marghadi
Valli Pataballa
Narayanan Radhakrishnan
Bryan Roberts
Helen Robertson
Lata Shivaprasad
John Soltani
James Spiller
Priya Vennapusa
t
a
y
da
ur H
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
Trademark Notice
Editors
Arijit Ghosh
Raj Kumar
Graphic Designer
Rajiv Chandrabhanu
Publisher
Giri Venugopal
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Contents
Preface
I
Introduction
Lesson Objectives I-2
Goals of the Course I-3
Oracle10g I-4
Oracle Database 10g I-6
Oracle Application Server 10g I-7
Oracle Enterprise Manager 10g Grid Control I-8
Relational and Object Relational Database Management Systems I-9
Oracle Internet Platform I-10
System Development Life Cycle I-11
Data Storage on Different Media I-13
Relational Database Concept I-14
Definition of a Relational Database I-15
Data Models I-16
Entity Relationship Model I-17
Entity Relationship Modeling Conventions I-19
Relating Multiple Tables I-21
Relational Database Terminology I-23
Relational Database Properties I-25
Communicating with an RDBMS Using SQL I-26
Oracles Relational Database Management System I-27
SQL Statements I-28
Tables Used in the Course I-29
Summary I-30
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
ur H
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
iii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
t
a
y
da
ur H
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
iv
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
t
a
y
da
ur H
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
DECODE Function 3-57
has uide
)
Using the DECODE Function 3-58
om nt G
c
Summary 3-60
oo tude
h
Practice 3: Overview of Part 2a3-61
y is S
@
th the Group Functions
07 DataseUsing
0
4 Reporting Aggregated
t
ya to u
a
Objectives
4-2
d
( Group Functions? 4-3
t
What
Are
a
y
aTypes
d
of Group Functions 4-4
i
H
r
Group Functions: Syntax 4-5
u
Using the CASE Expression 3-56
vi
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
Retrieving Records with the ON Clause
om 5-14
tG
c
n
o
e
Self-Joins Using the ON Clause
5-15
o tud
h
a
y is toSa Join 5-17
Applying Additional Conditions
@
thwith the ON Clause 5-18
Creating Three-Way
07 sJoins
e
0
t
Nonequijoins
ya to5-19u
a
d
(
Retrieving
Records with Nonequijoins 5-20
t
a
y
da Outer Joins
5-21
INNER Versus OUTER Joins 5-22
i
H
r
u
vii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
an
r
t
n
l
b
a
fer
ns
e
c
li
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
Using the UNION Operator y
7-9
S
s
@ thi
77-11
UNION ALL Operator
0
0
t ALL Operator
se 7-12
a
u
Using the UNION
y
daOperatorto7-13
(
INTERSECT
t
a
y
a
Using
the INTERSECT Operator 7-14
id
rH
u
N
Manipulating Data
Objectives 8-2
Data Manipulation Language 8-3
Adding a New Row to a Table 8-4
INSERT Statement Syntax 8-5
Inserting New Rows 8-6
viii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
an
r
t
n
l
b
a
fer
ns
e
c
li
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
t Data After
s COMMIT 8-32
State y
ofa
the
u
o
a
t
(d
Committing
Data 8-33
r
Nu
t
a
y
da State of the Data After ROLLBACK
Hi
8-34
ix
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
10 Creating Other Schema Objects
om nt G
c
o ude
Objectives 10-2
o
h
t
a
S
y
Database Objects 10-3
s
7@ e thi
What Is a View? 0
10-4
0
t 10-5
s
aViews
u
Advantagesaof
y
o
t Views 10-6
(d and Complex
Simple
Views
t
a
y a View 10-7
aCreating
d
i
ur H Retrieving Data from a View 10-10
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
USER_OBJECTS View 11-8
has uide
)
Table Information 11-9
m tG
o
c
r
Nu
Hi
11-19
Summary 11-20
Practice 11: Overview 11-21
Practice Solutions
xi
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices
Additional Practices: Table Descriptions and Data
Additional Practices: Solutions
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
xii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
__________________
Additional Practices
__________________
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices
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.
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
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).
t
a
y
da
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
s
n
a
r
5. Show those employees who have a last name starting with the letters
-t J, K, L, or M.
n
o
n
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t all employees and indicate with the words Yes or No whether
6. Create a(report
d that displays
t
a
y receive a commission. Use the DECODE expression in your query.
athey
d
i
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
8. Find the number of employees who have a last name that ends with the letter n. Create two
possible solutions.
l
b
a
fer
ns
e
c
li
s
n
a
r
-t for each department.
9. Create a report that shows the name, location, and number of employees
n
o
nemployees.
Make sure that the report also includes departments without
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
t
a
ay
d
i
ur H
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.
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.
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
s
n
a
r
13. Create a report that displays the following for all employees: last name,
-t salary, and salary
n
o
expressed in terms of thousands of dollars.
n
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
t
a
ay
d
i
ur H
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.
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
t
a
y
16.
Create a report to display the department number and the lowest salary of the department
idawith
H
the highest average salary.
ur
17. Create a report that displays the departments where no sales representatives work. Include the
department number, department name, and location in the output.
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
department number,
19. Create a report that displays the employee number,
alasts name,
esalary,
h
d
i
)
and the average salary in their departments for
u
m all employees.
G
o
t
c
o uden
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
t
a
ay
d
i
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
21. Create an anniversary overview based on the hire date of the employees. Sort the
anniversaries in ascending order.
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
the subject
of
set up for
TITLE
#* id
* title
* description
o rating
o category
o release date
le
b
a
available
er as
f
s
n
-tra
on
n
aa copy
responsible
s
a
h uide
for
)
om nt G TITLE_COPY
c
o ude
#* id
o
MEMBER
h
t
* status
ya is S
#* id
@
th
* last name 07
e
0
the subject of
t
s
o first y
name
a ou
a
t responsible
o(address
d
t
a
o city
for
made against
day
ur H
ns
e
c
li
o phone
* join date
created
for
RENTAL
#* book date
o act ret date
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).
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Nu
Column_
Name
MEMBER_
ID
Key
Type
Null/
Unique
Default
Value
PK
Data
Type
Length
NUMBER
VARCHAR2
VARCHAR2
VARCHAR2
VARCHAR2
10
25
25
100
30
NN,U
LAST_
NAME
FIRST_NAME
ADDRESS
CITY
PHONE
JOIN
_
DATE
NN
NN
s 15
an
r
t
n
no
a
b. Table name: TITLE
has uide
)
m tG
o
c
TITLE_ID TITLE
Column_
oDESCRIPTION
en RATING
o
d
h
u
Name
t
a
S
y
s
PK
Key
7@ e thi
0
Type
0
at oNNus
NN,U
NN
y
Null/
a
t
Unique t (d
a
G, PG, R,
Check
y
a
d
NC17, NR
i
H
l
b
a
fer
VARCHAR2
ns
e
c
li
System
Date
DATE
CATEGORY
Data Type
NUMBER
VARCHAR2
VARCHAR2
VARCHAR2
DRAMA,
COMEDY,
ACTION,
CHILD,
SCIFI,
DOCUMEN
TARY
VARCHAR2
Length
10
60
400
20
RELEASE_
DATE
DATE
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
FK Ref
Table
FK Ref
Col
Data
Type
Length
COPY_ID
TITLE_ID
PK
PK,FK
NN,U
NN,U
STATUS
NN
AVAILABLE,
DESTROYED,
RENTED,
RESERVED
Value
FK Ref
Table
FK Ref
Col
Data
Type
Length
l
b
a
fer
TITLE_ID
NUMBER
10
VARCHAR2
10
Date
TITLE_
ID
PK,FK2
+ 2 days
MEMBER
DATE
ns
e
c
li
an
r
t
n
NUMBER
no15
a
has uide
)
om nt G
c
o ude
d. Table name: RENTAL
o
h
t
a
S
y
s
@ thi COPY_ ACT_RET_ EXP_RET_
BOOK_ 7
MEMBER_
Column
0
DATE
ID
DATE
DATE
0
t
Name
se ID
a
u
y
aPK toPK,FK1 PK,FK2
Key
d
(
t
Type
a
y
a
System
System Date
Default
id
rH
u
N
TITLE
MEMBER_I
D
NUMBER
TITLE_
COPY
COPY_
ID
NUMBER
10
10
TITLE_
COPY
TITLE_ID
DATE
DATE
NUMBER
10
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
RES_
DATE
MEMBER_
ID
TITLE_
ID
PK
PK,FK1
PK,FK2
NN,U
NN,U
NN
MEMBER
TITLE
MEMBER_ID
TITLE_ID
NUMBER
NUMBER
10
10
DATE
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
2. Verify that the tables and constraints were created properly by checking the data dictionary.
t
a
y
da
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
4. Add data to the tables. Create a script for each set of data to be added.
ns
e
c
li
an
r
t
n
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.
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
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
ns
e
c
li
le
b
a
r
fe12-SEP-1995
an
r
t
n
o
n
a
Soda Gang
NR s
ACTION
01-JUN-1995
a
e
h
d
) Gui
m
o
c dent
o
o
h Stu
a
y
b. Add data to the MEMBER
is the INSERT statements in a script named
@ table.thPlace
7
0
lab_apcs_4b.sql.
Execute
0 use the commands in the script. Be sure to use the sequence
t
a
to add the
aymembertonumbers.
d
(
t
First_
a
y
Last_Name
Address
City
Phone
Join_Date
idaName
rH
u
N
Carmen
Velasquez
283 King
Street
5 Modrany
Seattle
206-899-6666
08-MAR-1990
LaDoris
Ngao
Bratislava
586-355-8882
08-MAR-1990
Midori
Nagayama
Sao Paolo
254-852-5764
17-JUN-1991
Lagos
63-559-7777
07-APR-1990
Ropeburn
68 Via
Centrale
6921 King
Way
86 Chu Street
Mark
Quick-to-See
Audry
Hong Kong
41-559-87
18-JAN-1991
Molly
Urguhart
3035 Laurier
Quebec
418-542-9988
18-JAN-1991
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Copy_Id
Status
Title
Copy_Id
Willie and
Christmas Too
Alien Again
AVAILABLE
AVAILABLE
RENTED
The Glob
AVAILABLE
The Glob
My Day Off
AVAILABLE
My Day Off
AVAILABLE
RENTED
1
2
l
b
a
fer
ns
e
c
li
s3
n
a
r
n-t
no
1
a
s
haSoda uGang
de
i
Soda Gang
1
AVAILABLE )
1
m
G
o
c dent
o
o
tu table:
ahthe RENTAL
d. Add the following rentalsyto
S
is depending on the sequence number.
@may bethdifferent
Note: The title number
7
0
t0 uMember_Id
se
a
Title_ Id
Copy_
y
da Id to
Book_date
Exp_Ret_Date
(
t
92ya
1
101
3 days ago
1 day ago
ida
Miracles on Ice
rH
u
N
AVAILABLE
Miracles on Ice
93
101
1 day ago
95
102
2 days ago
Today
97
106
4 days ago
2 days ago
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
s
n
a
r
-t PG and classified as a
a. Add a new title. The movie is Interstellar Wars, which is rated
n
o
n description is Futuristic
science fiction movie. The release date is 07-JUL-77. The
a
s from
interstellar action movie. Can the rebels save theahumans
e the evil empire? Be sure
d
i
to add a title copy record for two copies. ) h
m t Gu
o
c
b. Enter two reservations. One reservation
o uisdforenCarmen Velasquez, who wants to rent
o
h
Interstellar Wars. The other
Mark
t Quick-to-See, who wants to rent Soda Gang.
a is for S
y
s
i
@of thethtables.
7. Make a modification to7one
0
e
0
s
at in lab_apcs_7a.sql
u
a. Run the script
to add a PRICE column to the TITLE table to
y
o
a
t price of the video. Verify
d
(
record
the
purchase
your modifications.
t
a
ay
d
i
ur H
6. Make changes to the data in the tables.
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
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
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.
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Tables Used in Additional Practices
Additional practice questions 1-21 use the HR schema. Refer to Appendix B to look at the HR
schema tables. The tables used in the additional practices: case study are described below.
Note: These table do not exist by default. You will be creating them in the case study practice
questions.
TITLE
for
RESERVATION
#* res date
the subject
of
set up for
#* id
* title
* description
o rating
o category
o release date
l
b
a
fer
ns
e
c
li
an
r
t
n
available as
no
a
has uidaecopy
responsible
)
for
om nt G
c
TITLE_COPY
o ude
o
h
t
#* id
ya is S
MEMBER
@
* status
7 e th
#* id
0
0
at o us
* lastyname
the subject of
a
o(d
first name t
t
a o address
y
responsible
a
id
rH
u
N
o city
o phone
* join date
for
made against
created
for
RENTAL
#* book date
o act ret date
o exp ret date
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
RESERVATION Table
DESCRIBE reservation
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
MEMBER Table
DESCRIBE member
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
TITLE Table
DESCRIBE title
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
TITLE_COPY Table
DESCRIBE title_copy
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
RENTAL Table
DESCRIBE rental
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
__________________
Additional Practices:
Solutions
__________________
e
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
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.
SELECT last_name, job_id, salary, commission_pct
FROM
employees
WHERE commission_pct IS NOT NULL
ORDER BY salary DESC;
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
m tG
o
c
SELECT last_name,
TRUNC(MONTHS_BETWEEN(SYSDATE, hire_date) / 12) YEARS,
TRUNC(MOD(MONTHS_BETWEEN(SYSDATE, hire_date), 12))
MONTHS
FROM employees
ORDER BY years DESC, MONTHS desc;
5. Show those employees who have a last name starting with the letters J, K, L, or M.
SELECT last_name
FROM
employees
WHERE SUBSTR(last_name, 1,1) IN ('J', 'K', 'L', 'M');
6. Create a report that displays all the employees and indicate with the words Yes or No whether
they receive a commission. Use the DECODE expression in your query.
SELECT last_name, salary,
decode(commission_pct, NULL, 'No', 'Yes') commission
FROM
employees;
Oracle Database 10g: SQL Fundamentals I Additional Practices: Solutions - 2
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Solutions (continued)
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.
SELECT d.department_name, d.location_id, e.last_name, e.job_id, e.salary
FROM
employees e, departments d
WHERE
e.department_id = d.department_id
AND
d.location_id = &dept_no;
8. Find the number of employees who have a last name that ends with the letter n. Create two
possible solutions.
SELECT
FROM
WHERE
--or
SELECT
FROM
WHERE
COUNT(*)
employees
last_name LIKE '%n';
COUNT(*)
employees
SUBSTR(last_name, -1) = 'n';
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
om nt G
SELECT d.department_id, d.department_name,
c
o ude
d.location_id,
COUNT(e.employee_id)
o
h
t departments d
FROM
employees e RIGHTya
OUTER JOIN
S
s
ON
e.department_id
7@= ed.department_id
thi
0
GROUP BY d.department_id,
d.department_name,
d.location_id;
0
t
s
a
u
y
to
da
(department
10. TheaHR
needs to find the job titles in departments 10 and 20. Create a report to
t
y the job IDs for these departments.
adisplay
d
i
H
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.
Nu
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.
SELECT e.job_id, count(e.job_id) FREQUENCY
FROM
employees e JOIN departments d
ON e.department_id = d.department_id
WHERE
d.department_name IN ('Administration', 'Executive')
GROUP BY e.job_id
ORDER BY FREQUENCY DESC;
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).
Oracle Database 10g: SQL Fundamentals I Additional Practices: Solutions - 3
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Solutions (continued)
SELECT last_name, hire_date
FROM
employees
WHERE TO_CHAR(hire_date, 'DD') < 16;
13. Create a report that displays the following for all employees: last name, salary, and salary
expressed in terms of thousands of dollars.
SELECT last_name, salary, TRUNC(salary, -3)/1000
FROM
employees;
Thousands
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.
SELECT
FROM
ON
JOIN
ON
AND
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
om nt G
SELECT d.department_id, d.department_name,
c
oemployees,
e
o
d
count(e1.employee_id)
h
u
t
NVL(TO_CHAR(AVG(e1.salary),
ya is S '99999.99'), 'No average' ) avg_sal,
@
e2.last_name,
thOUTERe2.job_id
07d e2.salary,
e
0
FROM
departments
RIGHT
JOIN employees e1
t
s
a o u = e1.department_id
y
ON
d.department_id
a
t
(d JOIN employees
RIGHT OUTER
e2
t
a
ON ay d.department_id = e2.department_id
id BY d.department_id, d.department_name, e2.last_name, e2.salary,
GROUP
H
r
e2.job_id
u
15. Show the department number, name, number of employees, and average salary of all
departments together with the names, salaries, and jobs of the employees working in each
department.
16. Create a report to display the department number and lowest salary of the department with
the highest average salary.
SELECT department_id, MIN(salary)
FROM
employees
GROUP BY department_id
HAVING AVG(salary) = (SELECT MAX(AVG(salary))
FROM
employees
GROUP BY department_id);
17. Create a report that displays the departments where no sales representatives work. Include the
department number, department name, and location in the output.
SELECT *
FROM
departments
WHERE department_id NOT IN(SELECT department_id
FROM employees
Oracle Database 10g: SQL Fundamentals I Additional Practices: Solutions - 4
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Solutions (continued)
WHERE job_id = 'SA_REP'
AND department_id IS NOT NULL);
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:
SELECT d.department_id, d.department_name, COUNT(*)
FROM
departments d JOIN employees e
ON
d.department_id = e.department_id
GROUP BY d.department_id, d.department_name
HAVING COUNT(*) < 3;
an
r
t
n
Nu
l
b
a
fer
ns
e
c
li
no
a
as ide
h
)
c. Has the lowest number of employees: m
u
G
o
t
c
SELECT d.department_id, d.department_name,
o uden COUNT(*)
o
h
FROM
departments d JOINaemployees
t e
S
y
ON
d.department_id
=
e.department_id
s
7@ e d.department_name
thi
0
GROUP BY d.department_id,
0
s MIN(COUNT(*))
at= (SELECT
HAVING COUNT(*)
u
y
o
a
tFROM employees
(d
t
GROUP BY department_id);
a
y
a
Hid
19. Create a report that displays the employee number, last name, salary, department number,
and the average salary in their department for all employees.
SELECT e.employee_id, e.last_name, e.department_id, e.salary,
AVG(s.salary)
FROM
employees e JOIN employees s
ON
e.department_id = s.department_id
GROUP BY e.employee_id, e.last_name, e.department_id, e.salary;
20. Show all employees who were hired on the day of the week on which the highest number of
employees were hired.
SELECT last_name, TO_CHAR(hire_date, 'DAY') day
FROM
employees
WHERE TO_CHAR(hire_date, 'Day') =
(SELECT TO_CHAR(hire_date, 'Day')
FROM
employees
GROUP BY TO_CHAR(hire_date, 'Day')
HAVING COUNT(*) = (SELECT MAX(COUNT(*))
FROM
employees
GROUP BY TO_CHAR(hire_date, 'Day')));
Oracle Database 10g: SQL Fundamentals I Additional Practices: Solutions - 5
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Solutions (continued)
21. Create an anniversary overview based on the hire date of the employees. Sort the
anniversaries in ascending order.
SELECT last_name, TO_CHAR(hire_date, 'Month DD') BIRTHDAY
FROM
employees
ORDER BY TO_CHAR(hire_date, 'DDD');
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
CREATE TABLE title
(title_id
NUMBER(10)
has uide
)
CONSTRAINT title_title_id_pk
G KEY,
om PRIMARY
t
c
n
title
VARCHAR2(60)
o uNOT
e NULL,
o
d
CONSTRAINT title_title_nn
h
t
ya is S
description
VARCHAR2(400)
@
th
CONSTRAINT
NOT NULL,
07title_description_nn
e
0
t
s
rating a
VARCHAR2(4)
u
ay
totitle_rating_ck CHECK
CONSTRAINT
d
(
t
a (rating IN ('G', 'PG', 'R', 'NC17', 'NR')),
y
a
category
VARCHAR2(20)
id
H
CONSTRAINT
title_category_ck
CHECK
ur
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Case Study Solutions (continued)
CREATE TABLE rental
(book_date
DATE DEFAULT SYSDATE,
member_id
NUMBER(10)
CONSTRAINT rental_member_id_fk REFERENCES member(member_id),
copy_id
NUMBER(10),
act_ret_date DATE,
exp_ret_date DATE DEFAULT SYSDATE + 2,
title_id
NUMBER(10),
CONSTRAINT rental_book_date_copy_title_pk
PRIMARY KEY (book_date, member_id, copy_id,title_id),
CONSTRAINT rental_copy_id_title_id_fk
FOREIGN KEY (copy_id, title_id)
REFERENCES title_copy(copy_id, title_id));
ns
e
c
li
l
b
a
fer
an
r
t
n
Nu
no
a
has uide
)
m properly
ocreated
t G by checking the data dictionary.
c
2. Verify that the tables and constraints were
n
o
e
o tud
h
SELECT
table_name
a
y is S
FROM
user_tables
@
th
07IN s('MEMBER',
WHERE
table_name
'TITLE', 'TITLE_COPY',
e
0
t
a
u
'RENTAL',
'RESERVATION');
y
a
to
d
(
t constraint_name, constraint_type, table_name
a
SELECT
y
user_constraints
ida
HFROM
WHERE
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.
CREATE SEQUENCE member_id_seq
START WITH 101
NOCACHE;
b. Title number for the TITLE table: Start with 92; do not allow caching of values. Name
the sequence TITLE_ID_SEQ.
CREATE SEQUENCE title_id_seq
START WITH 92
NOCACHE;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Case Study Solutions (continued)
SELECT
FROM
WHERE
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.
INSERT INTO title(title_id, title, description, rating,
category, release_date)
VALUES (title_id_seq.NEXTVAL, 'Willie and Christmas Too',
'All of Willie''s friends make a Christmas list for
Santa, but Willie has yet to add his own wish list.',
'G', 'CHILD', TO_DATE('05-OCT-1995','DD-MON-YYYY'))
/
INSERT INTO title(title_id , title, description, rating,
category, release_date)
VALUES
(title_id_seq.NEXTVAL, 'Alien Again', 'Yet another
installment of science fiction history. Can the
heroine save the planet from the alien life form?',
'R', 'SCIFI', TO_DATE( '19-MAY-1995','DD-MON-YYYY'))
/
INSERT INTO title(title_id, title, description, rating,
category, release_date)
VALUES
(title_id_seq.NEXTVAL, 'The Glob', 'A meteor crashes
near a small American town and unleashes carnivorous
goo in this classic.', 'NR', 'SCIFI',
TO_DATE( '12-AUG-1995','DD-MON-YYYY'))
/
INSERT INTO title(title_id, title, description, rating,
category, release_date)
VALUES
(title_id_seq.NEXTVAL, 'My Day Off', 'With a little
luck and a lot ingenuity, a teenager skips school for
a day in New York.', 'PG', 'COMEDY',
TO_DATE( '12-JUL-1995','DD-MON-YYYY'))
/
...
COMMIT
/
SELECT title
FROM
title;
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
b. Add data to the MEMBER table. Place the INSERT statements in a script named
lab_apcs_4b.sql. Execute the commands in the script. Be sure to use the sequence
to add the member numbers.
SET VERIFY OFF
INSERT INTO member(member_id, first_name, last_name,
address, city, phone, join_date)
Oracle Database 10g: SQL Fundamentals I Additional Practices: Solutions - 9
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Case Study Solutions (continued)
VALUES (member_id_seq.NEXTVAL, 'Carmen', 'Velasquez',
'283 King Street', 'Seattle', '206-899-6666', TO_DATE('08-MAR1990',
'DD-MM-YYYY'))
/
INSERT INTO member(member_id, first_name, last_name,
address, city, phone, join_date)
VALUES (member_id_seq.NEXTVAL, 'LaDoris', 'Ngao',
'5 Modrany', 'Bratislava', '586-355-8882', TO_DATE('08-MAR-1990',
'DD-MM-YYYY'))
/
INSERT INTO member(member_id, first_name, last_name,
address, city, phone, join_date)
VALUES (member_id_seq.NEXTVAL, 'Midori', 'Nagayama',
'68 Via Centrale', 'Sao Paolo', '254-852-5764', TO_DATE('17-JUN1991',
'DD-MM-YYYY'))
/
INSERT INTO member(member_id, first_name, last_name,
address, city, phone, join_date)
VALUES (member_id_seq.NEXTVAL, 'Mark', 'Quick-to-See',
'6921 King Way', 'Lagos', '63-559-7777', TO_DATE('07-APR-1990',
'DD-MM-YYYY'))
/
INSERT INTO member(member_id, first_name, last_name,
address, city, phone, join_date)
VALUES (member_id_seq.NEXTVAL, 'Audry', 'Ropeburn',
'86 Chu Street', 'Hong Kong', '41-559-87', TO_DATE('18-JAN-1991',
'DD-MM-YYYY'))
/
INSERT INTO member(member_id, first_name, last_name,
address, city, phone, join_date)
VALUES (member_id_seq.NEXTVAL, 'Molly', 'Urguhart',
'3035 Laurier', 'Quebec', '418-542-9988', TO_DATE('18-JAN-1991',
'DD-MM-YYYY'));
/
COMMIT
SET VERIFY ON
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Case Study Solutions (continued)
INSERT
VALUES
/
INSERT
VALUES
/
INSERT
VALUES
/
INSERT
VALUES
/
INSERT
VALUES
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
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.
Note: Your results may be different.
CREATE VIEW title_avail AS
SELECT
t.title, c.copy_id, c.status, r.exp_ret_date
FROM
title t JOIN title_copy c
ON
t.title_id = c.title_id
FULL OUTER JOIN rental r
ON
c.copy_id = r.copy_id
AND
c.title_id = r.title_id;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Case Study Solutions (continued)
SELECT
*
FROM
title_avail
ORDER BY title, copy_id;
an
r
t
n
Nu
l
b
a
fer
ns
e
c
li
no
a
has uide
)
om
t G Velasquez, who wants to rent
c
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
Additional Practices: Case Study Solutions (continued)
WHERE title_id = &title_id;
SET VERIFY OFF
SET ECHO OFF
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.
SET ECHO OFF
SET VERIFY OFF
SELECT m.first_name||' '||m.last_name MEMBER, t.title,
r.book_date, r.act_ret_date - r.book_date DURATION
FROM
member m, title t, rental r
WHERE
r.member_id = m.member_id
AND
r.title_id = t.title_id
ORDER BY member;
SET VERIFY ON
SET ECHO ON
t
a
y
da
l
b
a
fer
ns
e
c
li
an
r
t
n
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Universitas Gunadarma
l
b
a
fer
ns
e
c
li
an
r
t
n
t
a
y
da
no
a
has uide
)
om nt G
c
o ude
o
h
t
a
S
y
s
7@ e thi
0
0
at o us
y
a
t
(d
ur H
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.