Escolar Documentos
Profissional Documentos
Cultura Documentos
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
l c om
Oracle Database 11g:
t m ai SQL ide
Fundamentals hI o G u
@ n t
5 _ 95 tude
c k 2 Student
i s S
i
Volume III
h Guide
z (er use t
D a to
d e z nse
n lice
r d o M rable
e ra sfe
k G tran
c
Er non-
i
D49996GC11
Edition 1.1
April 2009
D59982
TI TECNOEDUCACION MEXICO SC
Brian Pottle This document contains proprietary information and is protected by copyright and
other intellectual property laws. You may copy and print this document solely for your
Technical Contributors own use in an Oracle training course. The document may not be modified or altered in
any way. Except where your use constitutes "fair use" under copyright law, you may
and Reviewers not use, share, download, upload, copy, print, display, perform, reproduce, publish,
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Claire Bennett license, post, transmit, or distribute this document in whole or in part without the
express authorization of Oracle.
Tom Best
Purjanti Chang The information contained in this document is subject to change without notice. If you
Ken Cooper find any problems in the document, please report them in writing to: Oracle University,
Lszl Czinkczki 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
warranted to be error-free.
Burt Demchick
Mark Fleming Restricted Rights Notice
Gerlinde Frenzen
Nancy Greenberg If this documentation is delivered to the United States Government or anyone using
the documentation on behalf of the United States Government, the following notice is
Chaitanya Koratamaddi applicable:
Wendy Lo
Timothy Mcglue U.S. GOVERNMENT RIGHTS
s a
) ha
Alan Paulson The U.S. Governments rights to use, modify, reproduce, release, perform, display, or
disclose these training materials are restricted by the terms of the applicable Oracle
Bryan Roberts
Abhishek Singh
l om
license agreement and/or the applicable U.S. Government contract.
c
Lori Tritz Trademark Notice
t m ai ide
Michael Versaci
Lex van der Werff h o t Gu
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other
_ 9 5@ uden
names may be trademarks of their respective owners.
Editors
k 2 5 s St
Raj Kumar r ic t h i
Amitha Narayan e
z ( o use
Vijayalakshmi Narasimhan a
D et
d e z n s
Graphic Designer n
e lic e
Satish Bettegowda o M
a r d erabl
G er nsf
r c k n-tra
Publishers
i
E SujathanNagendra
o
Syed Ali
TI TECNOEDUCACION MEXICO SC
Contents
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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 s a
Oracle Database 11g: Focus Areas I-9 ) ha
Oracle Database 11g I-10 l c om
Oracle Fusion Middleware I-12 t m ai ide
h o t Gu
Oracle Enterprise Manager Grid Control 10g I-13
Oracle BI Publisher I-14 _ 9 5@ uden
k 2 5 s St
Lesson Agenda I-15
r ic t h i
e
z ( o use
Relational and Object Relational Database Management Systems I-16
a
Data Storage on Different Media I-17
D et
e z s
Relational Database Concept I-18
d n
n e
M ble lic
Definition of a Relational Database I-19
o
d
rar sfera
Data Models I-20
eEntity Relationship Model I-21
G tran
c k
Er non-
i 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
iii
TI TECNOEDUCACION MEXICO SC
Summary I-39
Practice I: Overview I-40
Objectives 1-2
Lesson Agenda 1-3
Capabilities of SQL SELECT Statements 1-4
Basic SELECT Statement 1-5
Selecting All Columns 1-6
Selecting Specific Columns 1-7
Writing SQL Statements 1-8
Column Heading Defaults 1-9
Lesson Agenda 1-10
Arithmetic Expressions 1-11 s a
Using Arithmetic Operators 1-12 ) ha
Operator Precedence 1-13 l c om
Defining a Null Value 1-14 t m ai ide
Null Values in Arithmetic Expressions 1-15 h o t Gu
Lesson Agenda 1-16 _ 9 5@ uden
Defining a Column Alias 1-17 k 2 5 s St
r ic t h i
Using Column Aliases 1-18 e
z ( o use
Lesson Agenda 1-19 a
D et
e z
Concatenation Operator 1-20
d n s
n e
M ble lic
Literal Character Strings 1-21
o
d
Using Literal Character Strings 1-22
e rar sfera
Alternative Quote (q) Operator 1-23
k G tran
c
Er non-
i Duplicate Rows 1-24
Lesson Agenda 1-25
Displaying the Table Structure 1-26
Using the DESCRIBE Command 1-27
Quiz 1-28
Summary 1-29
Practice 1: Overview 1-30
iv
TI TECNOEDUCACION MEXICO SC
v
TI TECNOEDUCACION MEXICO SC
vi
TI TECNOEDUCACION MEXICO SC
s a
) ha
5 Reporting Aggregated Data Using the Group Functions
Objectives 5-2
l c om
Lesson Agenda 5-3
t m ai ide
What Are Group Functions? 5-4
h o t Gu
Types of Group Functions 5-5
_ 9 5@ uden
Group Functions: Syntax 5-6
k 2 5 s St
Using the AVG and SUM Functions 5-7
r ic t h i
Using the MIN and MAX Functions e
z (5-8o use
Using the COUNT FunctionD5-9 a t
e z s e
Using the DISTINCT
nd Keyword
l i c en5-10
M and
Group Functions
o b leNull Values 5-11
r d
a Agenda
Lesson a
er 5-12
e r s f
i c k GCreating
- t r anGroups of Data 5-13
Er noCreating
n Groups of Data: GROUP BY Clause Syntax 5-14
Using the GROUP BY Clause 5-15
Grouping by More than One Column 5-17
Using the GROUP BY Clause on Multiple Columns 5-18
Illegal Queries Using Group Functions 5-19
Restricting Group Results 5-21
Restricting Group Results with the HAVING Clause 5-22
Using the HAVING Clause 5-23
Lesson Agenda 5-25
Nesting Group Functions 5-26
Quiz 5-27
Summary 5-28
Practice 5: Overview 5-29
vii
TI TECNOEDUCACION MEXICO SC
viii
TI TECNOEDUCACION MEXICO SC
ix
TI TECNOEDUCACION MEXICO SC
s a
) ha
9 Manipulating Data
Objectives 9-2
l c om
Lesson Agenda 9-3
t m ai ide
Data Manipulation Language 9-4
h o t Gu
Adding a New Row to a Table 9-5
_ 9 5@ uden
INSERT Statement Syntax 9-6
k 2 5 s St
Inserting New Rows 9-7
r ic t h i
e
z ( o use
Inserting Rows with Null Values 9-8
a
D et
Inserting Special Values 9-9
d e z n s
Inserting Specific Date and Time Values 9-10
n e
M ble lic
Creating a Script 9-11
o
d
rar sfera
Copying Rows from Another Table 9-12
e
Lesson Agenda 9-13
G tran
c k
Er non-
i Changing Data in a Table 9-14
UPDATE Statement Syntax 9-15
Updating Rows in a Table 9-16
Updating Two Columns with a Subquery 9-17
Updating Rows Based on Another Table 9-18
Lesson Agenda 9-19
Removing a Row from a Table 9-20
DELETE Statement 9-21
Deleting Rows from a Table 9-22
Deleting Rows Based on Another Table 9-23
TRUNCATE Statement 9-24
Lesson Agenda 9-25
Database Transactions 9-26
Database Transactions: Start and End 9-27
x
TI TECNOEDUCACION MEXICO SC
xi
TI TECNOEDUCACION MEXICO SC
xii
TI TECNOEDUCACION MEXICO SC
xiii
TI TECNOEDUCACION MEXICO SC
Summary C-22
Practice C: Overview C-23
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
Using the CHANGE Command D-13
SQL*Plus File Commands D-14 s a
Using the SAVE, START, and EDIT Commands D-15 ) ha
SERVEROUTPUT Command D-17 l c om
Using the SQL*Plus SPOOL Command D-18 t m ai ide
h o t Gu
Using the AUTOTRACE Command D-19
_ 9 5@ uden
Summary D-20
k 2 5 s St
r ic t h i
Appendix E: Using SQL Developer
z e
( use
Objectives E-2
D e toa
d e
What Is Oracle SQL Developer? z E-3 ns
Specifications of SQL n
Developerl i c e
E-4
r
Installing SQL d o M rabE-5
Developer
le
e ra 1.2sfInterface
SQL Developer
e E-6
G
k na-Database
tr a n
cCreating
Eri Browsing
Connection E-7
o
n Database Objects E-10
Creating a Schema Object E-11
Creating a New Table: Example E-12
Using the SQL Worksheet E-13
Executing SQL Statements E-16
Saving SQL Scripts E-17
Executing Saved Script Files: Method 1 E-18
Executing Saved Script Files: Method 2 E-19
Executing SQL Statements E-20
Formatting the SQL Code E-21
Using Snippets E-22
Using Snippets: Example E-23
Using SQL*Plus E-24
xiv
TI TECNOEDUCACION MEXICO SC
Index
Additional Practices
s a
Additional Practices: Solutions ) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
xv
TI TECNOEDUCACION MEXICO SC
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
TI TECNOEDUCACION MEXICO SC
__________________
Additional Practices
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
__________________
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
TI TECNOEDUCACION MEXICO SC
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
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
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).
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
4. Create a report of employees and their length of employment. Show the last names of all the
employees together with the number of years and the number of completed months that they
have been employed. Order the report by the length of their employment. The employee who
has been employed the longest should appear at the top of the list.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
l c om
t m ai ide
h o t Gu
5. Show those employees who have a last name starting with the
_ 9 5@ u d enK, L, or M.
letters J,
k 2 5 s St
r ic t h i
e
z ( o use
D et a
d e z n s
n e
o M ble lic
d
e rar sfera
6. c k Ga report
Create t r n displays all employees, and indicate with the words Yes or No whether
athat
i -
Er theynreceive
on a commission. Use the DECODE expression in your query.
Note: Results are continued on the next page.
6. (continued)
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
These exercises can be used for extra practice after you have discussed the following topics:
Basic SQL SELECT statement, basic SQL Developer commands, SQL functions, joins, and
group functions.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
7. Create a report that displays the department name, location ID, last 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, these are the results:
8. Find the number of employees who have a last name that ends with the letter n. Create two
possible solutions.
s a
) ha
l c om
t m ai ide
h o forteach
G u
9. Create a report that shows the name, location, and number of employees
@ n department.
5 e
2 5 S tud
_9 employees.
Make sure that the report also includes departments without
e r ick this
a z ( o use
z D et
d e e n s
n
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
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.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
These exercises can be used for extra practice after you have discussed the following topics:
Basic SQL SELECT statements, basic SQL Developer commands, SQL functions, joins, group
functions, and subqueries.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
12. Show all the employees who were hired in the first half of the month (before the 16th of the
month).
s a
) ha
l c om
t m ai andisalary
de
13. Create a report that displays the following for all employees: last name,
o
h nt Gsalary, u
expressed in terms of thousands of dollars. @
5 _ 95 tude
Note: Results are continued on the next page. ck2 i s S
i h
z (er use t
D a to
d e z nse
n lice
r d o M rable
e ra sfe
k G tran
c
Er non-
i
13. (continued)
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
14. Show all the employees who have managers with a salary higher than $15,000. Show the s a
) ha
following data: employee name, manager name, manager salary, and salary grade of the
manager.
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
15. Show the department number, name, number of employees, and average salary of all the
departments, together with the names, salaries, and jobs of the employees working in each
department.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
l c om
16. Create a report to display the department number and lowest salary of the department with
the highest average salary. t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
17. Create a report that displays departments where no sales representatives work. Include the
department number, department name, manager ID, and the location in the output.
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:
s a
a. Employs fewer than three employees: ) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
b. Has the highest number of employees:
D eta
d e z n s
n e
o M ble lic
rd fera
ralowest
c. Hasethe snumber of employees:
G a n
ck tr
Eri non-
19. Create a report that displays the employee number, last name, salary, department number,
and the average salary in their department for all employees.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
i on which the highest number
c of thethweek
riday
e
20. Show all the employees who were hired on(the
z o use
of employees were hired.
D et a
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
21. Create an anniversary overview based on the hire date of the employees. Sort the
anniversaries in ascending order.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
c om
m
l
ai ide
h t
o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
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.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
The following is a diagram of the entities and attributes for the video application:
TITLE
for #* id
RESERVATION * title
#* res date the subject * description
of o rating
set up for o category
o release date
s a
) ha
available as
l c om
t m ai ide
responsible a copy o
h G u
for @ den
5TITLE_COPY t
5 _ 9 tu
2 S
MEMBER
e r ick this* status
#* id
#* id
a z ( o use
* last name
z D et the subject of
o first name
d e e n s
o address
M n responsible
e lic
o city o l
r a
o phone rd ferabfor made against
G *ejoin datea ns
r i c k n - t r RENTAL
E no created #* book date
for 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).
1. Create the tables based on the following table instance charts. Choose the appropriate data
types and be sure to add integrity constraints.
l c om
t m ai ide
b. Table name: TITLE
h o t Gu
Column_ _ 9 en RELEASE_
5@ uCATEGORY
d
5 s St
TITLE_ID TITLE DESCRIPTION RATING
Name
k 2 i
DATE
Key PK
( e ric e th
Type
a z o us
Null/ NN,U NN
z D et
NN
Unique
d e e n s
Check n
e lic G, PG, R, DRAMA,
M
o rabl NC17, NR COMEDY,
r d
e ra sfe ACTION,
G a n CHILD,
r i ck n-tr SCIFI,
E no DOCUMEN
TARY
Data Type NUMBER VARCHAR2 VARCHAR2 VARCHAR2 VARCHAR2 DATE
Length 10 60 400 4 20
Key PK PK,FK
Type
Null/ NN,U NN,U NN
Unique
Check AVAILABLE,
DESTROYED,
RENTED,
RESERVED
FK Ref TITLE
Table
FK Ref TITLE_ID
s a
) ha
Col
Data
om
NUMBER NUMBER VARCHAR2
Type l c
Length 10 10 15
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
d. Table name: RENTAL
r ic t h i
e
z ( oACT_RET_
se EXP_RET_ TITLE_
Column BOOK_ MEMBER_ a
COPY_ u
Name DATE ID z D ID e t DATE DATE ID
e n s
Key PK
nd licePK,FK2
PK,FK1 PK,FK2
Type
Default r d o M rable
System System Date
a
Value er Date sfe + 2 days
k G
Ref -tra
n MEMBER TITLE_
r i cFK
n
TITLE_
E Table n o COPY COPY
FK Ref MEMBER_I COPY_ TITLE_ID
Col D ID
Data DATE NUMBER NUMBER DATE DATE NUMBER
Type
Length 10 10 10
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 the
values. Name the sequence MEMBER_ID_SEQ.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
b. Title number for the TITLE table: Start with 92; do not allow caching of the values.
Name the sequence TITLE_ID_SEQ.
c. Verify the existence of the sequences in the Connections Navigator in SQL Developer.
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
D et a
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
i onto- the tables. Create a script for each set of data to be added.
E4.r Addndata
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.
wish list.
Alien Again Yet another installation of R SCIFI 19-MAY-1995
science fiction history. Can
the heroine save the planet
from the alien life form?
The Glob A meteor crashes near a small NR SCIFI 12-AUG-1995
American town and unleashes
carnivorous goo in this classic.
My Day Off With a little luck and a lot of PG COMEDY 12-JUL-1995
ingenuity, a teenager skips
school for a day in New York. a
s
) ha
Miracles on Ice A six-year-old has doubts PG DRAMA 12-SEP-1995
about Santa Claus, but she
discovers that miracles really
l c om
do exist.
t m ai ide
Soda Gang After discovering a cache of NR ACTION
h o t01-JUN-1995
G u
drugs, a young couple find
themselves pitted against a _ 9 5@ uden
vicious gang.
k 2 5 s St
r ic t h i
b. Add data to the MEMBER table. Savezthe
e
( insertustatements
se in a script named
lab_apcs_4b.sql. Execute D e a
commands t oin the script. Be sure to use the sequence to
e z s
nd licen
add the member numbers.
First_
Name r d o M rable Address
Last_Name City Phone Join_Date
e r a f e
Carmen
k ns
G traVelasquez 283 King Seattle 206-899-6666 08-MAR-1990
c - Street
Eri LaDoris
non Ngao 5 Modrany Bratislava 586-355-8882 08-MAR-1990
2 RENTED 2
2 AVAILABLE 2
s a
) ha
3 RENTED 3
l c om
i
Miracles on Ice 1 AVAILABLE Miracles on Ice
o t m 1a
u ide
@ h nt G
Soda Gang 1 AVAILABLE Soda Gang
_ 9 5 ude 1
k 2 5 s St
r ic t h i
d. Add the following rentals to the RENTAL e
z ( depending
table:
seon the sequence number.
a u
D e to
Note: The title number may be different
z
Title_ Id Copy_ Member_Idns
d e
Id n l i c e Book_date Exp_Ret_Date
92 1o
M b101 l e 3 days ago 1 day ago
a r d era
93 Ge
r 2 sf
i c k - t r an 101 1 day ago 1 day from now
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.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
t m ai ide
science fiction movie. The release date is 07-JUL-77. The description is Futuristic
o t Gu
interstellar action movie. Can the rebels save the humans from the evil empire? Be sure
h
to add a title copy record for two copies.
_ 9 5@ uden
2 S t who wants to rent
5 Velasquez,
ic k
b. Enter two reservations. One reservation is for Carmen
i s
th who wants to rent Soda Gang.
rQuick-to-See,
Interstellar Wars. The other is for Mark
( e
z o us e
D et a
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
s a
) ha
l c om
t m ai ide
Title Price h o t Gu
Willie and Christmas Too 25
_ 9 5@ uden
Alien Again 35
k 2 5 s St
The Glob 35 eric t h i
My Day Off a z35 ( o use
z D 30 e t
Miracles on Ice
d e e n s
n
Soda Gang
o M ble lic 29
InterstellardWars
35
e rar sfera
i c
G atscript
b.k Create
- r an named lab_apcs_7b.sql that contains update statements that update
Er each n onvideo 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.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
TI TECNOEDUCACION MEXICO SC
__________________
Additional Practices:
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Solutions
__________________
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
TI TECNOEDUCACION MEXICO SC
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
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
1997.
SELECT *
FROM employees
WHERE job_id = 'ST_CLERK'
AND hire_date > '31-DEC-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.
SELECT last_name, job_id, salary, commission_pct
s a
) ha
FROM employees
WHERE commission_pct IS NOT NULL
ORDER BY salary DESC;
l c om
t m ai Theidreport
e
3. For budgeting purposes, the HR department needs a report on projected o raises. u
should display those employees who do not get a commission @ but h
who haven taG10% raise in
salary (round off the salaries). 9 5
_ Stu d e
2 5
k ath10%
SELECT 'The salary of '||last_name||' ic
r after is raise is '
|| ROUND(salary*1.10) "New e
z ( o use
salary"
FROM employees
D eta
WHERE commission_pct IS NULL;
d e z n s
n e
c duration of employment. Show the last names of all
M bleandlitheir
4. Create a report of employees
o
e
have been
ard sfwith
employees together
remployed. rathe number of years and the number of completed months that they
eOrder the report by the duration of their employment. The employee
G
k hasnbeen a n
ricwho
ESELECT o -tr employeed the longest should appear at the top of the list.
nlast_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
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
These exercises can be used for extra practice after you have discussed the following topics:
Basic SQL SELECT statement, basic SQL Developer commands, SQL functions, joins, and
group functions.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
7. Create a report that displays the department name, location ID, name, job title, and salary of
those employees who work in a specific location. Prompt the user for the location.
8. Find the number of employees who have a last name that ends with the letter n. Create two
s a
) ha
possible solutions.
SELECT COUNT(*)
l c om
FROM employees
t m ai ide
WHERE last_name LIKE '%n';
h o t Gu
5@ uden
--or
SELECT COUNT(*)
_ 9
FROM employees
SUBSTR(last_name, -1) = 'n'; ick
25 is St
WHERE
( e r e th
a z and s
unumber
9. Create a report that shows the name,
z D location,
e t o of employees for each department.
d e n s
Make sure that the report alsoeincludes departments without employees.
n lic
d.department_name,
SELECT d.department_id,
o M l e
d.location_id,
r a rab COUNT(e.employee_id)
rd efeRIGHT
FROM
G e
employees
n s OUTER JOIN departments d
ON
i c k - t r a
e.department_id = d.department_id
Er non
GROUP BY d.department_id, d.department_name, d.location_id;
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;
These exercises can be used for extra practice after you have discussed the following topics:
Basic SQL SELECT statements, basic SQL Developer commands, SQL functions, joins, group
functions, and subqueries.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
12. Show all employees who were hired in the first half of the month (before the 16th of the
month).
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 Thousands
s a
) ha
FROM employees;
14. Show all employees who have managers with a salary higher than $15,000. Show the
l c om
t m ai ide
following data: employee name, manager name, manager salary, and salary grade of the
manager. h o t Gu
SELECT e.last_name, m.last_name manager, m.salary,
_ 9 en
5@ uj.grade_level
d
FROM employees e JOIN employees m
e.manager_id = m.employee_id ick
25 is St
ON
( e r e th
JOIN job_grades j
a z oANDusj.highest_sal
ON m.salary BETWEEN j.lowest_sal
m.salary > 15000; z D e t
AND
e s
nd licen
departments, r d o Mnumber,
15. Show the department
together r a
with b lename,
the names,
number of employees, and average salary of all
salaries, and jobs of the employees working in each
e r a f e
G trans
department.
k
ric od.department_id,
ESELECT n- d.department_name,
n 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;
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)
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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
s a
FROM employees
) ha
WHERE job_id = 'SA_REP'
l c om
AND department_id IS NOT NULL);
t m ai ide
h
18. Create the following statistical reports for the HR department: Include G u
o the department
number, department name, and the number of employees working 5@ inueach t
endepartment that:
_ 9
5 s St d
k 2 i
a. Employs fewer than three employees:
e r ic t h
SELECT d.department_id, d.department_name,
a z ( o useCOUNT(*)
FROM
z
departments d JOIN employees D e te
ON e
d.department_id d= e.department_id
e n s
GROUP BY d.department_id, n lic
d.department_name
HAVING COUNT(*)o<M 3; ble
d
e rar sfera
i
b. Has
c k - r an number of employees:
G the thighest
r
ESELECT on
FROM
nd.department_id, d.department_name, COUNT(*)
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);
ON d.department_id = e.department_id
GROUP BY d.department_id, d.department_name
HAVING COUNT(*) = (SELECT MIN(COUNT(*))
FROM employees
GROUP BY department_id);
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
s a
ON e.department_id = s.department_id
) ha
GROUP BY e.employee_id, e.last_name, e.department_id, e.salary;
l c om
m
20. Show all employees who were hired on the day of the week on which the
t ai number
highest ide of
employees were hired. o
h nt G u
@
95 tude
SELECT last_name, TO_CHAR(hire_date, 'DAY') _
FROM employees k 5 day
2 is S
WHERE TO_CHAR(hire_date, 'Day') = er i c th
(
z 'Day') us e
(SELECT TO_CHAR(hire_date,
FROM employees a
D e 'Day') t o
d e z
GROUP BY TO_CHAR(hire_date, n s
HAVING COUNT(*) n
e lFROM
= ic e
(SELECT MAX(COUNT(*))
M
o rabl GROUP BYemployees
r d
e ra sfe TO_CHAR(hire_date, 'Day')));
i
21. c k
Create - t r an overview based on the hire date of the employees. Sort the
Gan anniversary
Er anniversaries
non in ascending order.
SELECT last_name, TO_CHAR(hire_date, 'Month DD') BIRTHDAY
FROM employees
ORDER BY TO_CHAR(hire_date, 'DDD');
1. Create the tables based on the following table instance charts. Choose the appropriate data
types and be sure to add integrity constraints.
on-
Eri nrelease_date
'SCIFI', 'DOCUMENTARY')),
DATE);
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));
s a
e. Table name: RESERVATION ) ha
l c om
ai ide
CREATE TABLE reservation
(res_date DATE,
t m
o t Gu
member_id NUMBER(10) h
_ 9 5@ uden
CONSTRAINT reservation_member_id REFERENCES member(member_id),
title_id NUMBER(10)
k 2 5 s St
r ic t h i
CONSTRAINT reservation_title_id REFERENCES title(title_id),
e
z ( o use
CONSTRAINT reservation_resdate_mem_tit_pk PRIMARY KEY
(res_date, member_id, title_id));
D et a
2. Verify that the tables were d e z n s by checking in the Connections Navigator in
created properly
SQL Developer. M
n lic e
o l e
a. In the r a
Connections
ab expand Connections > myconnection > Tables.
rd ferNavigator,
G e ns
c k t r a
Eri non-
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 the
values. Name the sequence MEMBER_ID_SEQ.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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
c. Verify the existence of the sequences in the Connections Navigator in SQL Developer.
) has
c m
oexpanded,
l
ai ide
a. In the Connections Navigator, assuming that the myconnection node is
expand Sequences. t m
o t Gu
h
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
D eta
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
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
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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, s a
category, release_date)
) ha
VALUES (title_id_seq.NEXTVAL, 'Alien Again', 'Yet another
l c om
installment of science fiction history. Can the
t m ai ide
o t Gu
heroine save the planet from the alien life form?',
h
'R', 'SCIFI', TO_DATE( '19-MAY-1995','DD-MON-YYYY'))
/
_ 9 5@ uden
k 2 5 s St
INSERT INTO title(title_id, title, description, rating,
r ic
category, release_date)
t h i
VALUES e
z ( o use
(title_id_seq.NEXTVAL, 'The Glob', 'A meteor crashes
a
near a small American town and unleashes carnivorous
D et
d e z
goo in this classic.', 'NR', 'SCIFI',
n s
e
TO_DATE( '12-AUG-1995','DD-MON-YYYY'))
n
/
o M ble lic
d
INSERT INTO title(title_id, title, description, rating,
e rar sfera
category, release_date)
VALUES
k G tran
(title_id_seq.NEXTVAL, 'My Day Off', 'With a little
c
Er non-
i 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'))
/
INSERT INTO title(title_id, title, description, rating,
category, release_date)
VALUES (title_id_seq.NEXTVAL, 'Miracles on Ice', 'A six-year-old has
doubts about Santa Claus, but she discovers that miracles really do
exist.', 'PG', 'DRAMA',
TO_DATE('12-SEP-1995','DD-MON-YYYY'))
/
INSERT INTO title(title_id, title, description, rating,
category, release_date)
VALUES (title_id_seq.NEXTVAL, 'Soda Gang', 'After discovering a cache
of drugs, a young couple find themselves pitted against a vicious gang.',
'NR', 'ACTION', TO_DATE('01-JUN-1995','DD-MON-YYYY'))
/
...
COMMIT
/
SELECT title
FROM title;
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
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.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
r i ck OUTER
FULL n - traJOIN rental r
= c.title_id
E ON no c.copy_id = r.copy_id
AND c.title_id = r.title_id;
SELECT *
FROM title_avail
ORDER BY title, copy_id;
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
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
interstellar action movie. Can the rebels save the humans from the evil empire? Be sure
to add a title copy record for two copies.
INSERT INTO title(title_id, title, description, rating,
category, release_date)
VALUES (title_id_seq.NEXTVAL, 'Interstellar Wars',
'Futuristic interstellar action movie. Can the
rebels save the humans from the evil empire?',
'PG', 'SCIFI', '07-JUL-77')
/
INSERT INTO title_copy (copy_id, title_id, status)
s a
) ha
VALUES (1, 98, 'AVAILABLE')
/
INSERT INTO title_copy (copy_id, title_id, status)
l c om
VALUES (2, 98, 'AVAILABLE')
t m ai ide
/
h o t Gu
b. Enter two reservations. One reservation is for Carmen 9
_ 5@ udwho
Velasquez, enwants to rent
2 5 who
Interstellar Wars. The other is for Mark Quick-to-See, S t to rent Soda Gang.
wants
k i s
ric e thtitle_id)
INSERT INTO reservation (res_date,(e
VALUES (SYSDATE, 101, 98) a z o us
member_id,
/ z D et
d e e n s member_id, title_id)
INSERT INTO reservation
VALUES (SYSDATE, M
n (res_date,
97)e lic
104,
/
a r d erabl
o
e r sf to one of the tables.
7. Make G a a n
modification
ck n-tr
Eri a. Run
no 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
b. Create a script named lab_apcs_7b.sql that contains update statements that update
each video with a price according to the list provided. Run the commands in the script.
Note: Have the TITLE_ID numbers available for this exercise.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
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
s a
in a script file named lab_apcs_8.sql.
) ha
Note: Your results may be different.
l c om
SELECT m.first_name||' '||m.last_name MEMBER, t.title, ai
o t m u ide
FROM member m @ h nt G
r.book_date, r.act_ret_date - r.book_date DURATION
JOIN rental r
5 _ 95 tude
ON r.member_id = m.member_id
c k 2 is S
JOIN title t
e r i th
ON r.title_id = t.title_id (
z o us e
ORDER BY member; a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i
s a
) ha
l c om
t m ai ide
h o t Gu
_ 9 5@ uden
k 2 5 s St
r ic t h i
e
z ( o use
a
D et
d e z n s
n e
o M ble lic
d
e rar sfera
k G tran
c
Er non-
i