Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
s
a
h
s)
e
a ide
u
g
u
ta 11g:
G
s
t
Oracle Database
SQL
e
n
g
e
Fundamentals
ra@ StudI
r
a
leg e this
i
(
ez oVolume
l
us III Student Guide
z
t
on nse
G
ce
ra
i
r
l
a
eg rable
L
de nsfe
o
Iig on-tra
n
D49996GC11
Edition 1.1
April 2009
D59982
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
Authors
Puja Singh
Brian Pottle
Disclaimer
Technical Contributors
and Reviewers
Claire Bennett
Tom Best
Purjanti Chang
Ken Cooper
Lszl Czinkczki
Burt Demchick
Mark Fleming
Gerlinde Frenzen
Nancy Greenberg
Chaitanya Koratamaddi
Wendy Lo
Timothy Mcglue
Alan Paulson
Bryan Roberts
Abhishek Singh
Lori Tritz
Michael Versaci
Lex van der Werff
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
Editors
ez o us
l
Raj Kumar
nz se t
Amitha Narayan
o
Vijayalakshmi Narasimhana G
en
c
r
i
r
l
a ble
g
e
Graphic Designer
e L sfera
d
Satish
Bettegowda
o
n
Iig on-tra
n
Publishers
Sujatha Nagendra
Syed Ali
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
Contents
Preface
I
Introduction
Lesson Objectives I-2
Lesson Agenda I-3
Course Objectives I-4
Course Agenda I-5
Appendixes Used in the Course I-7
Lesson Agenda I-8
Oracle Database 11g: Focus Areas I-9
Oracle Database 11g I-10
Oracle Fusion Middleware I-12
Oracle Enterprise Manager Grid Control 10g I-13
Oracle BI Publisher I-14
Lesson Agenda I-15
Relational and Object Relational Database Management Systems I-16
Data Storage on Different Media I-17
Relational Database Concept I-18
Definition of a Relational Database I-19
Data Models I-20
Entity Relationship Model I-21
Entity Relationship Modeling Conventions I-23
Relating Multiple Tables I-25
Relational Database Terminology I-27
Lesson Agenda I-29
Using SQL to Query Your Database I-30
SQL Statements I-31
Development Environments for SQL I-32
Lesson Agenda I-33
The Human Resources (HR) Schema I-34
Tables Used in the Course I-35
Lesson Agenda I-36
Oracle Database 11g Documentation I-37
Additional Resources I-38
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
iii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
Summary I-39
Practice I: Overview I-40
1
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
iv
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o tran
Iig on-Using
the VERIFY Command 2-36
n
Quiz 2-37
Summary 2-38
Practice 2: Overview 2-39
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
enConditional Expressions
4 Using Conversion rFunctions
a G licand
r
a ble
g
Objectives e
4-2
L f4-3
Lesson
eAgenda
era
d
s
o traFunctions
n
Conversion
4-4
IigImplicit
non Data Type Conversion 4-5
vi
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
5 Reporting Aggregated Data Using the Group Functions
e
a ide
Objectives 5-2
u
g
ta t Gu
Lesson Agenda 5-3
s
e
g den
What Are Group Functions? 5-4
@
ra Stu
r
Types of Group Functions 5-5
a
g this
Group Functions: Syntax 5-6 (ile
z 5-7use
e
l
Using the AVG and SUM Functions
to5-8
nzFunctions
e
Using the MIN and MAX
o
s
GFunction
n5-9
e
a
c
Using the COUNT
r
r le li
a
g
Using
LethefDISTINCT
ab Keyword 5-10
r
e
e
Functions
and Null Values 5-11
s
o d Group
n
g
a
i
r
t Agenda 5-12
I on-Lesson
n Creating Groups of Data 5-13
vii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
Using Table Aliases with the USING Clause 6-14
s)
e
a ide
Creating Joins with the ON Clause 6-15
u
g
ta t Gu
Retrieving Records with the ON Clause 6-16
s
e
n
e
Creating Three-Way Joins with the ON Clause 6-17 @g
d
a Stu
Applying Additional Conditions to a Join 6-18 arr
leg e this
Lesson Agenda 6-19
i
(
z
Joining a Table to Itself 6-20 le
us
o
z
t
Self-Joins Using the ON Clause
on 6-21
se
G
n
e
Lesson Agenda 6-22
rra le lic
a
g
Nonequijoinse 6-23
LRecords
abNonequijoins 6-24
r
e
Retrieving
with
e
f
s 6-25
o d Agenda
n
Lesson
g
a
i
r
-t Records with No Direct Match Using OUTER Joins 6-26
I Returning
n
o
n
viii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
Using the ALL Operator in Multiple-Row
his 7-19
leg Subqueries
i
t
(
e
Lesson Agenda 7-20
ez o us
l
t
Null Values in a Subquery
nz 7-21
e
o
s
Quiz 7-23 a G
en
c
r
i
r
l
a 7-24ble
Summary
g
e
L 7:feOverview
ra
7-25
ePractice
d
s
o tran
Iig o8n-Using
the Set Operators
n
Objectives 8-2
Lesson Agenda 8-3
Set Operators 8-4
Set Operator Guidelines 8-5
The Oracle Server and Set Operators 8-6
Lesson Agenda 8-7
Tables Used in This Lesson 8-8
Lesson Agenda 8-12
UNION Operator 8-13
Using the UNION Operator 8-14
UNION ALL Operator 8-16
Using the UNION ALL Operator 8-17
Lesson Agenda 8-18
INTERSECT Operator 8-19
ix
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
9 Manipulating Data
s)
e
a ide
Objectives 9-2
u
g
ta t Gu
Lesson Agenda 9-3
s
e
g den
Data Manipulation Language 9-4
@
ra Stu
r
Adding a New Row to a Table 9-5
a
leg e this
i
INSERT Statement Syntax 9-6
(
ez o us
l
Inserting New Rows 9-7
nz 9-8se t
Inserting Rows with Null Values
o
a G9-9licen
Inserting Special Values
r
r
aDate bandleTime Values 9-10
g
Inserting Specific
e
ra
e aLScript
Creating
e9-11
f
d
s
o tRows
an from Another Table 9-12
r
IigCopying
Lesson
non Agenda 9-13
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
FOR UPDATE Clause: Examples 9-44
s
e
g den
Quiz 9-46
@
ra Stu
r
Summary 9-47
a
leg e this
i
Practice 9: Overview 9-48
(
ez o us
l
xi
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
11 Creating Other Schema Objects
@
ra Stu
r
Objectives 11-2
a
leg e this
Lesson Agenda 11-3
i
(
ez o us
Database Objects 11-4
l
nz se t
What Is a View? 11-5
o
G cen
Advantages of Views
a 11-6
r
r
li
a
e
Simple Views g
and
Complex
Views 11-7
l
e rab
Creating
aLView fe
11-8
e
d
s
o traData
n from a View 11-11
Retrieving
IigModifying
non a View 11-12
xii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
Appendix A: Practice Solutions
ta t Gu
s
e
g den
@
Appendix B: Table Descriptions
ra Stu
r
a
leg e this
i
(
Appendix C: Oracle Join Syntax
ez o us
l
Objectives C-2
nz se t
o
G Multiple
Obtaining Data from
enTables C-3
a
c
r
i
r
l
CartesianaProductse C-4
eg araCartesian
bl Product C-5
L
Generating
e
deTypesnofsfOracle-Proprietary
Joins C-6
o
g
a
i
r
t
xiii
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
Summary C-22
Practice C: Overview C-23
Appendix D: Using SQL*Plus
Objectives D-2
SQL and SQL*Plus Interaction D-3
SQL Statements Versus SQL*Plus Commands D-4
Overview of SQL*Plus D-5
Logging In to SQL*Plus D-6
Displaying Table Structure D-8
SQL*Plus Editing Commands D-10
Using LIST, n, and APPEND D-12
s
a
h
s)
e
a ide
u
g
ta t Gu
SERVEROUTPUT Command D-17
s
e
g den
Using the SQL*Plus SPOOL Command D-18
@
ra Stu
r
Using the AUTOTRACE Command D-19
a
leg e this
Summary D-20
i
(
ez o us
l
nz se t
Appendix E: Using SQL Developer
o
Objectives E-2
a G licen
r
r
a Developer?
g
le
What Is Oracle
SQL
E-3
e
b
L
a
Specifications
e ofsfSQL
er Developer E-4
d
o trSQL
n
Installing
E-5
- a Developer
IigSQLonDeveloper
1.2 Interface E-6
n
Using the CHANGE Command D-13
SQL*Plus File Commands D-14
Using the SAVE, START, and EDIT Commands D-15
xiv
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Additional Practices
xv
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
__________________
Additional Practices
__________________
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
Additional Practices
These exercises can be used for extra practice after you have discussed the following topics:
Basic SQL SELECT statement, basic SQL Developer commands, and SQL functions.
1. The HR department needs to find data for all of the clerks who were hired after the year
1997.
2. The HR department needs a report of employees who earn commission. Show the last name,
job, salary, and commission of those employees. Sort the data by salary in descending order.
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
5. Show those employees who have a last nameestarting
histhe letters J, K, L, or M.
l g e twith
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n a report that displays all employees, and indicate with the words Yes or No whether
6. Create
they receive a commission. Use the DECODE expression in your query.
Note: Results are continued on the next page.
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
8. Find the number of employees who have a last name that ends with the letter n. Create two
possible solutions.
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
g den
@
tu
ra ofSemployees
r
9. Create a report that shows the name, location, and
number
for each department.
a
s
g
i
Make sure that the report also includes departments
th employees.
(ile sewithout
z
le to u
z
on nse
G
ce
ra
i
r
l
a
eg rable
L
de nsfe
o
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
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.
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
nsalary, and salary
g last name,
e
13. Create a report that displays the following for all employees:
@
d
ra Stu
r
expressed in terms of thousands of dollars.
a
leg e this
i
(
ez page.
Note: Results are continued on the
next
l
us
o
z
t
on nse
G
ce
ra
i
r
l
a
eg rable
L
de nsfe
o
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
h the
14. Show all the employees who have managers with a salary higher than $15,000.
)Show
s
e
following data: employee name, manager name, manager salary, and salary
a gradeidofethe
u
g
manager.
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
)h
s
e
a ide
u
g
u
taof thet department
16. Create a report to display the department number and lowest salary
with
G
s
e
n
g de
the highest average salary.
@
a
tu
r
r
S
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
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:
s
a
h
s)
e
a ide
a. Employs fewer than three employees:
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
z et
nemployees:
o
b. Has the highest number
of
G cens
a
r
r le li
a
g
Le ferab
e
o d trans
g
i
I c. oHas
n n the lowest number of employees:
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
the subject
of
set up for
TITLE
#* id
* title
* description
o rating
o category
o release date
s
a
h
available
s) as
e
a ide
u
g
ta t Gu
s
e
gcopyden
a
@
responsible
ra Stu
r
a
for
leg e this TITLE_COPY
i
(
ez o us
#* id
l
MEMBER
z
t
* status
#* id
on nse
G
* last namera
ce
i
r
l
the subject of
a
o first name
e
g
l
e
b
L fera responsible
oeaddress
d
o o tcity
ns
for
made against
g
a
i
r
I ono- phone
n * 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. All the three sql scripts are present in the D:\labs\sql1\labs folder.
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.
Cas Training
MEMBER_
ID
Key
Type
Null/
Unique
Default
Value
PK
Data
Type
Length
NUMBER
NN,U
LAST_
NAME
FIRST_NAME
ADDRESS
CITY
PHONE
NN
JOIN
_
DATE
NN
System
Date
DATE
a
s
a
)h
s
10
25
25
100
30
15
e
a ide
u
g
ta t Gu
s
e
g den
@
b. Table name: TITLE
ra Stu
r
a
TITLE_ID TITLE
DESCRIPTION
CATEGORY
RELEASE_
Column_
his
leg e tRATING
i
(
DATE
z
Name
s
e
l
u
z e to
PK
Key
n
o
Type
G
ns
NN,U rra NN lice NN
Null/
a ble
Unique
g
e
G, PG, R,
DRAMA,
Check e L
era
f
d
s
NC17,
NR
COMEDY,
o tran
g
i
ACTION,
I onCHILD,
n
SCIFI,
VARCHAR2
VARCHAR2
VARCHAR2
VARCHAR2
VARCHAR2
Data Type
NUMBER
VARCHAR2
VARCHAR2
VARCHAR2
DOCUMEN
TARY
VARCHAR2
Length
10
60
400
20
DATE
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
COPY_ID
TITLE_ID
PK
PK,FK
NN,U
NN,U
STATUS
NN
AVAILABLE,
DESTROYED,
RENTED,
RESERVED
FK Ref
Table
FK Ref
Col
Data
Type
Length
TITLE
s
a
h
s)
e
a ide
NUMBER
NUMBER
VARCHAR2
u
g
ta t Gu
s
e
10
10
g 15 den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
d. Table name: RENTAL
nz se t
o
G cenCOPY_ ACT_RET_ EXP_RET_ TITLE_
BOOK_
MEMBER_
Column
a
r
DATE
ID
DATE
DATE
ID
r
li ID
Name
a
e
g
l
b
PK,FK2
PK,FK2
Key
LePK feraPK,FK1
Typede
o
ns
g
System Date
Default traSystem
i
I Value
n
Date
+
2 days
no
FK Ref
Table
FK Ref
Col
Data
Type
Length
TITLE_ID
MEMBER
DATE
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.
Cas Training
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
a
s
a
)h
s
e
a ide
u
g
u
ta t GNavigator
s
2. Verify that the tables were created properly by checking in the
Connections
in
e
n
g
e
SQL Developer.
ra@ Stud
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Length
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
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.
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
The Glob
My Day Off
Description
All of Willies friends make a
Christmas list for Santa, but
Willie has 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
a
s
a
)h
PG
DRAMA
12-SEP-1995
Miracles on Ice
s
e
a ide
u
g
ta t Gu
s
e
g den 01-JUN-1995
Soda Gang
NRa@ ACTION
tu
r
r
S
a
leg e this
i
(
ez o us
l
z et
ntable.
o
b. Add data to the MEMBER
s the insert statements in a script named
G cenSave
a
r
lab_apcs_4b.sql.
Execute
commands
in the script. Be sure to use the sequence to
r le li
a
g
add the member
e rnumbers.
L
ab
e
e
f
First_d
o transLast_Name Address
g
Name
City
Phone
Join_Date
i
I Carmen
n
Velasquez
283 King
Seattle
206-899-6666 08-MAR-1990
no
LaDoris
Ngao
Midori
Nagayama
Mark
Quick-to-See
Audry
Molly
Street
5 Modrany
Bratislava
586-355-8882
08-MAR-1990
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
Hong Kong
41-559-87
18-JAN-1991
Urguhart
3035 Laurier
Quebec
418-542-9988
18-JAN-1991
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
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
1
2
s
a
h
2
AVAILABLE
2
s)
e
a3 ide
u
3
RENTED
g
ta t Gu
s
e
Miracles on Ice
1
AVAILABLE
Miraclesgon Ice en 1
ra@ Stud
r
a
Soda Gang
1
AVAILABLE
1
s
hiGang
leg eSoda
i
t
(
ez o us
l
t table:
ntozthe RENTAL
d. Add the following rentals
e
o
s
Note: The title number
be
endifferent depending on the sequence number.
a G may
c
r
i
r
l
a
g
Title_ Id
Copy_ bleMember_Id
e
Book_date
Exp_Ret_Date
e L Idsfera
d
92o
1
101
3
days
ago
1 day ago
n
g
a
i
r
t
I
93
2
101
1 day ago
1 day from now
non
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.
Cas Training
a
s
a
)h
6. Make changes to the data in the tables.
s
e
a ide
u
g
a. Add a new title. The movie is Interstellar Wars, which is rated
classified
as a
u
ta PG tand
G
s
science fiction movie. The release date is 07-JUL-77. The
description
is
Futuristic
e
g fromdtheenevil empire? Be sure
@
interstellar action movie. Can the rebels save the a
humans
tu
r
S
to add a title copy record for two copies. gar
le e this
i
(
b. Enter two reservations. One reservation
who wants to rent
ez ois for
l
usCarmen Velasquez,
z
Interstellar Wars. The other
is
for
Mark
Quick-to-See,
who
wants
to rent Soda Gang.
t
n se
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
g den
@
Title
Price
ra Stu
r
a
Willie and Christmas Too
25 eg
his
l
i
t
(
e
Alien Again
ez35 o us
l
The Glob
nz se35 t
o
G cen 35
My Day Off
a
r
r
li
Miracles on
Ice
30
a
e
g
l
e
b
Soda Gang
35
e L sfera
d
29
o Interstellar
n Wars
g
a
i
r
t
I
no
n
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.
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
__________________
Additional Practices:
Solutions
__________________
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
2. The HR department needs a report of employees who earn commission. Show the last name,
job, salary, and commission of those employees. Sort the data by salary in descending order.
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
nraises. The report
gon projected
e
3. For budgeting purposes, the HR department needs a report
@
d
tuwho have a 10% raise in
ra Sbut
r
should display those employees who do not get aacommission
salary (round off the salaries).
leg e this
i
(
ez o usafter a 10% raise is '
l
SELECT 'The salary of '||last_name||'
t salary"
nz se"New
|| ROUND(salary*1.10)
o
FROM
employees
en
a GIS liNULL;
c
r
r
WHERE commission_pct
a ble
g
e
L fera
ea report
4. Create
of employees and their duration of employment. Show the last names of all
d
s
o
n
atogether with the number of years and the number of completed months that they
tremployed.
Iigemployees
n
have
been
Order the report by the duration of their employment. The employee
o
n
who has been employeed the longest should appear at the top of the list.
SELECT last_name, job_id, salary, commission_pct
FROM
employees
WHERE commission_pct IS NOT NULL
ORDER BY salary DESC;
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;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
6. Create a report that displays all 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;
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
)h
s
e
a ide
u
g
SELECT COUNT(*)
ta t Gu
s
FROM
employees
e
g den
WHERE last_name LIKE '%n';
@
ra Stu
--or
r
a
SELECT COUNT(*)
leg e this
i
(
FROM
employees
e= z'n';o us
l
WHERE SUBSTR(last_name, -1)
nz se t
o
enlocation, and number of employees for each department.
9. Create a report that rshows
a Gtheliname,
c
r
a reportblalso
Make sure thatgthe
e includes departments without employees.
e
L
a
e sfer
SELECT dd.department_id,
d.department_name,
o
n
d.location_id,
COUNT(e.employee_id)
tra
ig nemployees
IFROM
e RIGHT OUTER JOIN departments d
ON no e.department_id = d.department_id
8. Find the number of employees who have a last name that ends with the letter n. Create two
possible solutions.
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.
SELECT DISTINCT job_id
FROM
employees
WHERE department_id IN (10, 20);
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;
Oracle Database 11g: SQL Fundamentals I Additional Practices Solutions - 4
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
13. Create a report that displays the following for all employees: last name, salary, and salary
expressed in terms of thousands of dollars.
a
s
a
)h
s
e
a ide
u
g
14. Show all employees who have managers with a salary higher thanta
$15,000. Show
u the
G
s
t
e
following data: employee name, manager name, manager salary,
salary
n grade of the
g and
e
@
d
manager.
ra Stu
r
a
SELECT e.last_name, m.last_name manager,
j.grade_level
his
leg e m.salary,
i
t
(
FROM
employees e JOIN employees
m
ez o us
l
ON
e.manager_id = m.employee_id
nz se t
JOIN
job_grades j
o
G j.lowest_sal
n
ON
m.salary BETWEEN
AND j.highest_sal
e
a
c
r
i
r
l
AND
m.salarya> 15000;
eg rable
L
e department
fe number, name, number of employees, and average salary of all
15. Showdthe
s
o
n
tra together with the names, salaries, and jobs of the employees working in each
Iigdepartments,
n
department.
no
SELECT last_name, salary, TRUNC(salary, -3)/1000
FROM
employees;
Thousands
SELECT
d.department_id, d.department_name,
count(e1.employee_id) employees,
NVL(TO_CHAR(AVG(e1.salary), '99999.99'), 'No average' ) avg_sal,
e2.last_name, e2.salary, e2.job_id
FROM
departments d RIGHT OUTER JOIN employees e1
ON
d.department_id = e1.department_id
RIGHT OUTER JOIN employees e2
ON
d.department_id = e2.department_id
GROUP BY d.department_id, d.department_name, e2.last_name, e2.salary,
e2.job_id
ORDER BY d.department_id, employees;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
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
WHERE job_id = 'SA_REP'
AND department_id IS NOT NULL);
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
g den
@
18. Create the following statistical reports for the HR department:
tu the department
ra SInclude
r
a
number, department name, and the number lof
egemployees
hisworking in each department that:
i
t
(
e
ez o us
l
a. Employs fewer than three employees:
z et
nd.department_name,
o
SELECT d.department_id,
COUNT(*)
G cens
a
r
FROM
departments
d
JOIN
employees
e
r le= lie.department_id
a
g
ON
d.department_id
Le ferab
GROUP BYed.department_id,
d.department_name
d
s
HAVING
COUNT(*)
<
3;
o
n
Iig on-tra
b.n Has the highest number of 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(*) = (SELECT MAX(COUNT(*))
FROM
employees
GROUP BY department_id);
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
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;
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
n highest number of
20. Show all employees who were hired on the day of the weekgon which
the
e
@
d
employees were hired.
ra Stu
r
a
s
hiday
leg'DAY')
SELECT last_name, TO_CHAR(hire_date,
i
t
(
e
z
FROM
employees
e
l
us
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
b. Table name: TITLE
ta t Gu
s
e
g den
CREATE TABLE title
@
(title_id
NUMBER(10)
tu
rra sKEY,
S
CONSTRAINT title_title_id_pkga
PRIMARY
e
hi
t
title
VARCHAR2(60)(il
e
ez oNOT
CONSTRAINT title_title_nn
l
usNULL,
z
t
description
VARCHAR2(400)
on nse
CONSTRAINT title_description_nn
NOT NULL,
G
e
a VARCHAR2(4)
c
r
rating
i
r
l
a btitle_rating_ck
g
le
CONSTRAINT
CHECK
e
L
a
r
(rating
IN
('G',
'PG',
'R',
e sfe VARCHAR2(20) 'NC17', 'NR')),
d
category
o ran
title_category_ck CHECK
Iig on-tCONSTRAINT
(category
IN
('DRAMA', 'COMEDY', 'ACTION',
n 'CHILD', 'SCIFI',
'DOCUMENTARY')),
release_date
DATE);
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
)h
s
e
e. Table name: RESERVATION
a ide
u
g
ta t Gu
CREATE TABLE reservation
s
e
(res_date
DATE,
g den
@
member_id
NUMBER(10)
tu member(member_id),
raREFERENCES
r
S
a
CONSTRAINT reservation_member_id
g this
title_id
NUMBER(10) (ile
e REFERENCES title(title_id),
z
s
CONSTRAINT reservation_title_id
e
l
u
CONSTRAINT reservation_resdate_mem_tit_pk
PRIMARY KEY
z e to
n
o
s
(res_date, member_id,
title_id));
a G licen
r
r
a blecreated properly by checking in the Connections Navigator in
g
2. Verify that the
tables were
e
SQL d
Developer.
e L sfera
o tran
- Connections Navigator, expand Connections > myconnection > Tables.
Iiga. oInnthe
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
b. Title number for the TITLE table: Start with 92; do not allow caching of the values.
Name the sequence TITLE_ID_SEQ.
CREATE SEQUENCE title_id_seq
START WITH 92
NOCACHE;
a
s
a
)h
s
e
Developer.
c. Verify the existence of the sequences in the Connections Navigator in
aSQL
e
u
d
i
g
u
ta t G
s
a. In the Connections Navigator, assuming that the myconnection
node is expanded,
e
g den
expand Sequences.
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
...
COMMIT
/
SELECT
FROM
title
title;
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
INTO title_copy(copy_id, title_id, status)
s)
e
(1, 95, 'AVAILABLE')
a ide
u
g
u
a
INTO title_copy(copy_id, title_id,status) st
G
t
e
g den
(2, 95, 'AVAILABLE')
@
ra Stu
r
a
INTO title_copy(copy_id, title_id,status)
leg e this
i
(3, 95, 'RENTED')
(
ez o us
l
t
INTO title_copy(copy_id,
nz setitle_id,status)
o
(1, 96, 'AVAILABLE')
a G licen
r
r
a ble
g
INTO e
title_copy(copy_id,
title_id,status)
L
a
r
(1,
97,
'AVAILABLE')
de nsfe
o
I/ig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
ilegtheemovie
thistitles, the availability of
5. Create a view named TITLE_AVAILz
to (show
le if rented.
usQuery all rows from the view. Order the
each copy, and its expected return
date
o
z
t
results by title.
on nse
G
e
a
c
r
i
r
l
a
Note: Your results
beledifferent.
eg may
b
L
a
e title_avail
CREATE dVIEW
AS
er
f
s
o
n
SELECT
t.title,
c.copy_id,
c.status, r.exp_ret_date
g -tratitle t JOIN title_copy
IiFROM
n t.title_id = c.title_id c
ONno
FULL OUTER JOIN rental r
ON
c.copy_id = r.copy_id
AND
c.title_id = r.title_id;
SELECT
*
FROM
title_avail
ORDER BY title, copy_id;
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
a
s
a
)h
s
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
b. Enter two reservations. One reservationilis
egfor Carmen
hisVelasquez, who wants to rent
t
(
e
Interstellar Wars. The other is e
for
who wants to rent Soda Gang.
zMark Quick-to-See,
s
l
u
o
z e t member_id, title_id)
INSERT INTO reservationon
(res_date,
VALUES (SYSDATE, 101,G98) ens
rra le lic
/
a
g
INSERT INTO e
reservation
b (res_date, member_id, title_id)
L
a
r
VALUES d(SYSDATE,
104,
97)
e sfe
o
n
/
Iig on-tra
n a modification to one of the tables.
7. Make
a. Run the script lab_apcs_7a.sql located in D:\labs\sql1\labs folder, to
add a PRICE column to the TITLE table to record the purchase price of the video.
Verify your modifications.
ALTER TABLE title
ADD (price NUMBER(8,2));
DESCRIBE title
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
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.
s
a
h
s)
e
a ide
u
g
u
a
est ent G
SELECT m.first_name||' '||m.last_name MEMBER,gt.title,
r.book_date, r.act_ret_date - r.book_date
d
ra@ StuDURATION
r
FROM member m
a
JOIN rental r
leg e this
i
(
ON
r.member_id = m.member_id
ez o us
l
JOIN title t
nz se t
ON
r.title_id = t.title_id
o
en
ORDER BY member; ra G
c
i
r
l
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.
Cas Training
s
a
h
s)
e
a ide
u
g
ta t Gu
s
e
g den
@
ra Stu
r
a
leg e this
i
(
ez o us
l
nz se t
o
a G licen
r
r
a ble
g
e
e L sfera
d
o
n
Iig on-tra
n
Unauthorized reproduction or distribution prohibited. Copyright 2009, Oracle and/or its affiliates.