Académique Documents
Professionnel Documents
Culture Documents
bl
a
r
fe
ns
a
r
t
n-
nSQL
Oracle Database 10g:
a
s
Fundamentals) h
II a
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
Student Guide Volume 2
IM
N
OR
PO
D17111GC30
Edition 3.0
January 2009
D57874
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Salome Clement
Chaitanya Koratamaddi
Priya Vennapusa
Disclaimer
Technical Contributors
and Reviewers
Claire Bennett
Brian Boxx
Zarko Cesljas
Laurent Dereac
Nancy Greenberg
Yash Jain
Angelika Krupp
Malika Marghadi
Priya Nathan
Narayanan Radhakrishnan
Bryan Roberts
Lata Shivaprasad
Naoko Susuki
PO
ns
a
r
t
n-
n
a
has
)
om uide
Editors
c
l
ai nt G
Nita Pavitran
m
g de
Atanu Raychaudhuri
@
1
tu
2
S
i
ev this
d
a
Graphic Designer
jay to use
(
Sanjeev Sharma
I T se
V
E cen
D
li
A
M
I
Publishers
N
R
O Jobi Varghese
Trademark Notice
Giri Venugopal
bl
a
r
fe
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Contents
Preface
I
Introduction
Objectives I-2
Course Objectives I-3
Course Overview I-4
Course Application I-5
Summary I-6
bl
a
r
fe
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
IM
N
OR
PO
iii
ns
a
r
t
n-
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
3 Manipulating Large Data Sets
jay to use
(
Objectives 3-2
ItoTManipulate
V
se Data 3-3
Using Subqueries
n
E
e
D fromlicAnother Table 3-4
Copying
Rows
A
IM Using a Subquery as a Target 3-5
Inserting
N
R
OO Retrieving Data with a Subquery as Source 3-7
iv
ns
a
r
t
n-
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
R
O
O
n
a
ROLLUP Operator 4-7
as
h
)
ROLLUP Operator: Example 4-8
om uide
c
l
CUBE Operator 4-9
ai nt G
m
CUBE Operator: Example 4-10
g de
@
1
tu
GROUPING Function 4-11
2
S
i
is
ev 4-12
GROUPING Function: Example
h
d
t
a
y use
GROUPING SETS
ja4-13
(
to 4-15
GROUPINGI T
SETS: Example
e
V
s
n 4-17
E Columns
e
Composite
D
c
i
l
IMAComposite Columns: Example 4-19
GROUP BY with ROLLUP and CUBE Operators 4-6
ns
a
r
t
n-
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
no
a
has
TZ_OFFSET 5-26
)
om uide
TIMESTAMP Conversion Using FROM_TZ 5-28
c
l
aiTO_TIMESTAMP_TZ
G
Converting to TIMESTAMP Using TO_TIMESTAMPm
and
5-29
t
n
g
e
Time Interval Conversion with TO_YMINTERVAL
d
@ 5-30
u
1
t
2
S
i
Using TO_DSINTERVAL: Example v5-31
is
e
h
d
t
Daylight Saving Time 5-32ya
ja to use
(
Summary 5-34
I T 5-35se
V
Practice 5: Overview
E cen
D
li
A
M
I
Retrieving Data Using Subqueries
N
EXTRACT 5-25
R Objectives
O
O
ns
a
r
t
n-
6-2
Multiple-Column Subqueries 6-3
Column Comparisons 6-4
Pairwise Comparison Subquery 6-5
Nonpairwise Comparison Subquery 6-6
Scalar Subquery Expressions 6-7
Scalar Subqueries: Examples 6-8
Correlated Subqueries 6-10
Using Correlated Subqueries 6-12
Using the EXISTS Operator 6-14
Find Employees Who Have At Least One Person Reporting to Them 6-15
Find All Departments That Do Not Have Any Employees 6-16
Correlated UPDATE 6-17
vi
bl
a
r
fe
6-18
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
vii
ns
a
r
t
n-
n
a
as
h
)
Formatting Hierarchical Reports Using LEVEL and LPAD
m 7-11
e
o
d
c
i
S
i
ev this
d
a
8 Regular Expression
se
ay Support
j
u
(
Objectives T
8-2
to
I
e
V
s
Regular
n Overview 8-3
E Expression:
e
D
c
i
l
IMAMeta Characters 8-4
R
O
O
Hierarchical Retrieval
Objectives 7-2
Sample Data from the EMPLOYEES Table 7-3
Natural Tree Structure 7-4
Hierarchical Queries 7-5
Walking the Tree 7-6
Walking the Tree: From the Bottom Up 7-8
Walking the Tree: From the Top Down 7-9
Ranking Rows with the LEVEL Pseudocolumn 7-10
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
PO
ns
a
r
t
n-
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
IM
N
OR
bl
a
r
fe
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
ns
a
r
t
n-
Index
n
a
has
)
Additional Practice Solutions
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
M
I
N
Additional Practices
R
O
O
ix
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
ns
a
r
t
n-
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
IM
N
OR
PO
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
_______________
Additional
Practices
_______________
bl
a
r
fe
ns
a
r
t
n-
PO
N
OR
IM
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
Additional Practices
The following exercises can be used for extra practice after you have discussed data
manipulation language (DML) and data definition language (DDL) statements in the lessons
titled Managing Schema Objects and Manipulating Large Data Sets.
Note: Run the lab_ap_cre_special_sal.sql, lab_ap_cre_sal_history.sql,
and lab_ap_cre_mgr_history.sql scripts in the labs folder to create the
SPECIAL_SAL, SAL_HISTORY, and MGR_HISTORY tables.
1. The Human Resources department wants to get a list of underpaid employees, the salary
history of employees, and the salary history of managers based on an industry salary
survey. So they have asked you to do the following:
Write a statement to do the following:
- Retrieve the employee ID, hire date, salary, and manager ID of those employees
whose employee ID is more than or equal to 200 from the EMPLOYEES table.
- If the salary is less than $5,000, insert the employee ID and salary into the
SPECIAL_SAL table.
- Insert the employee ID, hire date, and salary into the SAL_HISTORY table.
- Insert the employee ID, manager ID, and salary into the MGR_HISTORY table.
2. Query the SPECIAL_SAL, SAL_HISTORY and MGR_HISTORY tables to view the
inserted records.
ns
a
r
t
n-
R
O
O
n
a
has
)
om uide
c
l
ai nt G
m
g de
SPECIAL_SAL
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
SALARY_HISTORY
li
A
M
I
N
bl
a
r
fe
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
MGR_HISTORY
3. The DBA wants you to create a table, which has a primary key constraint, but wants the
index to have a different name than the constraint. Create the
LOCATIONS_NAMED_INDEX table based on the following table instance chart. Name
the index for the PRIMARY KEY column as LOCATIONS_PK_IDX.
Column Name
Deptno
Dname
Yes
ns
a
r
t
n-
n
a
s
VARCHAR2 ha
Number
Data Type
)
m ide
4
30 co
Length
l
ai nt Gu
m
gINDEX_NAME
de for the
@
4. Query the USER_INDEXES table to display
u
1
t
i2 is S
LOCATIONS_NAMED_INDEX v
table.
e
d e th
a
y
(ja to us
T
VI ense
E
D lic
A
NIM
Primary Key
R
O
O
bl
a
r
fe
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ns
a
r
t
n-
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
IM
N
OR
PO
bl
a
r
fe
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
ns
a
r
t
n-
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
M
I
RN
PO
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
The following exercises can be used for extra practice after you have discussed the datetime
functions.
You work for a global company and the new vice president of operations wants to know the
different time zones of all the company branches. He has requested the following information:
7. Alter the session to set the NLS_DATE_FORMAT to DD-MON-YYYY HH24:MI:SS.
8. a. Write queries to display the time zone offsets (TZ_OFFSET) for the following time
zones:
Australia/Sydney
Chile/Easter Island
ns
a
r
t
n-
no
a
s
b. Alter the session to set the TIME_ZONE parameter value to h
theatime zone offset of
)
Australia/Sydney.
om uide
c
l
ai nt G
c. Display SYSDATE, CURRENT_DATE, CURRENT_TIMESTAMP,
and
m
g
e
LOCALTIMESTAMP for this session.
@ tud
1
2
R
O
O
bl
a
r
fe
cases, they will not match the actual results that the students get. Also, the time zone
offset of the various countries may differ based on the daylight saving time.
e. Display SYSDATE, CURRENT_DATE, CURRENT_TIMESTAMP, and
LOCALTIMESTAMP for this session.
Note: The output may be different based on the date when the command is executed.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
ns
a
r
t
n-
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
IM
N
OR
PO
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
The following exercises can be used for extra practice after you have discussed advanced
subqueries.
10. The CEO needs a report on the top three earners in the company for profit sharing. He
has asked you to provide him with a list.
Write a query to display the top three earners in the EMPLOYEES table. Display their last
names and salaries.
11. The benefits for the state of California have been changed based on a local ordinance. So
the benefits representative has asked you to compile a list of people who are affected.
Write a query to display the employee ID and last name of the employees who work in
the state of California.
Hint: Use scalar subqueries.
ns
a
r
t
n-
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
M
I
N
R
O
O
12. The DBA wants to remove old information from the database. One of the things that the
DBA thinks is unnecessary is the old employment records. She has asked you to do the
following:
Write a query to delete the oldest JOB_HISTORY row of an employee by looking up the
JOB_HISTORY table for MIN(START_DATE) for the employee. Delete the records of
only those employees who have changed at least two jobs.
Hint: Use a correlated DELETE command.
bl
a
r
fe
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
ns
a
r
t
n-
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
b. The organization
I T tree
eunder De Haan (employee number 102):
V
s
n
E
D lice
A
NIM
The following exercises can be used for extra practice after you have discussed hierarchical
retrieval.
15. Lex De Haan is quitting the company. His replacement wants reports of his direct reports.
Write a SQL statement to display the employee number, last name, start date, and salary,
showing:
a. De Haans direct reports:
R
O
O
16. Write a hierarchical query to display the employee number, manager number, and
employee last name for all employees who are two levels below employee De Haan
(employee number 102). Also, display the level of the employee.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
ns
a
r
t
n-
no
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
se All the rows from the actual output are not
Note: The output shown
ausample.
ayis only
j
(
included here. I T
to
e
V
s
E cen
D
li
A
M
I
N
R
O
O
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
______________________
Additional
Practice Solutions
______________________
bl
a
r
fe
ns
a
r
t
n-
PO
N
OR
IM
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
ns
a
r
t
n-
IM
N
OR
PO
n
a
has
)
om uide
c
INSERT ALL
l
ai nt G
m
WHEN SAL < 5000 THEN
g de
@
1
INTO special_sal VALUES
(EMPID,
tu SAL)
2
S
i
ELSE
ev this
d
a
se
INTO sal_history
jay to VALUES(EMPID,HIREDATE,SAL)
u
(
T e VALUES(EMPID,MGR,SAL)
INTO Imgr_history
V
ns
E
e
D
SELECTicemployee_id
EMPID, hire_date HIREDATE,
l
A
salary SAL, manager_id MGR
FROM employees
WHERE employee_id >=200;
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
3. The DBA wants you to create a table, which has a primary key constraint, but the DBA
wants the index to have a different name than the constraint. Create the
LOCATIONS_NAMED_INDEX table based on the following table instance chart.
Column Name
Deptno
Dname
Primary Key
Yes
Data Type
Number
VARCHAR2
Length
30
ns
a
r
t
n-
no
a
as
h
)
4. Query the USER_INDEXES table to display INDEX_NAME
mfor the
e
o
d
LOCATIONS_NAMED_INDEX table.
c
i
l
ai nt Gu
m
g de
@
SELECT INDEX_NAME, TABLE_NAME
1
tu
2
S
i
FROM USER_INDEXES ev
is
h
d
t
a
WHERE TABLE_NAME
se
jay t=oLOCATIONS_NAMED_INDEX;
u
(
I T se
V
E cen
D
li can be used for extra practice after you have discussed enhancements
A exercises
The Ifollowing
M
N
ORto the GROUP BY clause.
PO
bl
a
r
fe
5. The Human Resources department requires some reports on certain departments. These
are its requirements:
Write a query to display the following for those departments whose department ID is
greater than 80:
- The total salary for every job within a department
- The total salary
- The total salary for those cities in which the departments are located
- The total salary for every job, irrespective of the department
- The total salary for every department irrespective of the city
- The total salary for the departments, irrespective of the job titles and cities
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
COLUMN
COLUMN
COLUMN
COLUMN
SELECT
ns
a
r
t
n-
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
y use
SELECT (ja
I T se to
department_id,job_id,manager_id,max(salary),
V
n
Emin(salary)
e
D
c
i
l
A
6. The Accounting department requires an analysis on the maximum and minimum salaries
by department, job, and manager. They have asked you to do the following:
Write a query to display the following groupings:
- Department ID, Job ID
- Job ID, Manager ID
The query should calculate the maximum and minimum salaries for each of these groups.
IM
PO
N
OR
FROM
employees
GROUP BY GROUPING SETS
((department_id,job_id), (job_id,manager_id));
The following exercises can be used for extra practice after you have discussed the datetime
functions.
You work for a global company and the new vice president of operations wants to know the
different time zones of all the company branches. He has requested the following information:
7. Alter the session to set the NLS_DATE_FORMAT to DD-MON-YYYY HH24:MI:SS.
ALTER SESSION
SET NLS_DATE_FORMAT = DD-MON-YYYY HH24:MI:SS;
bl
a
r
fe
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
8. a.
Write queries to display the time zone offsets (TZ_OFFSET) for the following time
zones:
- Australia/Sydney
SELECT TZ_OFFSET (Australia/Sydney) from dual;
-
Chile/Easter Island
SELECT TZ_OFFSET (Chile/EasterIsland) from dual;
b.
Alter the session to set the TIME_ZONE parameter value to the time zone offset of
Australia/Sydney.
bl
a
r
fe
ns
a
r
t
n-
no
a
has
)
m ide
SELECT SYSDATE, CURRENT_DATE, o
CURRENT_TIMESTAMP,
c
l
LOCALTIMESTAMP FROM DUAL;
ai nt Gu
m
g de
@
1
tu value to the time zone offset of
d. Alter the session to set the TIME_ZONE
parameter
2
S
i
is
Chile/Easter Island. dev
h
t
a
se question are based on a different date, and in
Note: The results
jaofy thetopreceding
u
(
Tthey wille not match the actual results that the students get. Also, the
some cases,
I
V
s the various countries may differ based on the daylight saving
timeE
zone offsetnof
e
D
time.
lic
IMA
R
O
O
e.
f.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
ns
a
r
t
n-
no
a
has
)
The following exercises can be used for extra practice after youm
e advanced
o haveudiscussed
d
c
i
subqueries.
l
ai nt G
m
g de
@
1
10. The CEO needs a report on the topthree
earners
tinu the company for profit sharing. He
2
S
i
v a list.this
has asked you to provide him e
with
d
a
Write a query to display
three
seearners in the EMPLOYEES table. Display their last
aythe top
j
u
(
names and salaries.
I T se to
V
E cen
D
A SELECTli last_name, salary
M
I
N
FROM employees e
OR
PO
bl
a
r
fe
11. The benefits for the state of California have been changed based on a local ordinance. So
the benefits representative has asked you to compile a list of people who are affected.
Write a query to display the employee ID and last name of the employees who work in
the state of California.
Hint: Use scalar subqueries.
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
12. The DBA wants to remove old information from the database. One of the things that the fera
s
DBA thinks is unnecessary is the old employment records. He or she has asked youatondo
r
t
the following:
no
n
Write a query to delete the oldest JOB_HISTORY row of an employeeaby looking up the
s the records of
JOB_HISTORY table for MIN(START_DATE)for the employee.
haDelete
)
only those employees who have changed at least two jobs.m
o uide
c
i
is employee_id
WHERE employee_id
ev= (SELECT
h
d
t
a
se E
ay employees
jFROM
u
(
toJH.employee_id = E.employee_id
I T WHERE
e
V
s
E cenAND START_DATE = (SELECT MIN(start_date)
D
li
A
FROM job_history JH
M
I
N
R
WHERE JH.employee_id = E.employee_id)
PO
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
14. The sluggish economy is forcing the management to take cost reduction actions. The
CEO wants to review the highest paid jobs in the company. He has requested a list from
you based on the following specifications:
Write a query to display the job IDs of those jobs whose maximum salary is above half
the maximum salary in the entire company. Use the WITH clause to write this query.
Name the query MAX_SAL_CALC.
WITH
MAX_SAL_CALC AS (SELECT job_title, MAX(salary) AS
job_total
FROM employees, jobs
WHERE employees.job_id = jobs.job_id
GROUP BY job_title)
SELECT job_title, job_total
FROM MAX_SAL_CALC
WHERE job_total > (SELECT MAX(job_total) * 1/2
FROM MAX_SAL_CALC)
ORDER BY job_total DESC;
ns
a
r
t
n-
no
a
has
)
om uide
c
l
ai nt G
m
g deyou have discussed hierarchical
@
The following exercises can be used for extra
practicetafter
u
1
2
S
i
retrieval.
is
ev tHis
h
dcompany.
15. Lex De Haan is quittingythe
a
e replacement wants reports of his direct reports.
s
a
j
u
( to display
Write a SQL statement
o the employee number, last name, start date, and salary,
t
T
I
e
showing: V
s
n
E
e
D
a. A De Haans
lic direct reports:
M
I
N
SELECT employee_id, last_name, hire_date, salary
OR
PO
FROM employees
WHERE manager_id = (SELECT employee_id
FROM employees
WHERE last_name = De Haan);
b.
bl
a
r
fe
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
16. Write a hierarchical query to display the employee number, manager number, and
employee last name for all employees who are two levels below employee De Haan
(employee number 102). Also display the level of the employee.
SELECT employee_id, manager_id, level, last_name
FROM employees
WHERE LEVEL = 3
CONNECT BY manager_id = PRIOR employee_id
START WITH employee_id = 102;
17. The CEO wants a hierarchical report on all employees. He has given you the following
requirements:
Produce a hierarchical report to display the employee number, manager number, the
LEVEL pseudocolumn, and employee last name. For every row in the EMPLOYEES table,
you should print a tree structure that shows the employee, the employees manager, the
managers manager, and so on. Use indentations for the NAME column.
bl
a
r
fe
ns
a
r
t
n-
no
a
has
COLUMN name FORMAT A25
)
m
o
SELECT employee_id, manager_id,
LEVEL, ide
c
l
ai nt Gu
LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_')
m
g de
LAST_NAME
@
1
tu
2
S
i
FROM employees
ev this= PRIOR manager_id;
d
CONNECT BY
employee_id
a
se
ay CLEAR
jname
u
(
COLUMN
I T se to
V
E cen
D
li
A
IM
N
OR
PO
Unauthorized reproduction or distribution prohibited Copyright 2014, Oracle and/or its affiliates
bl
a
r
fe
ns
a
r
t
n-
PO
N
OR
IM
n
a
has
)
om uide
c
l
ai nt G
m
g de
@
1
tu
2
S
i
ev this
d
a
jay to use
(
I T se
V
E cen
D
li
A