Sunteți pe pagina 1din 146

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

s
) ha

co

eSQL
ed 12c:

d
Oracle Database
i
a
u
n
e
G
s
t
i
Workshop
m Iden
@
ila s Stu
h
c
ar Activity
thi Guide
g
(
e
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

D80190GC11
Edition 1.1
July 2014
D87242

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

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

Disclaimer
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 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 not use, share, download, upload, copy, print, display,
perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization
of Oracle.
The information contained in this document is subject to change without notice. If you find any problems in the document, please
report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
warranted to be error-free.
Restricted Rights Notice
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 applicable:
U.S. GOVERNMENT RIGHTS
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 license agreement and/or the applicable U.S. Government contract.

s
) ha

co

u
ed ide

a
en t Gu
s
i
Author
m den
@
Dimpi Rani Sarmah
ila s Stu
h
c
ar e thi
Technical Contributors and Reviewers (g
us Laszlo Czinkoczki, KimSeong Loh,
Nancy Greenberg, Swarnapriya Shridhar,
onBryantoRoberts,
r
d
l
Brent Dayley, Jim Spiller, Christopher
Manish Pawar, Clair Bennett, Yanti Chang,
uaandeWensley,
se Siddireddy
n
Joel Goodman, Gerlinde Frenzen
Madhavi
G
la le lic
i
h
rc rab
A
o
fe using: Oracle Tutor
s
m
This book
was published
r
n
e
ill n-tra
u
G no
Trademark Notice

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

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

Table of Contents
Practices for Lesson 1: Introduction ........................................................................................................ 1-1
Practices for Lesson 1: Overview............................................................................................................. 1-2
Practice 1-1: Introduction......................................................................................................................... 1-3
Solution 1-1: Introduction......................................................................................................................... 1-4
Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement........................................... 2-1
Practices for Lesson 2: Overview............................................................................................................. 2-2
Practice 2-1: Retrieving Data Using the SQL SELECT Statement ............................................................. 2-3
Solution 2-1: Retrieving Data Using the SQL SELECT Statement ............................................................. 2-8
Practices for Lesson 3: Restricting and Sorting Data .............................................................................. 3-1
Practices for Lesson 3: Overview............................................................................................................. 3-2
Practice 3-1: Restricting and Sorting Data................................................................................................ 3-3
Solution 3-1: Restricting and Sorting Data................................................................................................ 3-7

s
) ha

Practices for Lesson 4: Using Single-Row Functions to Customize Output ........................................... 4-1
Practices for Lesson 4: Overview............................................................................................................. 4-2
Practice 4-1: Using Single-Row Functions to Customize Output................................................................ 4-3
Solution 4-1: Using Single-Row Functions to Customize Output................................................................ 4-9

co

u
ed ide

a
u 5-1
n .............................
Practices for Lesson 5: Using Conversion Functions and Conditional Expressions
e
G
s
t
i
Practices for Lesson 5: Overview.............................................................................................................
5-2
m den
@
Practice 5-1: Using Conversion Functions and Conditional Expressions
....................................................
5-3
tu
ila ....................................................
S
h
Solution 5-1: Using Conversion Functions and Conditional Expressions
5-9
c
is
r
h
a
t
g
Practices for Lesson 6: Reporting Aggregated Data(Using the Group
e Functions................................... 6-1
s
n
u
o
Practices for Lesson 6: Overview.............................................................................................................
6-2
r
o
d
t
l
Practice 6-1: Reporting Aggregated Data
Group Functions......................................................... 6-3
a by Using
uData
se
n
G
Solution 6-1: Reporting Aggregated
bye
Using
Group Functions......................................................... 6-6
c
ila e lifrom
Practices for Lessonc
7:h
DisplayinglData
Multiple Tables Using Joins ............................................ 7-1
b
r 7: Overview
a
A
Practices for Lesson
.............................................................................................................
7-2
r
e
o
f
s
Practice
7-1: Displaying
Data from Multiple Tables by Using Joins............................................................ 7-3
n Data
erm
a
l
r
l
i
Solution
7-1:
Displaying
from Multiple Tables by Using Joins............................................................ 7-8
t
u onGPractices
n for Lesson 8: Using Subqueries to Solve Queries ................................................................... 8-1
Practices for Lesson 8: Overview............................................................................................................. 8-2
Practice 8-1: Using Subqueries to Solve Queries ..................................................................................... 8-3
Solution 8-1: Using Subqueries to Solve Queries ..................................................................................... 8-6

Practices for Lesson 9: Using the Set Operators ..................................................................................... 9-1


Practices for Lesson 9: Overview............................................................................................................. 9-2
Practice 9-1: Using Set Operators ........................................................................................................... 9-3
Solution 9-1: Using Set Operators ........................................................................................................... 9-5
Practices for Lesson 10: Manipulating Data............................................................................................. 10-1
Practices for Lesson 10: Overview ........................................................................................................... 10-2
Practice 10-1: Managing Tables by Using DML Statements ...................................................................... 10-3
Solution 10-1: Managing Tables by Using DML Statements ...................................................................... 10-7
Practices for Lesson 11: Using DDL Statements to Create and Manage Tables ..................................... 11-1
Practices for Lesson 11: Overview ........................................................................................................... 11-2
Practice 11-1: Introduction to Data Definition Language ........................................................................... 11-3
Solution 11-1: Introduction to Data Definition Language ........................................................................... 11-7
Additional Practices and Solutions .......................................................................................................... 12-1
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Oracle Database 12c: SQL Workshop I Table of Contents


i

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

Practices for Lesson 1............................................................................................................................. 12-2


Practice 1-1: Additional Practice .............................................................................................................. 12-3
Solution 1-1: Additional Practice .............................................................................................................. 12-11
Case Study: Online Book Store ............................................................................................................... 12-17
Practice 1-2 ............................................................................................................................................ 12-18
Solution 1-2 ............................................................................................................................................ 12-23

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Oracle Database 12c: SQL Workshop I Table of Contents


ii

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

s
) ha

co

u
ed ide

a
en t G1:u
s
i
Practices for
Lesson
m den
@
Introduction
ila s Stu
h
c
ar e1 thi
Chapter
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 1

Practices for Lesson 1: Overview

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

Practice Overview
In this practice, you start SQL Developer, create a new database connection, and browse your
HR tables. You also set some SQL Developer preferences.
In some of the practices, there may be exercises that are prefaced with the phrases If you have
time or If you want an extra challenge. Work on these exercises only if you have completed all
other exercises within the allocated time, and would like an additional challenge to your skills.
Perform the practices slowly and precisely. You can experiment with saving and running
command files. If you have any questions at any time, ask your instructor.
Notes

All written practices use Oracle SQL Developer as the development environment.
Although it is recommended that you use Oracle SQL Developer, you can also use
SQL*Plus that is available in this course.
For any query, the sequence of rows retrieved from the database may differ from
the screenshots shown.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 2

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

Practice 1-1: Introduction


Overview
This is the first of many practices in this course. The solutions (if you require them) can be found
at the end of this practice. The practices are intended to cover most of the topics that are
presented in the corresponding lesson.
In this practice, you perform the following:
Start Oracle SQL Developer and create a new connection to the ora1 account.

Use Oracle SQL Developer to examine the data objects in the ora1 account. The ora1
account contains the HR schema tables.
Note the following location for the practice files:
/home/oracle/labs/sql1/labs
If you are asked to save any practice files, save them in the preceding location.
Tasks
1. Start Oracle SQL Developer by using the SQL Developer Desktop icon.
2. Create a New Oracle SQL Developer Database Connection
a. To create a new database connection, in the Connections Navigator, right-click
Connections and select New Connection from the context menu. The New/Select
Database Connection dialog box appears.
b. Create a database connection by using the following information:
Connection Name: myconnection
Username: ora1
Password: ora1

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
Hostname: localhost
G
Port: 1521
la le lic
i
h
rcORCLrab
SID:
A
o that you
fe
Ensure
select the Save Password check box.
s
m
r
n
e
a
l
r
l
i 3. Testing
t the Oracle SQL Developer Database Connection and Connecting to the
Gu nDatabase
on-

a. Test the new connection.


b. If the status is Success, connect to the database by using this new connection.
4. Browsing the Tables in the Connections Navigator
a. In the Connections Navigator, view the objects that are available to you in the
Tables node. Verify that the following tables are present:
COUNTRIES
DEPARTMENTS
EMPLOYEES
JOB_GRADES
JOB_HISTORY
JOBS
LOCATIONS
REGIONS
b.
c.

Browse the structure of the EMPLOYEES table.


View the data of the DEPARTMENTS table.
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 3

Solution 1-1: Introduction

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

1. Starting Oracle SQL Developer Using the SQL Developer Desktop Icon
Double-click the Oracle SQL Developer desktop icon.

The SQL Developer Interface appears.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

2. Creating a New Oracle SQL Developer Database Connection


a. To create a new database connection, in the Connections Navigator, right-click
Connections and select New Connection from the context menu.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 4

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

The New / Select Database Connection dialog box appears.

s
) ha

co

u
ed ide

a
en t Gu
s
i
n
b. Create a database connection by using the following
m information:
e
d
@
i. Connection Name: myconnection
ila s Stu
h
c
ii. Username: ora1
ar e thi
g
(
iii. Password: ora1on
us
r
o
d
t
l
iv. Hostname:
ualocalhost
se
n
G
e
v. Port:
ic
a 1521
l
l
i
h
e
l
bORCL
rc vi. SID:
a
A
r
o Ensure
fethat you select the Save Password check box.
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 5

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

3. Testing and Connecting Using the Oracle SQL Developer Database Connection
a. Test the new connection.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
tu
ila s bySusing
hdatabase
b. If the status is Success, connect to rthe
this new connection.
c
i
h
a
t
g
(
e
s
n
u
o
dr e to
l
a
u ens
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

When you create a connection, a SQL Worksheet for that connection opens automatically.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 6

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
4. Browsing the Tables in the Connections Navigator
la Stu
i
h
c
a. In the Connections Navigator, viewrthe objectsis
are available to you in the
a tables
tharethat
g
Tables node. Verify that the following
present:
(
e
COUNTRIES
ron to us
d
l
DEPARTMENTS ua
se
n
EMPLOYEES G
e
la le lic
i
JOB_GRADES
h
rc rab
JOB_HISTORY
A
o JOBSsfe
m
r
e
an
ill n-trLOCATIONS
u
G no REGIONS

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 7

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

b. Browse the structure of the EMPLOYEES table.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
c. View the data of the DEPARTMENTS table. @
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 8

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

s
) ha

co

u
ed ide

a
en t G2:u
s
i
Practices for
Lesson
m den
@
Retrieving
tu Using the
ila sData
S
h
c
SQL
Statement
ar SELECT
thi
g
(
e
us 2
ron Chapter
o
d
t
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 1

Practices for Lesson 2: Overview


Practice Overview

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

This practice covers the following topics:

Selecting all data from different tables

Describing the structure of tables

Performing arithmetic calculations and specifying column names

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 2

Practice 2-1: Retrieving Data Using the SQL SELECT Statement


Overview

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

In this practice, you write simple SELECT queries. The queries cover most of the SELECT
clauses and operations that you learned in this lesson.
Task 1
Test your knowledge:
1.

The following SELECT statement executes successfully:


SELECT last_name, job_id, salary AS Sal
FROM
employees;
True/False

s
) ha

co

u
SELECT *
ed ide

a
FROM
job_grades;
en t Gu
s
i
m den
@
True/False
ila s Stu
h
c
ar e thi
g
(
n statement.
us Can you identify them?
3. There are four coding errors in thero
following
o
d
t
l
SELECT
employee_id,
ua enlast_name
se
G
sal x 12 ilANNUAL
a e SALARY
lic
h
l
FROM rc
A feemployees;
rab
o
Task 2rm
s
n
e
a
l
r
l
-t points before you begin with the practices:
ui theofollowing
n
GNote
n
2.

The following SELECT statement executes successfully:

Save all your practice files at the following location:


/home/oracle/labs/sql1/labs

Enter your SQL statements in a SQL Worksheet. To save a script in SQL Developer,
make sure that the required SQL Worksheet is active, and then from the File menu,
select Save As to save your SQL statement as a lab_<lessonno>_<stepno>.sql
script. When you modify an existing script, make sure that you use Save As to save it
with a different file name.

To run the query, click the Execute Statement icon in the SQL Worksheet. Alternatively,
you can press F9. For DML and DDL statements, use the Run Script icon or press F5.

After you have executed the query, make sure that you do not enter your next query in
the same worksheet. Open a new worksheet.

You have been hired as a SQL programmer for Acme Corporation. Your first task is to create
some reports based on the data from the Human Resources tables.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 3

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

4.

Your first task is to determine the structure of the DEPARTMENTS table and its contents.

s
) ha

co

u
ed ide

a
en t Gu
s
i
n
m anddits
e
5. Your task is to determine the structure of the EMPLOYEES
table
@
la Stu contents.
i
h
a. Determine the structure of the EMPLOYEES
rctable. this
a
g
(
e
s
n
u
o
dr e to
l
a
u ens
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 4

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

b.

The HR department wants a query to display the last name, job ID, hire date, and
employee ID for each employee, with the employee ID appearing first. Provide an alias
STARTDATE for the HIRE_DATE column. Save your SQL statement to a file named
lab_02_5b.sql so that you can dispatch this file to the HR department. Test your
query in the lab_02_5b.sql file to ensure that it runs correctly.

Note: After you have executed the query, make sure that you do not enter your next query
in the same worksheet. Open a new worksheet.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 5

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

6.

The HR department wants a query to display all unique job IDs from the EMPLOYEES table.

s
) ha

co

u
ed ide

Task 3
a
u
n
e
G
s
t
i
If you have time, complete the following exercises:
m den
@
7. The HR department wants more descriptive columnila
headings for
tuits report on employees.
S
h
c
Copy the statement from lab_02_5b.sql tora new SQLis
Worksheet. Name the columns
a e thThen
g
Emp #, Employee, Job, and Hire Date,
respectively.
run the query again.
(
s
n
u
o
dr e to
l
a
u ens
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 6

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

8.

The HR department has requested a report of all employees and their job IDs. Display the
last name concatenated with the job ID (separated by a comma and space) and name the
column Employee and Title.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
If you want an extra challenge, complete the following
exercise:
c
ar e thi
g
(
9. To familiarize yourself with the data innthe EMPLOYEES
s table, create a query to display all
u
o
r
the data from that table. Separate
each
column
output
by a comma. Name the column
o
d et
l
a
THE_OUTPUT.
u ens
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 7

Solution 2-1: Retrieving Data Using the SQL SELECT Statement


Task 1

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

Test your knowledge:


1.

The following SELECT statement executes successfully:


SELECT last_name, job_id, salary AS Sal
FROM
employees;
True/False

2.

The following SELECT statement executes successfully:


SELECT *
FROM
job_grades;

s
) ha

o
c

u
True/False
ed ide

a
en t Gu
s
i
n them?
myou identify
3. There are four coding errors in the following statement. Can
e
d
@
ila s Stu
SELECT
employee_id, last_name
h
c
r
thi
sal x 12 ANNUAL SALARY (ga
e
us
FROM
employees; ron
o
d
t
l
The EMPLOYEES table does
contain
ua not
se a column called sal. The column is called
n
G
e
SALARY.
la le lic
i
h
b
rc raoperator
The multiplication
is *, not x, as shown in line 2.
A
e
o
f
sSALARY alias cannot include spaces. The alias should read
m ANNUAL
rThe
n
e
a
l
r
l
or should be enclosed within double quotation marks.
i ANNUAL_SALARY
n-t
Gu nAocomma
is missing after the LAST_NAME column.
Task 2
You have been hired as a SQL programmer for Acme Corporation. Your first task is to create
some reports based on the data from the Human Resources tables.
4.

Your first task is to determine the structure of the DEPARTMENTS table and its contents.
a.

To determine the DEPARTMENTS table structure:


DESCRIBE departments

b.

To view the data contained in the DEPARTMENTS table:


SELECT *
FROM
departments;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 8

5.

Your task is to determine the structure of the EMPLOYEES table and its contents.
a.

Determine the structure of the EMPLOYEES table.


DESCRIBE employees

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

b.

The HR department wants a query to display the last name, job ID, hire date, and
employee ID for each employee, with the employee ID appearing first. Provide an alias
STARTDATE for the HIRE_DATE column. Save your SQL statement to a file named
lab_02_5b.sql so that you can dispatch this file to the HR department. Test your
query in the lab_02_5b.sql file to ensure that it runs correctly.
SELECT employee_id, last_name, job_id, hire_date StartDate
FROM
employees;

6.

s
) ha

The HR department wants a query to display all unique job IDs from the EMPLOYEES table.

co

u
ed ide

a
en t Gu
Task 3
s
i
m den
If you have time, complete the following exercises:
@
la Stu
i
h
c
7. The HR department wants more descriptive column
for its report on employees.
atora neweheadings
thisWorksheet.
g
Copy the statement from lab_02_5b.sql
SQL
Name the columns
(
s Then run the query
n respectively.
u
Emp #, Employee, Job, and Hirero
Date,
again.
o
d
t
l
ua"Emp
se last_name "Employee",
SELECT employee_id
#",
n
G
e
lichire_date "Hire Date"
job_id
la "Job",
i
h
e
l
FROM Arc
employees;
rab
e
o
f
rm rans
e
l
l
i HR
t
-department
has requested a report of all employees and their job IDs. Display the
n
G8.u The
o
n
SELECT DISTINCT job_id
FROM
employees;

last name concatenated with the job ID (separated by a comma and space) and name the
column Employee and Title.
SELECT last_name||', '||job_id "Employee and Title"
FROM
employees;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 9

If you want an extra challenge, complete the following exercise:

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

9.

To familiarize yourself with the data in the EMPLOYEES table, create a query to display all
the data from that table. Separate each column output by a comma. Name the column
THE_OUTPUT.
SELECT employee_id || ',' || first_name || ',' || last_name
|| ',' || email || ',' || phone_number || ','|| job_id
|| ',' || manager_id || ',' || hire_date || ','
|| salary || ',' || commission_pct || ',' ||
department_id
THE_OUTPUT
FROM
employees;

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 10

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

s
) ha

co

u
ed ide

a
en t G3:u
s
i
Practices for
Lesson
m den
@
Restricting
tu Sorting Data
ila s and
S
h
c
ar 3 thi
g
Chapter e
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 1

Practices for Lesson 3: Overview


Practices Overview

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

This practice covers the following topics:

Selecting data and changing the order of the rows that are displayed

Restricting rows by using the WHERE clause

Sorting rows by using the ORDER BY clause

Using substitution variables to add flexibility to your SQL SELECT statements

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 2

Practice 3-1: Restricting and Sorting Data

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

Overview
In this practice, you build more reports by using statements that use the WHERE clause and the
ORDER BY clause. You make the SQL statements more reusable and generic by including the
ampersand substitution.

Task
The HR department needs your assistance in creating some queries.
1. Because of budget issues, the HR department needs a report that displays the last name
and salary of employees who earn more than $12,000. Save your SQL statement as a file
named lab_03_01.sql. Run your query.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
arthat displays
thi the last name and department
g
2. Open a new SQL Worksheet. Create a report
(
e
n the query.
number for employee number 176.
us
roRun
o
d
t
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
3. illThe HR department
to find high-salary and low-salary employees. Modify
tra to needs
u
n
lab_03_01.sql
display
the last name and salary for any employee whose salary is not
G no
in the range $5,000 through $12,000. Save your SQL statement as lab_03_03.sql.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 3

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

4.

Create a report to display the last name, job ID, and hire date for employees with the last
names of Matos and Taylor. Order the query in ascending order by hire date.

5.

Display the last name and department ID of all employees in departments 20 or 50 in


ascending alphabetical order by last_name.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den who earn
6. Modify lab_03_03.sql to display the last name and @
salary of employees
u the columns
ila20 ors50.StLabel
between $5,000 and $12,000, and are in department
h
c
hi
ar Save
Employee and Monthly Salary, respectively.
as
tlab_03_03.sql
g
(
e
s
lab_03_06.sql again. Run the statement
ron toin ulab_03_06.sql.
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
illThe HR
tra
7.
department
needs a report that displays the last name and hire date of all
u
n
G employees
o
who were hired in 2006.
n

8.

Create a report to display the last name and job title of all employees who do not have a
manager.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 4

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

9.

Create a report to display the last name, salary, and commission of all employees who earn
commissions. Sort the data in descending order of salary and commissions.
Use the columns numeric position in the ORDER BY clause.

10. Members of the HR department want to have more flexibility with the queries that you are
writing. They would like a report that displays the last name and salary of employees who
earn more than an amount that the user specifies after a prompt. Save this query to a file
named lab_03_10.sql. (You can use the query created in Task 1 and modify it.) If you
enter 12000 when prompted, the report displays the following results:

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
on to us
r
d
l
11. The HR department wants toarun reports
on a manager. Create a query that prompts
ebased
uand generates
s
n
the user for a manager ID,
the
employee
ID, last name, salary, and
G
e
c
i
a
l
l
department for that
managers
employees.
The
HR
department
wants the ability to sort the
i
h
e
l
c
report on a selected
ab You can test the data with the following values:
Ar fercolumn.
o
s sorted by last_name:
rm ra=n103,
manager_id
e
l
l
i
t
Gu nonmanager_id = 201, sorted by salary:

manager_id = 124, sorted by employee_id:

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 5

If you have time, complete the following exercises:

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

12. Display the last names of all employees where the third letter of the name is a.

13. Display the last names of all employees who have both an a and an e in their last name.

s
) ha

co

u
If you want an extra challenge, complete the following exercises:
ed ide

a
uthat of a
14. Display the last name, job, and salary for all employees whosesjobs
enare either
G
t
i
nto $2,500, $3,500,
sales representative or a stock clerk, and whose salaries are
mnot equal
e
d
@
or $7,000.
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
fe
s
rmolab_03_06.sql
n
15. llModify
to display the last name, salary, and commission for all
e
a
r
i employees
t
u
G non whose commission is 20%. Save lab_03_06.sql as lab_03_15.sql again.
Rerun the statement in lab_03_15.sql.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 6

Solution 3-1: Restricting and Sorting Data


The HR department needs your assistance in creating some queries.

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

1.

Because of budget issues, the HR department needs a report that displays the last name
and salary of employees earning more than $12,000. Save your SQL statement as a file
named lab_03_01.sql. Run your query.
SELECT
FROM
WHERE

2.

last_name, salary
employees
salary > 12000;

Open a new SQL Worksheet. Create a report that displays the last name and department
number for employee number 176.
SELECT
FROM
WHERE

last_name, department_id
employees
employee_id = 176;

s
) ha

co

u
edModifyide
3. The HR department needs to find high-salary and low-salary employees.

a
usalary is not
lab_03_01.sql to display the last name and salary for all employees
en whose
G
s
t
i
in the range $5,000 through $12,000. Save your SQL statement
n
m asdlab_03_03.sql.
e
@
ila s Stu
SELECT last_name, salary
h
c
ar e thi
FROM
employees
g
(
s 12000;
WHERE
salary NOT BETWEEN
uAND
ron 5000
o
d
t
l
4. Create a report to display the
last name,
ejob ID, and hire date for employees with the last
uaOrder
squery
n
G
names of Matos and Taylor.
the
in ascending order by hire date.
e
c
i
a
l
l
i
le job_id, hire_date
SELECTrchlast_name,
b
a
A
er
FROM
employees
o
f
s
m
r
an last_name IN ('Matos', 'Taylor')
r
ille WHERE
t
u
n BY hire_date;
G noORDER
5.

Display the last name and department ID of all employees in departments 20 or 50 in


ascending alphabetical order by last_name.
SELECT
FROM
WHERE
ORDER BY

6.

last_name, department_id
employees
department_id IN (20, 50)
last_name ASC;

Modify lab_03_03.sql to list the last name and salary of employees who earn between
$5,000 and $12,000, and are in department 20 or 50. Label the columns Employee and
Monthly Salary, respectively. Save lab_03_03.sql as lab_03_06.sql again. Run
the statement in lab_03_06.sql.
SELECT
FROM
WHERE
AND

last_name "Employee", salary "Monthly Salary"


employees
salary BETWEEN 5000 AND 12000
department_id IN (20, 50);
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 7

7.

The HR department needs a report that displays the last name and hire date of all
employees who were hired in 2006.

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

SELECT
FROM
WHERE
8.

Create a report to display the last name and job title of all employees who do not have a
manager.
SELECT
FROM
WHERE

9.

last_name, hire_date
employees
hire_date >= '01-JAN-06' AND hire_date < '01-JAN-07';

last_name, job_id
employees
manager_id IS NULL;

Create a report to display the last name, salary, and commission for all employees who
earn commissions. Sort data in descending order of salary and commissions. Use the
columns numeric position in the ORDER BY clause.

s
) ha

co

u
ed ide

a
en t Gu
s
i
n
mwith the
e
10. Members of the HR department want to have more flexibility
queries that you are
d
@
tusalary of employees who
writing. They would like a report that displays thehlast
ilanames and
S
c
earn more than an amount that the user specifies
ar toafter
tha iprompt. (You can use the query
g
(
created in Task 1 and modify it.) Save this
query s
a file named lab_03_10.sql.
e
ron to u
Enter 12000 when prompted:ald
u ense
G
SELECT last_name,
salary
c
a
l
l
i
h ble i
FROM rcemployees
A salary
ra > &sal_amt;
WHERE
e
o
f
s
erm12000
anwhen prompted for a value in a dialog box. Click OK.
Enter
l
r
l
i
t
u
G non
SELECT
FROM
WHERE
ORDER BY

last_name, salary, commission_pct


employees
commission_pct IS NOT NULL
2 DESC, 3 DESC;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 8

11. The HR department wants to run reports based on a manager. Create a query that prompts
the user for a manager ID, and generates the employee ID, last name, salary, and
department for that managers employees. The HR department wants the ability to sort the
report on a selected column. You can test the data with the following values:

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

manager _id = 103, sorted by last_name


manager_id = 201, sorted by salary
manager_id = 124, sorted by employee_id
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE manager_id = &mgr_num
ORDER BY &order_col;
If you have the time, complete the following exercises:

s
) ha

co

u
SELECT
last_name
ed ide

a
FROM
employees
en t Gu
s
i
WHERE
last_name LIKE '__a%';
m den
@
13. Display the last names of all employees who have both
tu an e in their last name.
ila ansaSand
h
c
SELECT
last_name
ar e thi
g
(
FROM
employees
n
us
o
r
o
d
t
l
WHERE
last_nameaLIKE '%a%'
u LIKE
se
n
AND
last_name
'%e%';
G
e
la le lic
i
h
rc rab
A
If you want o
an extra challenge,
complete the following exercises:
fe
s
m
r
n
e
14.illDisplay -the
job, and salary for all employees whose job is that of a sales
tralastorname,
u
n
representative
a
stock
clerk, and whose salary is not equal to $2,500, $3,500, or $7,000.
G no
12. Display the last names of all employees where the third letter of the name is a.

SELECT
FROM
WHERE
AND

last_name, job_id, salary


employees
job_id IN ('SA_REP', 'ST_CLERK')
salary NOT IN (2500, 3500, 7000);

15. Modify lab_03_06.sql to display the last name, salary, and commission for all
employees whose commission amount is 20%. Save lab_03_06.sql as
lab_03_15.sql again. Rerun the statement in lab_03_15.sql.
SELECT
FROM
WHERE

last_name "Employee", salary "Monthly Salary",


commission_pct
employees
commission_pct = .20;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 9

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Restricting and Sorting Data


Chapter 3 - Page 10

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

s
) ha

co

u
ed ide

a
en t G4:u Using
s
i
Practices for
Lesson
m den
@
Single-Row
to
tu
ila s SFunctions
h
c
Customize
ar e thi Output
g
(
us 4
ron Chapter
o
d
t
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 1

Practices for Lesson 4: Overview


Practice Overview

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

This practice covers the following topics:

Writing a query that displays the current date

Creating queries that require the use of numeric, character, and date functions

Performing calculations of years and months of service for an employee

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 2

Practice 4-1: Using Single-Row Functions to Customize Output

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

Overview
This practice provides a variety of exercises using the different functions that are available for
character, number, and date data types. Remember that for nested functions, the results are
evaluated from the innermost function to the outermost function.
Tasks
1.

Write a query to display the system date. Label the column Date.
Note: If your database is remotely located in a different time zone, the output will be the
date for the operating system on which the database resides.

2.

s
) ha

The HR department needs a report to display the employee number, last name, salary, and
salary increased by 15.5% (expressed as a whole number) for each employee. Label the
column New Salary. Save your SQL statement in a file named lab_04_02.sql.

co

u
ed ide

3. Run your query in the lab_04_02.sql file.


a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 3

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

4.

Modify your query in lab_04_02.sql to add a column that subtracts the old salary from
the new salary. Label the column Increase. Save the contents of the file as
lab_04_04.sql. Run the revised query.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
illPerform
trafollowing tasks:
u
5.
the
n
G no
a. Write a query that displays the last name (with the first letter in uppercase and all the

other letters in lowercase) and the length of the last name for all employees whose
name starts with the letters J, A, or M. Give each column an appropriate label. Sort
the results by the employees last names.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 4

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

b.

Rewrite the query so that the user is prompted to enter the letter that the last name
starts with. For example, if the user enters H (capitalized) when prompted for a letter,
the output should show all employees whose last name starts with the letter H.

c. Modify the query such that the case of the letter that is entered does not affect the
output. The entered letter must be capitalized before being processed by the SELECT
query.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
If you have time, rcomplete
c atheb following exercises:
A
er wants to find the duration of employment for each employee. For
oHR department
f
6. The
s
m
r
an display the last name and calculate the number of months between
employee,
r
ille each
t
u
G ntoday
on and the date on which the employee was hired. Label the column as
MONTHS_WORKED. Order your results by the number of months employed. The number
of months must be rounded to the closest whole number.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 5

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

Note: Because this query depends on the date when it was executed, the values in the
MONTHS_WORKED column will differ for you.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 6

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

7. Create a query to display the last name and salary for all employees. Format the salary
to be 15 characters long, left-padded with the $ symbol. Label the column SALARY.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
8.
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 7

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

Create a query that displays the employees last names, and indicates the amounts of
their salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in
descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G 9. nCreate
o a query to display the last name and the number of weeks employed for all

employees in department 90. Label the number of weeks column as TENURE. Truncate
the number of weeks value to 0 decimal places. Show the records in descending order
of the employees tenure.

Note: The TENURE value will differ because it depends on the date on which you run the
query.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 8

Solution 4-1: Using Single-Row Functions to Customize Output


1.

Write a query to display the system date. Label the column Date.

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

Note: If your database is remotely located in a different time zone, the output will be the
date for the operating system on which the database resides.
SELECT
FROM
2.

sysdate "Date"
dual;

The HR department needs a report to display the employee number, last name, salary, and
salary increased by 15.5% (expressed as a whole number) for each employee. Label the
column New Salary. Save your SQL statement in a file named lab_04_02.sql.
SELECT
FROM

employee_id, last_name, salary,


ROUND(salary * 1.155, 0) "New Salary"
employees;

s
) ha

co

u
3. Run your query in the file lab_04_02.sql.
ed ide

a
en t Gu
SELECT employee_id, last_name, salary,
s
i
m den
ROUND(salary * 1.155, 0) "New Salary"
@
FROM
employees;
ila s Stu
h
c
ar e thi
g
(
usa column that subtracts the old salary
4. Modify your query in the lab_04_02.sql
ron ttooadd
d
l
from the new salary. Label the
Increase.
Save the contents of the file as
a column
e
u
s
n
G
lab_04_04.sql. Run the revised
cequery.
i
a
l
l
i
h ble last_name, salary,
SELECTrcemployee_id,
A
ra
ROUND(salary
* 1.155, 0) "New Salary",
e
o
f
s
m
ROUND(salary
* 1.155, 0) - salary "Increase"
r
e -tran
l
l
i
employees;
n
Gu noFROM
5.

Perform the following tasks:


a.

Write a query that displays the last name (with the first letter in uppercase and all the
other letters in lowercase) and the length of the last name for all employees whose
name starts with the letters J, A, or M. Give each column an appropriate label. Sort
the results by the employees last names.
SELECT

INITCAP(last_name) "Name",
LENGTH(last_name) "Length"
FROM
employees
WHERE
last_name LIKE 'J%'
OR
last_name LIKE 'M%'
OR
last_name LIKE 'A%'
ORDER BY last_name;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 9

b.

Rewrite the query so that the user is prompted to enter the letter that starts the last
name. For example, if the user enters H (capitalized) when prompted for a letter, the
output should show all employees whose last names start with the letter H.

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

SELECT

INITCAP(last_name) "Name",
LENGTH(last_name) "Length"
FROM
employees
WHERE
last_name LIKE '&start_letter%'
ORDER BY last_name;
c.

Modify the query such that the case of the letter that is entered does not affect the
output. The entered letter must be capitalized before being processed by the SELECT
query.
SELECT INITCAP(last_name) "Name",
LENGTH(last_name) "Length"
FROM
employees
WHERE
last_name LIKE UPPER('&start_letter%' )
ORDER BY last_name;

s
) ha

co

u
ed ide

a
en t Gu
s
If you have time, complete the following exercises:
i
m den
@
6. The HR department wants to find the duration of lemployment
employee. For
tuforofeach
i a thesnumber
S
h
each employee, display the last name and calculate
months
c
iLabel the column between
rwas hired.
h
a
t
today and the date on which the employee
g
(by the number
e of months employed. The number
s
n
MONTHS_WORKED. Order your results
u
o
drthe closest
towhole number.
l
of months must be rounded
to
a
e
u enonsthe date when it was executed, the values in the
G
Note: Because this query
depends
ic for you.
ldiffer
la lwill
i
MONTHS_WORKED
column
h
e
rc rab
A
SELECT
ROUND(MONTHS_BETWEEN(
e
o last_name,
f
s
m
r
hire_date)) MONTHS_WORKED
anSYSDATE,
r
ille FROM
t
employees
u
G non
ORDER BY months_worked;

7. Create a query to display the last name and salary for all employees. Format the salary
to be 15 characters long, left-padded with the $ symbol. Label the column SALARY.
SELECT last_name,
LPAD(salary, 15, '$') SALARY
FROM
employees;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 10

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

8. Create a query that displays employees last names, and indicates the amounts of their
salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in
descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
SELECT last_name,
rpad(' ', salary/1000, '*')
EMPLOYEES_AND_THEIR_SALARIES
FROM employees
ORDER BY salary DESC;
9. Create a query to display the last name and the number of weeks employed for all
employees in department 90. Label the number of weeks column as TENURE. Truncate
the number of weeks value to 0 decimal places. Show the records in descending order
of the employees tenure.

a
s
a
Note: The TENURE value will differ because it depends on the date when you run the
) hquery.
o
c

u
SELECT last_name, trunc((SYSDATE-hire_date)/7) AS TENURE
d
e uide
a
FROM
employees
n
ise ent G
WHERE department_id = 90
m
ORDER BY TENURE DESC;
la@ Stud
i
h
rc this
a
g
(
e
s
n
u
o
dr e to
l
a
u ens
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 11

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Using Single-Row Functions to Customize Output


Chapter 4 - Page 12

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

s
) ha

co

u
ed ide

a
en t G5:u Using
s
i
Practices for
Lesson
m den
@
Conversion
and
tu
ila s SFunctions
h
c
Conditional
ar e thi Expressions
g
(
us 5
ron Chapter
o
d
t
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 1

Practices for Lesson 5: Overview


Practice Overview

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

This practice covers the following topics:

Creating queries that use the TO_CHAR and TO_DATE functions.

Creating queries that use conditional expressions such as CASE , SEARCHED CASE, and
DECODE

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 2

Practice 5-1: Using Conversion Functions and Conditional


Expressions

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

Overview
This practice provides a variety of exercises using the TO_CHAR and TO_DATE functions, and
conditional expressions such as CASE, searched CASE, and DECODE.
Tasks
1. Create a report that produces the following for each employee:
<employee last name> earns <salary> monthly but wants <3 times salary.>.
Label the column Dream Salaries.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 3

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

2. Display each employees last name, hire date, and salary review date, which is the first
Monday after six months of service. Label the column REVIEW. Format the dates to
appear in a format that is similar to Monday, the Thirty-First of July, 2000.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 4

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

3. Create a query that displays employees last names and commission amounts. If an
employee does not earn commission, show No Commission. Label the column COMM.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 5

4. Using the CASE function, write a query that displays the grade of all employees based on
the value of the JOB_ID column, using the following data:

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

Job

Grade

AD_PRES
ST_MAN
IT_PROG
SA_REP
ST_CLERK
None of the above

A
B
C
D
E
0

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 6

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

5. Rewrite the statement in the preceding exercise by using the searched CASE syntax.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 7

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

6. Rewrite the statement in the preceding exercise by using the searched DECODE syntax.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 8

Solution 5-1: Using Conversion Functions and Conditional


Expressions

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

1.

Create a report that produces the following for each employee:


<employee last name> earns <salary> monthly but wants <3 times salary.>. Label
the column Dream Salaries.
SELECT

FROM
2.

last_name || ' earns '


|| TO_CHAR(salary, 'fm$99,999.00')
|| ' monthly but wants '
|| TO_CHAR(salary * 3, 'fm$99,999.00')
|| '.' "Dream Salaries"
employees;

Display each employees last name, hire date, and salary review date, which is the first
Monday after six months of service. Label the column REVIEW. Format the dates to appear
in a format that is similar to Monday, the Thirty-First of July, 2000.

s
) ha

co

u
ed ide

a
en t Gu
s
i
n
m deamounts.
3. Create a query that displays employees last names and
commission
If an
@
u
a
t
l
i
employee does not earn commission, show No Commission.
Label
the
column
COMM.
h is S
c
r
a e th
g
SELECT last_name,
(
NVL(TO_CHAR(commission_pct),
on to us 'No Commission') COMM
r
d
l
FROM
employees; a
u ense
G
4. Using the CASE function,
write aic
that displays the grade of all employees based on the
l query
lacolumn,
i
value of the JOB_ID
using
the
following data:
h
e
l
rc rab
A
Job o
Grade
fe
s
m
r
n
e
l
A
ilAD_PRES
tra
u
n
G ST_MAN
B
no
SELECT last_name, hire_date,
TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date, 6),'MONDAY'),
'fmDay, "the" Ddspth "of" Month, YYYY') REVIEW
FROM
employees;

IT_PROG
SA_REP
ST_CLERK
None of the above

C
D
E
0

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 9

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

SELECT job_id, CASE


WHEN
WHEN
WHEN
WHEN
WHEN
ELSE
FROM employees;

job_id
'ST_CLERK' THEN
'SA_REP'
THEN
'IT_PROG' THEN
'ST_MAN'
THEN
'AD_PRES' THEN
'0' END GRADE

'E'
'D'
'C'
'B'
'A'

5. Rewrite the statement in the preceding exercise by using the searched CASE syntax.
SELECT job_id, CASE
WHEN
WHEN
WHEN
WHEN
WHEN
ELSE
FROM employees;

job_id =
job_id =
job_id =
job_id =
job_id =
'0' END

'ST_CLERK'
'SA_REP'
'IT_PROG'
'ST_MAN'
'AD_PRES'
GRADE

THEN
THEN
THEN
THEN
THEN

'E'
'D'
'C'
'B'
'A'

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar by eusing
thithe searched DECODE syntax.
g
6. Rewrite the statement in the preceding exercise
(
ron to us
d
l
ua (job_id,
se
n
G
SELECT job_id, decode
e
lic
la le'ST_CLERK',
i
'E',
h
c
b
r
A fera 'SA_REP', 'D',
o
'IT_PROG',
'C',
rm rans
e
l
l
i
t
'ST_MAN',
'B',
Gu non'AD_PRES',
'0')GRADE

'A',

FROM employees;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Using Conversion Functions and Conditional Expressions


Chapter 5 - Page 10

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

s
) ha

co

u
ed ide

a
en t G6:u
s
i
Practices for
Lesson
m den
@
Reporting
Data
tu
ila sAggregated
S
h
c
Using
ar ethe
thi Group Functions
g
(
us 6
ron Chapter
o
d
t
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 1

Practices for Lesson 6: Overview


Practice Overview

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

This practice covers the following topics:

Writing queries that use group functions

Grouping by rows to achieve multiple results

Restricting groups by using the HAVING clause

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 2

Practice 6-1: Reporting Aggregated Data by Using Group Functions


Overview

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

After completing this practice, you should be familiar with using the group functions and
selecting groups of data.
Tasks
Determine the validity of the following statements. Circle either True or False.
1.

Group functions work across many rows to produce one result per group.
True/False

2.

Group functions include nulls in calculations.


True/False

3.

The WHERE clause restricts rows before inclusion in a group calculation.


True/False

s
) ha

co

u
ed ide

The HR department needs the following reports:


a
u
n
e
G
s
t
i
4. Find the highest, lowest, sum, and average salary of all
employees.
Label the columns
nresults
mRound
e
d
Maximum, Minimum, Sum, and Average, respectively.
your
to the
@
tu
la as lab_06_04.sql.
i
S
nearest whole number. Save your SQL statement
Run
the query.
h
c this
r
a
(g se
n
ro to u
d
l
ua ense to display the minimum, maximum, sum, and
G
5. Modify the query in lab_06_04.sql
a eachejoblictype. Save lab_06_04.sql as lab_06_05.sql again.
lfor
i
average salary
h
l
rc rainblab_06_05.sql.
Run the
statement
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 3

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

6.

Write a query to display the number of people with the same job.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
i
ar listing
ththem.
g
7. Determine the number of managers(without
Label the column Number of
e
s
n
u
o
Managers.
dr e to
l
a
u column
Hint: Use the MANAGER_ID
ns to determine the number of managers.
G
e
c
ila le li
h
c
Ar ferab
o
m ans
r
e
l
r difference between the highest and lowest salaries. Label the column
l
i 8. Find
tthe
u
n
G noDIFFERENCE.

Generalize the query so that the user in the HR department is prompted for a job title.
Save the script to a file named lab_06_06.sql. Run the query. Enter IT_PROG when
prompted.

If you have time, complete the following exercises:


9.

Create a report to display the manager number and the salary of the lowest-paid
employee for that manager. Exclude anyone whose manager is not known. Exclude
any groups where the minimum salary is $6,000 or less. Sort the output in descending
order of salary.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 4

If you want an extra challenge, complete the following exercises:

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

10. Create a query to display the total number of employees and, of that total, the number
of employees hired in 2005, 2006, 2007, and 2008. Create appropriate column
headings.

11. Create a matrix query to display the job, the salary for that job based on the department
number, and the total salary for that job, for departments 20, 50, 80, and 90, giving each
column an appropriate heading.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 5

Solution 6-1: Reporting Aggregated Data by Using Group Functions

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

Determine the validity of the following statements. Circle either True or False.
1.

Group functions work across many rows to produce one result per group.
True/False

2.

Group functions include nulls in calculations.


True/False

3.

The WHERE clause restricts rows before inclusion in a group calculation.


True/False

a
s
a
4. Find the highest, lowest, sum, and average salary of all employees. Label the columns
)h
onearest
Maximum, Minimum, Sum, and Average, respectively. Round your results to the
c
whole number. Save your SQL statement as lab_06_04.sql. Run theequery.
du de

i
a
u
n
SELECT ROUND(MAX(salary),0) "Maximum",
e
G
s
t
n
ROUND(MIN(salary),0) "Minimum", mi
e
ROUND(SUM(salary),0) "Sum", la@
tud
i
S
h
c this
ROUND(AVG(salary),0) "Average"
r
a
g
FROM
employees;
(
e
s
n
u
o
dr e to
l
a
u ensto display the minimum, maximum, sum, and average
5. Modify the query in lab_06_04.sql
G
ic
a Save
llab_06_04.sql
salary for each jobiltype.
as lab_06_05.sql again. Run the
h
e
l
c
statement A
in rlab_06_05.sql.
rab
e
o
f
s
SELECT n
job_id,
ROUND(MAX(salary),0) "Maximum",
rm
e
a
l
r
l
i
ROUND(MIN(salary),0) "Minimum",
t
Gu nonROUND(SUM(salary),0) "Sum",
The HR department needs the following reports:

ROUND(AVG(salary),0) "Average"
FROM
employees
GROUP BY job_id;

6.

Write a query to display the number of people with the same job.
SELECT job_id, COUNT(*)
FROM
employees
GROUP BY job_id;
Generalize the query so that the user in the HR department is prompted for a job title.
Save the script to a file named lab_06_06.sql. Run the query. Enter IT_PROG when
prompted and click OK.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 6

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

SELECT job_id, COUNT(*)


FROM
employees
WHERE job_id = '&job_title'
GROUP BY job_id;
7.

Determine the number of managers without listing them. Label the column Number of
Managers.
Hint: Use the MANAGER_ID column to determine the number of managers.
SELECT COUNT(DISTINCT manager_id) "Number of Managers"
FROM
employees;

8.

Find the difference between the highest and lowest salaries. Label the column
DIFFERENCE.

s
) ha

co

u
ed ide

a
u
n
e
G
s
t
i
If you have time, complete the following exercises:
m den
@
a salarySoftuthe lowest-paid employee
9. Create a report to display the manager number andilthe
h
c
for that manager. Exclude anyone whose manager
is
notiknown.
s Exclude any groups where
r
h
a
t
g
the minimum salary is $6,000 or less. Sort
( the output
e in descending order of salary.
s
n
u
o
r
SELECT
manager_id,ldMIN(salary)
to
a
e
FROM
employees
u ens
G
WHERE
manager_id
la le licIS NOT NULL
i
GROUP BY
manager_id
h
rc MIN(salary)
HAVING
> 6000
A
rab
e
o
f
s
ORDER BY
n MIN(salary) DESC;
erm
a
l
r
l
i
t
Gu nonSELECT
FROM

MAX(salary) - MIN(salary) DIFFERENCE


employees;

If you want an extra challenge, complete the following exercises:

10. Create a query that displays the total number of employees and, of that total, the number of
employees hired in 2005, 2006, 2007, and 2008. Create appropriate column headings.
SELECT

FROM

COUNT(*) total,
SUM(DECODE(TO_CHAR(hire_date,
SUM(DECODE(TO_CHAR(hire_date,
SUM(DECODE(TO_CHAR(hire_date,
SUM(DECODE(TO_CHAR(hire_date,
employees;

'YYYY'),2005,1,0))"2005",
'YYYY'),2006,1,0))"2006",
'YYYY'),2007,1,0))"2007",
'YYYY'),2008,1,0))"2008"

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 7

11. Create a matrix query to display the job, the salary for that job based on the department
number, and the total salary for that job, for departments 20, 50, 80, and 90, giving each
column an appropriate heading.

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

SELECT

job_id "Job",
SUM(DECODE(department_id
SUM(DECODE(department_id
SUM(DECODE(department_id
SUM(DECODE(department_id
SUM(salary) "Total"
FROM
employees
GROUP BY job_id;

,
,
,
,

20,
50,
80,
90,

salary))
salary))
salary))
salary))

"Dept
"Dept
"Dept
"Dept

20",
50",
80",
90",

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 8

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

s
) ha

co

u
ed ide

a
en t G7:u
s
i
Practices for
Lesson
m den
@
Displaying
tu from Multiple
ila s Data
S
h
c
Tables
hi Joins
ar e tUsing
g
(
us 7
ron Chapter
o
d
t
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 1

Practices for Lesson 7: Overview


Practice Overview

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

This practice covers the following topics:

Joining tables using an equijoin

Performing outer and self-joins

Adding conditions

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 2

Practice 7-1: Displaying Data from Multiple Tables by Using Joins


Overview

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

This practice is intended to give you experience in extracting data from multiple tables using the
SQL:1999compliant joins.
Tasks
1. Write a query for the HR department to produce the addresses of all the departments.
Use the LOCATIONS and COUNTRIES tables. Show the location ID, street address, city,
state or province, and country in the output. Use a NATURAL JOIN to produce the
results.

s
) ha

o
c

u
2. The HR department needs a report of all employees with corresponding departments.
e Write
ed foridthese

a query to display the last name, department number, and department


name
a
en t Gu
employees.
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 3

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

3.

The HR department needs a report of employees in Toronto. Display the last name, job,
department number, and the department name for all employees who work in Toronto.

4.

Create a report to display employees last names and employee numbers along with their
managers last names and manager numbers. Label the columns Employee, Emp#,
Manager, and Mgr#, respectively. Save your SQL statement as lab_07_04.sql. Run the
query.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no
5.

Modify lab_07_04.sql to display all employees, including King, who has no manager.
Order the results by employee number. Save your SQL statement as lab_07_05.sql.
Run the query in lab_07_05.sql.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 4

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

6.

Create a report for the HR department that displays employee last names, department
numbers, and all the employees who work in the same department as a given employee.
Give each column an appropriate label. Save the script to a file named lab_07_06.sql.

s
) ha

co

u
ed ide

a
en t Gu

s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rneeds
ab a report on job grades and salaries. To familiarize yourself with
A
7. The HR department
e
o
f
therJOB_GRADES
m ans table, first show the structure of the JOB_GRADES table. Then create a
e
l
l
i querynthat
trdisplays the name, job, department name, salary, and grade for all employees.
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 5

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar exercises:
thi
g
If you want an extra challenge, complete the (following
e
us of all employees who were hired after
ron thetonames
d
8. The HR department wants to determine
l
e and hire date of any employee hired after
ua ethensname
Davies. Create a query G
to display
employee Davies.ila
lic
h
e
l
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 6

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

9.

The HR department needs to find the names and hire dates of all employees who were
hired before their managers, along with their managers names and hire dates. Save the
script to a file named lab_07_09.sql.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 7

Solution 7-1: Displaying Data from Multiple Tables by Using Joins

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

1.

Write a query for the HR department to produce the addresses of all the departments. Use
the LOCATIONS and COUNTRIES tables. Show the location ID, street address, city, state or
province, and country in the output. Use a NATURAL JOIN to produce the results.
SELECT location_id, street_address, city, state_province,
country_name
FROM
locations
NATURAL JOIN countries;

2.

The HR department needs a report of all employees with corresponding departments. Write
a query to display the last name, department number, and department name for all the
employees.
SELECT last_name, department_id, department_name
FROM
employees
JOIN
departments
USING (department_id);

s
) ha

co

d name,
ejob,
elast

d
3. The HR department needs a report of employees in Toronto. Displayathe
i
u
enwork int Toronto.
department number, and department name for all employees iwho
G
s
n
m ded.department_name
SELECT e.last_name, e.job_id, e.department_id,
@
ilad s Stu
h
FROM
employees e JOIN departments
c
ar e thi
g
ON
(e.department_id = d.department_id)
(
JOIN
locations l
ron to us
d
l
USING (location_id)
ua ense
G
WHERE LOWER(l.city)
la le lic= 'toronto';
i
h
b employees last names and employee numbers along with their
rc to display
4. Create a report
A
raand
e
o
managers
last names
manager numbers. Label the columns Employee, Emp#,
f
s
m
r
n
Manager,
and
Mgr#,
respectively.
Save your SQL statement as lab_07_04.sql. Run the
e
illquery.n-tra
u
G no
SELECT w.last_name "Employee", w.employee_id "EMP#",
m.last_name "Manager", m.employee_id "Mgr#"
FROM
employees w JOIN employees m
ON
(w.manager_id = m.employee_id);

5.

Modify lab_07_04.sql to display all employees, including King, who has no manager.
Order the results by employee number. Save your SQL statement as lab_07_05.sql.
Run the query in lab_07_05.sql.
SELECT w.last_name "Employee", w.employee_id "EMP#",
m.last_name "Manager", m.employee_id "Mgr#"
FROM
employees w
LEFT
OUTER JOIN employees m
ON
(w.manager_id = m.employee_id)
ORDER BY 2;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 8

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

6.

Create a report for the HR department that displays employee last names, department
numbers, and all employees who work in the same department as a given employee. Give
each column an appropriate label. Save the script to a file named lab_07_06.sql. Run
the query.
SELECT e.department_id department, e.last_name employee,
c.last_name colleague
FROM
employees e JOIN employees c
ON
(e.department_id = c.department_id)
WHERE e.employee_id <> c.employee_id
ORDER BY e.department_id, e.last_name, c.last_name;

7.

The HR department needs a report on job grades and salaries. To familiarize yourself with
the JOB_GRADES table, first show the structure of the JOB_GRADES table. Then create a
query that displays the name, job, department name, salary, and grade for all employees.
DESC JOB_GRADES
/
SELECT e.last_name, e.job_id, d.department_name,
e.salary, j.grade_level
FROM
employees e JOIN departments d
ON
(e.department_id = d.department_id)
JOIN
job_grades j
ON
(e.salary BETWEEN j.lowest_sal AND j.highest_sal);

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
n following
usexercises:
rothe
If you want an extra challenge, complete
o
d
t
l
utoadetermine
sethe names of all employees who were hired after
8. The HR department wants
n
G
e
Davies. Create a query
lic the name and hire date of any employee hired after
la toldisplay
i
h
e
employee Davies.
rc rab
A
o e.last_name,
fe
SELECT
e.hire_date
s
m
r
n
e
a
l
r
l
i FROMt employees e JOIN employees davies
Gu noONn- (davies.last_name = 'Davies')
WHERE

9.

davies.hire_date < e.hire_date;

The HR department needs to find the names and hire dates of all employees who were
hired before their managers, along with their managers names and hire dates. Save the
script to a file named lab_07_09.sql.
SELECT w.last_name, w.hire_date, m.last_name, m.hire_date
FROM
employees w JOIN employees m
ON
(w.manager_id = m.employee_id)
WHERE
w.hire_date < m.hire_date;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 9

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 10

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

s
) ha

co

u
ed ide

a
en t G8:u Using
s
i
Practices for
Lesson
m den
@
Subqueries
ila s StotuSolve Queries
h
c
ar 8 thi
g
Chapter e
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Subqueries to Solve Queries


Chapter 8 - Page 1

Practices for Lesson 8: Overview


Practice Overview

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

This practice covers the following topics:

Creating subqueries to query values based on unknown criteria

Using subqueries to find values that exist in one set of data and not in another

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Subqueries to Solve Queries


Chapter 8 - Page 2

Practice 8-1: Using Subqueries to Solve Queries


Overview

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

In this practice, you write complex queries using nested SELECT statements.
For practice questions, you may want to create the inner query first. Make sure that it runs and
produces the data that you anticipate before you code the outer query.
Tasks
1.

The HR department needs a query that prompts the user for an employees last name. The
query then displays the last name and hire date of any employee in the same department
as the employee whose name the user supplies (excluding that employee). For example, if
the user enters Zlotkey, find all employees who work with Zlotkey (excluding Zlotkey).

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
licemployee number, last name, and salary of all employees
ladisplays
i
h
e
2. Create a report
that
the
l
rcthan rthe
abaverage salary. Sort the results in ascending order by salary.
A
who earn more
e
o
f
rm rans
e
l
l
i
t
Gu non-

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Subqueries to Solve Queries


Chapter 8 - Page 3

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

3.

Write a query that displays the employee number and last name of all employees who work
in a department with any employee whose last name contains the letter u. Save your SQL
statement as lab_08_03.sql. Run your query.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc so that
Modify theA
query
abthe user is prompted for a location ID. Save this to a file named
r
e
o
f
lab_08_04.sql.
rm rans
e
l
l
i
-t
n
G5.u Create
o
n a report for HR that displays the last name and salary of every employee who
4.

The HR department needs a report that displays the last name, department number, and
job ID of all employees whose department location ID is 1700.

reports to King.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Subqueries to Solve Queries


Chapter 8 - Page 4

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

6.

Create a report for HR that displays the department number, last name, and job ID for every
employee in the Executive department.

7.

Create a report that displays a list of all employees whose salary is more than the salary of
any employee from department 60.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
sfe the following exercise:
m
r
If youehave
time,acomplete
n
illModifynthe
trquery in lab_08_03.sql to display the employee number, last name, and
u
8.
G no
salary of all employees who earn more than the average salary, and who work in a
department with any employee whose last name contains the letter u. Save
lab_08_03.sql as lab_08_08.sql again. Run the statement in lab_08_08.sql.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Subqueries to Solve Queries


Chapter 8 - Page 5

Solution 8-1: Using Subqueries to Solve Queries

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

1.

The HR department needs a query that prompts the user for an employees last name. The
query then displays the last name and hire date of any employee in the same department
as the employee whose name the user supplies (excluding that employee). For example, if
the user enters Zlotkey, find all employees who work with Zlotkey (excluding Zlotkey).
--Execute the UNDEFINE command to remove a variable
UNDEFINE Enter_name
-- Execute the below SELECT statements to retrieve the values
from employees table

s
) ha

SELECT last_name, hire_date


FROM
employees
WHERE department_id = (SELECT department_id
FROM
employees
WHERE last_name = '&&Enter_name')
AND
last_name <> '&Enter_name';

co

u
ed ide

a
en t Gu
s
i
m den
@
tu one after the other or
Note: UNDEFINE and SELECT are individual queries;
them
ilaexecute
S
h
c
press Ctrl + A + F9 to run them together. ar
this
g
(
e
2. Create a report that displays the employee
number,
us last name, and salary of all employees
ron Sort
o
d
t
who earn more than the average
salary.
the
results in ascending order by salary.
l
a nse
u
G iclast_name,
SELECT employee_id,
salary
e
a
l
l
i
h ble
FROM
cemployees
r
A
ra> (SELECT AVG(salary)
WHERE
salary
e
o
f
s
FROM
employees)
erm -tran
l
l
i
u
n BY salary;
G noORDER
3.

Write a query that displays the employee number and last name of all employees who work
in a department with any employee whose last name contains the letter u. Save your SQL
statement as lab_08_03.sql. Run your query.
SELECT employee_id, last_name
FROM
employees
WHERE department_id IN (SELECT department_id
FROM
employees
WHERE last_name like '%u%');

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Subqueries to Solve Queries


Chapter 8 - Page 6

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

4.

The HR department needs a report that displays the last name, department number, and
job ID of all employees whose department location ID is 1700.
SELECT last_name, department_id, job_id
FROM
employees
WHERE department_id IN (SELECT department_id
FROM
departments
WHERE location_id = 1700);
Modify the query so that the user is prompted for a location ID. Save this to a file named
lab_08_04.sql.
SELECT last_name, department_id, job_id
FROM
employees
WHERE department_id IN (SELECT department_id
FROM
departments
WHERE location_id =
&Enter_location);

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
5. Create a report for HR that displays the last name and
oftu
every employee who
ila salary
S
h
reports to King.
c
ar e this
g
(
SELECT last_name, salaryn
ro to us
FROM
employees
d
l
ua= (SELECT
se employee_id
WHERE manager_id
n
G
e
la le licFROM employees
i
h
rc rab
WHERE last_name = 'King');
A
e
o
f
s
erm -tran
l
l
i
G6.u Create
nona report for HR that displays the department number, last name, and job ID for every
employee in the Executive department.

SELECT department_id, last_name, job_id


FROM
employees
WHERE department_id IN (SELECT department_id
FROM
departments
WHERE department_name =
'Executive');
7.

Create a report that displays a list of all employees whose salary is more than the salary of
any employee from department 60.
SELECT last_name FROM employees
WHERE salary > ANY (SELECT salary
FROM employees
WHERE department_id=60);
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Subqueries to Solve Queries


Chapter 8 - Page 7

If you have time, complete the following exercise:

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

8.

Modify the query in lab_08_03.sql to display the employee number, last name, and
salary of all employees who earn more than the average salary and who work in a
department with any employee whose last name contains the letter u. Save
lab_08_03.sql to lab_08_08.sql again. Run the statement in lab_08_08.sql.
SELECT employee_id, last_name, salary
FROM
employees
WHERE department_id IN (SELECT department_id
FROM
employees
WHERE last_name like '%u%')
AND
salary > (SELECT AVG(salary)
FROM
employees);

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Using Subqueries to Solve Queries


Chapter 8 - Page 8

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

s
) ha

co

u
ed ide

a
en t G9:u Using
s
i
Practices for
Lesson
m den
@
the Set
tu
ilaOperators
S
h
c
ar 9 this
g
Chapter e
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Using the Set Operators


Chapter 9 - Page 1

Practices for Lesson 9: Overview


Practice Overview

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

In this practice, you create reports by using the following:

UNION operator

INTERSECT operator

MINUS operator

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Using the Set Operators


Chapter 9 - Page 2

Practice 9-1: Using Set Operators


Overview

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

In this practice, you write queries using the set operators UNION, INTERSECT, and MINUS.
Tasks
1.

The HR department needs a list of department IDs for departments that do not contain the
job ID ST_CLERK. Use the set operators to create this report.

s
) ha

co

u
d de
elocated

2. The HR department needs a list of countries that have no departments


in
i them.
a
u
n
e
G
Display the country IDs and the names of the countries. Use the
set
operators
to
create this
is ent
m
report.
la@ Stud
i
h
rc this
a
g
(
e
s
n
u
o
3. Produce a list of all the employees
dr whoework
toin departments 50 and 80. Display the
l
a
u ensID by using the set operators.
employee ID, job ID, and
department
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

4.

Create a report that lists the detail of all employees who are sales representatives and are
currently working in the sales department.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Using the Set Operators


Chapter 9 - Page 3

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

5.

The HR department needs a report with the following specifications:


Last names and department IDs of all employees from the EMPLOYEES table,
regardless of whether or not they belong to a department
Department IDs and department names of all departments from the DEPARTMENTS
table, regardless of whether or not they have employees working in them
Write a compound query to accomplish this report.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Using the Set Operators


Chapter 9 - Page 4

Solution 9-1: Using Set Operators

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

1.

The HR department needs a list of department IDs for departments that do not contain the
job ID ST_CLERK. Use the set operators to create this report.
SELECT
FROM
MINUS
SELECT
FROM
WHERE

2.

department_id
departments
department_id
employees
job_id = 'ST_CLERK';

The HR department needs a list of countries that have no departments located in them.
Display the country IDs and the names of the countries. Use the set operators to create this
report.
SELECT country_id,country_name
FROM countries
MINUS
SELECT l.country_id,c.country_name
FROM locations l JOIN countries c
ON (l.country_id = c.country_id)
JOIN departments d
ON d.location_id=l.location_id;

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
s
n work inudepartments
rowho
3. Produce a list of all the employees
50 and 80. Display the
o
d
t
l
a
e
employee ID, job ID, and department
ID
by
using
the
set
operators.
u ens
G
la le licjob_id, department_id
SELECT employee_id,
i
h
rc rab
FROM A
EMPLOYEES
o department_id=50
fe
WHERE
s
m
r
n
e
ill UNION
traALL
u
n
G noSELECT employee_id, job_id, department_id
FROM EMPLOYEES
WHERE department_id=80;

4.

Create a report that lists the detail of all employees who are sales representatives and are
currently working in the sales department.
SELECT EMPLOYEE_ID
FROM EMPLOYEES
WHERE JOB_ID='SA_REP'
INTERSECT
SELECT EMPLOYEE_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID=80;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Using the Set Operators


Chapter 9 - Page 5

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

5.

The HR department needs a report with the following specifications:

Last names and department IDs of all employees from the EMPLOYEES table,
regardless of whether or not they belong to a department

Department IDs and department names of all departments from the DEPARTMENTS
table, regardless of whether or not they have employees working in them

Write a compound query to accomplish this report.


SELECT last_name,department_id,TO_CHAR(null)dept_name
FROM
employees
UNION
SELECT TO_CHAR(null),department_id,department_name
FROM departments;

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Using the Set Operators


Chapter 9 - Page 6

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

s
) ha

co

u
ed ide

a
u
en t G10:
s
i
Practices for
Lesson
m den
@
u
Manipulating
ila s StData
h
c
ar 10 thi
g
Chapter e
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 1

Practices for Lesson 10: Overview


Lesson Overview

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

This practice covers the following topics:

Inserting rows into tables

Updating and deleting rows in a table

Controlling transactions

Note: Before starting this practice, execute


/home/oracle/labs/sql1/code_ex /cleanup_scripts/cleanup_10.sql script.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 2

Practice 10-1: Managing Tables by Using DML Statements

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

Overview
The HR department wants you to create SQL statements to insert, update, and delete employee
data. As a prototype, you use the MY_EMPLOYEE table before giving the statements to the HR
department.
Notes

For all the DML statements, use the Run Script icon (or press F5) to execute the query.
Thus, you get to see the feedback messages on the Script Output tabbed page. For
SELECT queries, continue to use the Execute Statement icon or press F9 to get the
formatted output on the Results tabbed page.

Execute cleanup_10.sql script from /home/oracle/labs/sql1/code_ex


/cleanup_scripts/ before performing the following tasks.

s
) ha

co

u
1. Create a table called MY_EMPLOYEE.
ed ide

a
u
n names.
ecolumn
G
2. Describe the structure of the MY_EMPLOYEE table to identify the
s
t
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no
Tasks

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 3

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

3.

Create an INSERT statement to add the first row of data to the MY_EMPLOYEE table from
the following sample data. Do not list the columns in the INSERT clause. Do not enter all
rows yet.

ID

LAST_NAME

FIRST_NAME

USERID

SALARY

Patel

Ralph

rpatel

895

Dancs

Betty

bdancs

860

Biri

Ben

bbiri

1100

s
) ha

co

u
d de
e1550

i
a
5
Ropeburn
Audrey
aropebur
u
n
e
G
is ent
m
la@ Stud
i
h
4. Populate the MY_EMPLOYEE table with the second
rc rowthofisthe sample data from the
a
g
preceding list. This time, list the columns( explicitly in
e the INSERT clause.
s
n
u
o
5. Confirm your addition to the table.
dr e to
l
a
u ens
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ra statement in a dynamic reusable script file to load the remaining rows into
6.
illWrite nan-tINSERT
u
G thenoMY_EMPLOYEE table. The script should prompt for all the columns (ID, LAST_NAME,
4

Newman

Chad

cnewman

750

FIRST_NAME, USERID, and SALARY). Save this script to a lab_10_06.sql file.

7.

Populate the table with the next two rows of the sample data listed in step 3 by running the
INSERT statement in the script that you created.

8.

Confirm your additions to the table.

9.

Make the data additions permanent.

Update and delete data in the MY_EMPLOYEE table.


10. Change the last name of employee 3 to Drexler.
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 4

11. Change the salary to $1,000 for all employees who have a salary less than $900.

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

12. Verify your changes to the table.

13. Delete Betty Dancs from the MY_EMPLOYEE table.


14. Confirm your changes to the table.

s
) ha

co

u
ed ide

a
en t Gu
s
i
Control the data transaction to the MY_EMPLOYEE table.
m den
@
u
ila listed
16. Populate the table with the last row of the samplehdata
in tstep 3 by using the
S
c
statements in the script that you created in step
ar 6. eRunththeisstatements in the script.
g
(
s
n sessionuonly.
Note: Perform the steps (17-23) in o
r one
o
d
t
l
a nse
17. Confirm your addition to the
utable.
G
ce
i
a
l
l
i
rch rable
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

15. Commit all pending changes.

18. Mark an intermediate point in the processing of the transaction.


19. Delete all the rows from the MY_EMPLOYEE table.
20. Confirm that the table is empty.
21. Discard the most recent DELETE operation without discarding the earlier INSERT operation.
22. Confirm that the new row is still intact.

23. Make the data addition permanent.


Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 5

If you have time, complete the following exercise:

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

24. Modify the lab_10_06.sql script such that the USERID is generated automatically by
concatenating the first letter of the first name and the first seven characters of the last
name. The generated USERID must be in lowercase. Therefore, the script should not
prompt for the USERID. Save this script to a file named lab_10_24.sql.
25. Run the lab_10_24.sql script to insert the following record:

ID

LAST_NAME

FIRST_NAME

USERID

SALARY

Anthony

Mark

manthony

1230

26. Confirm that the new row was added with the correct USERID.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 6

Solution 10-1: Managing Tables by Using DML Statements


Insert data into the MY_EMPLOYEE table.

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

1.

Create a table called MY_EMPLOYEE.


CREATE TABLE my_employee
(id NUMBER(4) CONSTRAINT my_employee_id_pk PRIMARY Key,
last_name VARCHAR2(25),
first_name VARCHAR2(25),
userid VARCHAR2(8),
salary NUMBER(9,2));

2.

Describe the structure of the MY_EMPLOYEE table to identify the column names.
DESCRIBE my_employee

s
) ha

co

d table
3. Create an INSERT statement to add the first row of data to the MY_EMPLOYEE
from
e
e

d
i
a
the following sample data. Do not list the columns in the INSERT clause.
en t Gu
s
i
m den
@
ID
LAST_NAME
FIRST_NAME ila USERID
SALARY
tu
S
h
c
ar e this
g
(
1
Patel
rpatel
895
on to us
rRalph
d
l
ua ense
G
2
Dancs
bdancs
860
la le lic Betty
i
h
rc rab
A
fe
3o
Biri
Ben
bbiri
1100
s
m
r
n
e
a
l
r
l
i
t
Gu non4

Newman

Chad

cnewman

750

Ropeburn

Audrey

aropebur

1550

INSERT INTO my_employee


VALUES (1, 'Patel', 'Ralph', 'rpatel', 895);
4.

Populate the MY_EMPLOYEE table with the second row of the sample data from the
preceding list. This time, list the columns explicitly in the INSERT clause.
INSERT INTO my_employee (id, last_name, first_name,
userid, salary)
VALUES (2, 'Dancs', 'Betty', 'bdancs', 860);
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 7

5.

Confirm your additions to the table.

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

SELECT
FROM
6.

*
my_employee;

Write an INSERT statement in a dynamic reusable script file to load the remaining rows into
the MY_EMPLOYEE table. The script should prompt for all the columns (ID, LAST_NAME,
FIRST_NAME, USERID, and SALARY). Save this script to a file named lab_10_06.sql.
INSERT INTO my_employee
VALUES (&p_id, '&p_last_name', '&p_first_name',
'&p_userid', &p_salary);

7.

Populate the table with the next two rows of the sample data listed in step 3 by running the
INSERT statement in the script that you created.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
8. Confirm your additions to the table.
h
c
ar e thi
g
SELECT
*
(
FROM my_employee;
ron to us
d
l
ua ense
G
lic
la permanent.
i
9. Make the data additions
h
e
l
rc rab
A
COMMIT;
o
fe
s
m
r
n
e
ill n-tra
u
GUpdatenand
o delete data in the MY_EMPLOYEE table.
INSERT INTO my_employee
VALUES (&p_id, '&p_last_name', '&p_first_name',
'&p_userid', &p_salary);

10. Change the last name of employee 3 to Drexler.


UPDATE
SET
WHERE

my_employee
last_name = 'Drexler'
id = 3;

11. Change the salary to $1,000 for all employees with a salary less than $900.
UPDATE
SET
WHERE

my_employee
salary = 1000
salary < 900;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 8

12. Verify your changes to the table.

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

SELECT
FROM

*
my_employee;

13. Delete Betty Dancs from the MY_EMPLOYEE table.


DELETE
FROM my_employee
WHERE last_name = 'Dancs';
14. Confirm your changes to the table.
SELECT
FROM

*
my_employee;

s
) ha

co

u
15. Commit all pending changes.
ed ide

a
COMMIT;
en t Gu
s
i
m den
@
ila s Stu
h
Control the data transaction to the MY_EMPLOYEE table.
c
i
r
h
a
t
g
(
e
16. Populate the table with the last row of the samplesdata listed in step 3 by using the
u6. Run the statements in the script.
statements in the script that youdcreated
ron intstep
o
l
ua ense
G
lic
lamy_employee
INSERT INTO
i
h
e
l
rc(&p_id,
VALUES
A
rab'&p_last_name', '&p_first_name',
e
o
f
&p_salary);
s
rm '&p_userid',
n
e
a
l
r
l
i
t
Gu nonNote: Perform the steps (17-23) in one session only.

17. Confirm your addition to the table.


SELECT
FROM

*
my_employee;

18. Mark an intermediate point in the processing of the transaction.


SAVEPOINT step_17;
19. Delete all the rows from the MY_EMPLOYEE table.
DELETE
FROM my_employee;
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 9

20. Confirm that the table is empty.

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

SELECT *
FROM
my_employee;
21. Discard the most recent DELETE operation without discarding the earlier INSERT operation.
ROLLBACK TO step_17;
22. Confirm that the new row is still intact.
SELECT *
FROM
my_employee;

s
) ha

23. Make the data addition permanent.

co

u
ed ide

a
en t Gu
If you have time, complete the following exercise:
s
i
n
mgenerated
e
d
24. Modify the lab_10_06.sql script such that the USERID
is
automatically by
@
tucharacters
lafirst seven
i
S
concatenating the first letter of the first name and
the
of the last
h
isscript should, therefore,
rc tThe
h
a
name. The generated USERID must be inglowercase.
not
(
e
s lab_10_24.sql.
prompt for the USERID. Save this script
ron to taofileunamed
d
l
SET ECHO OFF
ua ense
G
SET VERIFY OFF
a e lic
l
i
h
l
INSERTrc
INTO my_employee
b
a
A
VALUES
er '&&p_last_name', '&&p_first_name',
o (&p_id,
f
s
m
r
1, 1) ||
ran
ille n-lower(substr('&p_first_name',
tsubstr('&p_last_name',
u
1, 7)), &p_salary);
G no
COMMIT;

SET VERIFY ON
SET ECHO ON
UNDEFINE p_first_name
UNDEFINE p_last_name
25. Run the lab_10_24.sql script to insert the following record:

ID

LAST_NAME

FIRST_NAME

USERID

SALARY

Anthony

Mark

manthony

1230

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 10

26. Confirm that the new row was added with the correct USERID.

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

SELECT *
FROM my_employee
WHERE ID='6';

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 11

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 10: Manipulating Data


Chapter 10 - Page 12

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

s
) ha

co

u
ed ide

a
u
en t G11:
s
i
Practices for
Lesson
m den
@
Usingila
DDL Statements
to
tu
S
h
c
Create
ar e and
this Manage Tables
g
(
us 11
ron Chapter
o
d
t
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 1

Practices for Lesson 11: Overview


Lesson Overview

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

This practice covers the following topics:

Creating new tables

Creating a new table by using the CREATE TABLE AS syntax

Verifying that tables exist

Altering tables

Adding columns

Dropping columns

Setting a table to read-only status

Dropping tables

s
) ha

co

u
Note: Before starting this practice, execute the
ed ide

a
/home/oracle/labs/sql1/code_ex/cleanup_scripts/cleanup_11.sql
en t Guscript.
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 2

Practice 11-1: Introduction to Data Definition Language

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

Overview
In this practice, you create new tables by using the CREATE TABLE statement. Confirm that the
new table was added to the database. You also learn to set the status of a table as READ ONLY,
and then revert to READ/WRITE. You use the ALTER TABLE command to modify table columns.
Notes

For all the DDL and DML statements, click the Run Script icon (or press F5) to execute
the query in SQL Developer. Thus, you get to see the feedback messages on the Script
Output tabbed page. For SELECT queries, continue to click the Execute Statement icon
or press F9 to get the formatted output on the Results tabbed page.

Execute the cleanup_11.sql script from


/home/oracle/labs/sql1/code_ex/cleanup_scripts/cleanup_11.sql
before performing the following tasks.

s
) ha

o
c

u
1. Create the DEPT table based on the following table instance chart. Save the
statement
ethe in
edto create
d
i
the lab_11_01.sql script, and then execute the statement in thenscript
a
u
e
G
table. Confirm that the table is created.
s
t
i
m den
@
ID
Column Name
tu
ila s SNAME
h
c
Primary keyar
Key Type
thi
g
(
e
Nulls/Unique
ron to us
d
l
FK Table
ua ense
G
FK Column
la le lic
i
h
rc rab NUMBER
VARCHAR2
Data type
A
e
o
f
7
25
Length
rm rans
e
l
l
i
t
Gu nonTasks

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 3

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

2. Create the EMP table based on the following table instance chart. Save the statement in the
lab_11_02.sql script, and then execute the statement in the script to create the table.
Confirm that the table is created.

Column Name ID

LAST_NAME

FIRST_NAME

DEPT_ID

Key Type
Nulls/Unique
FK Table

DEPT

FK Column

ID

Data type

NUMBER

VARCHAR2

VARCHAR2

NUMBER

Length

25

25

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
a nse column of the NUMBER data type, with precision 2
3. Modify the EMP table. Add auCOMMISSION
G
and scale 2. Confirmayour modification.
ce
i
l
l
i
rch rable
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

4. Modify the EMP table to allow for longer employee last names. Confirm your modification.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 4

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

5. Drop the FIRST_NAME column from the EMP table. Confirm your modification by checking
the description of the table.

6. In the EMP table, mark the DEPT_ID column as UNUSED. Confirm your modification by
checking the description of the table.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
7. Drop all the UNUSED columns from thenEMP table. s
u
dro onethetostructure
l
8. Create the EMPLOYEES2 tableabased
of the EMPLOYEES table. Include only
u eLAST_NAME,
s
n
the EMPLOYEE_ID, FIRST_NAME,
SALARY, and DEPARTMENT_ID columns.
G
c ID, FIRST_NAME, LAST_NAME, SALARY, and
litable
linayour lnew
Name the columns
i
h
e
rc rab
DEPT_ID, respectively.
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

9. Alter the status of the EMPLOYEES2 table to read-only.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 5

10. Try to add a column JOB_ID in the EMPLOYEES2 table.

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

Note: You will get the Update operation not allowed on table error message. You will not
be allowed to add any column to the table because it is assigned a read-only status.

s
) ha

11. Revert the EMPLOYEES2 table to read/write status. Now try to add the same column again.

co

u
ed ide

a
You should get the following messages:
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Now, because the table is assigned a READ WRITE status, you will be allowed to add a
column to the table.

12. Drop the EMP, DEPT, and EMPLOYEES2 table.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 6

Solution 11-1: Introduction to Data Definition Language

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

1.

Create the DEPT table based on the following table instance chart. Save the statement in a
script called lab_11_01.sql, and then execute the statement in the script to create the
table. Confirm that the table is created.
Column Name

ID

NAME

Key Type

Primary key

Nulls/Unique
FK Table
FK Column
Data type

NUMBER

VARCHAR2

Length

25

s
) ha

co

u
ed ide

a
CREATE TABLE dept
u
n
ePRIMARY
G
s
t
i
(id
NUMBER(7)CONSTRAINT department_id_pk
KEY,
m den
@
name VARCHAR2(25));
la Stu
i
h
c
To confirm that the table was created and to view
ar itsestructure,
this issue the following command:
g
(
DESCRIBE dept;
ron to us
d
l
a the following
uon
se table instance chart. Save the statement in a
2. Create the EMP table based
n
G
e
la le licand then execute the statement in the script to create the
script called lab_11_02.sql,
i
h
b is created.
rcthat the
table. Confirm
A
ratable
e
o
f
s
rm
n
e
a
l
LAST_NAME
FIRST_NAME
DEPT_ID
r
l
i Column
t Name ID
u
n
G noKey Type
Nulls/Unique
FK Table

DEPT

FK Column

ID

Data type

NUMBER

VARCHAR2

VARCHAR2

NUMBER

Length

25

25

CREATE TABLE emp


(id
NUMBER(7),
last_name
VARCHAR2(25),
first_name
VARCHAR2(25),
dept_id
NUMBER(7)
CONSTRAINT emp_dept_id_FK REFERENCES dept (id)
);
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 7

To confirm that the table was created and to view its structure:

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

DESCRIBE emp
3. Modify the EMP table. Add a COMMISSION column of the NUMBER data type, with precision 2
and scale 2. Confirm your modification.
ALTER TABLE emp
ADD commission NUMBER(2,2);
DESCRIBE emp

4. Modify the EMP table to allow for longer employee last names. Confirm your modification.

s
) ha

co

u
ed ide

a
DESCRIBE emp
en t Gu
s
i
m den
@
ila s Stu
h
c
r
atable.
thi your modification by checking
g
(
5. Drop the FIRST_NAME column from the EMP
Confirm
e
the description of the table.
ron to us
d
l
ua ense
ALTER TABLE emp
G
lic
la lefirst_name;
DROPhiCOLUMN
rc emprab
DESCRIBE
A
o
fe
s
m
r
n
e
a
ilInl the EMP
trtable,
u
6.
mark the DEPT_ID column as UNUSED. Confirm your modification by
n
G no
ALTER TABLE emp
MODIFY (last_name VARCHAR2(50));

checking the description of the table.

ALTER TABLE emp


SET
UNUSED (dept_id);
DESCRIBE emp
7. Drop all the UNUSED columns from the EMP table.
ALTER TABLE emp
DROP UNUSED COLUMNS;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 8

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

8.

Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table. Include only
the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, and DEPARTMENT_ID columns.
Name the columns in your new table ID, FIRST_NAME, LAST_NAME, SALARY, and
DEPT_ID, respectively. Confirm that the table is created.
CREATE TABLE employees2 AS
SELECT employee_id id, first_name, last_name, salary,
department_id dept_id
FROM
employees;
DESCRIBE employees2

9. Alter the EMPLOYEES2 table status to read-only.


ALTER TABLE employees2 READ ONLY;

s
) ha

co

u
ed ide

a
en t Gu
s
i
ALTER TABLE employees2
m den
@
ADD job_id VARCHAR2(9);
ila s Stu
h
c
r
hi
a
t
g
(
e
11. Revert the EMPLOYEES2 table to the read/write
status.
s Now try to add the same column
n
u
o
r
again.
o
ld se t
a
u
Now, because the table G
is assigned
ena READ WRITE status, you will be allowed to add a
c
i
a
column to the table.
l
l
hi ble
c
r
a
ATABLE
ALTER
READ WRITE;
remployees2
e
o
f
s
n
erm
aTABLE
l
r
l
i
t
ALTER
employees2
u
G non
10. Try to add a column JOB_ID in the EMPLOYEES2 table.

Note: You will get the Update operation not allowed on table error message. You will not
be allowed to add any column to the table because it is assigned a read-only status.

ADD job_id VARCHAR2(9);


DESCRIBE employees2

12. Drop the EMP, DEPT, and EMPLOYEES2 table.


Note: You can even drop a table that is in READ ONLY mode. To test this, alter the table
again to READ ONLY status, and then issue the DROP TABLE command. The tables will be
dropped.
DROP TABLE emp;
DROP TABLE dept;
DROP TABLE employees2;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 9

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 11: Using DDL Statements to Create and Manage Tables
Chapter 11 - Page 10

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

s
) ha

co

u
ed ide

a
u
n
ise ent G
Additionalm
Practices
and
Solutions
la@ Stud
i
h
rc this
a
g
( Chapterse12
n
u
o
r
o
d
t
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 1

Practices for Lesson 1


Practices Overview

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

In these practices, you will be working on extra exercises that are based on the following topics:

Basic SQL SELECT statement

Basic SQL Developer commands


SQL functions

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 2

Practice 1-1: Additional Practice

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

Overview
In this practice, exercises have been designed to be worked on after you have discussed the
following topics: basic SQL SELECT statement, basic SQL Developer commands, and SQL
functions.

Tasks
1. The HR department needs to find data for all the clerks who were hired after 1997.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense needs a report on projected raises. The report
3. For budgeting purposes,G
the HR department
licwho have no commission, but who have a 10% raise in
laemployees
should display those
i
h
e
l
salary (roundrc
ab
A off the
rsalaries).
e
o
f
rm rans
e
l
l
i
t
Gu non2.

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

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 3

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

4.

Create a report of employees and their duration 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 duration of their employment. The
employee who has been employed the longest should appear at the top of the list.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
5. Show those employees
have
lic a last name starting with the letters J, K, L, or M.
la who
i
h
e
l
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 4

Unauthorized reproduction or distribution prohibited Copyright 2015, 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.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc canrbe
abused for extra practice after you have discussed the following
A
These exercises
e
o
f
s SELECT statements, basic SQL Developer commands, SQL functions,
topics:
SQL
rm basic
n
e
a
l
r
l
i joins, nand-t group functions.
G7.u Create
no 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 a location.
For example, if the user enters 1800, results are as follows:

8.

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

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 5

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

9.

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

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.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
11. Create a report that displays the jobs that are foundla
Administration
Executive
tu Show and
i inforthethese
S
h
departments. Also display the number of employees
jobs.
the
job
with the
c
is
r
h
a
t
highest number of employees first.
g
(
e
s
n
u
o
dr e to
l
a
u ens
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
illThesenexercises
tra can be used for extra practice after you have discussed the following
u
G no
topics: basic SQL SELECT statements, basic SQL Developer commands, SQL functions,
joins, group functions, and subqueries.

12. Show all the employees who were hired in the first half of the month (before the 16th of the
month, irrespective of the year).

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 6

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

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
14. Show all the employees
who
have
managers with a salary higher than $15,000. Show the
c
i
a
l
l
i
following data:cemployee
h bname,
le manager name, manager salary, and salary grade of the
manager. Ar
a
er
o
f
s
m
r
ille n-tran
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 7

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

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.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
16. Create a report to display the
uadepartment
se number and lowest salary of the department with
n
G
the highest average salary.
e
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ra that displays departments where no sales representatives work. Include the
illCreatena-treport
17.
u
G department
number, department name, manager ID, and location in the output.
no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 8

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

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

Employs fewer than three employees:

b.

Has the highest number of employees:

c.

Has the lowest number of employees:

s
) ha

co

ed idenumber,
19. Create a report that displays the employee number, last name, salary, department
a
u
and the average salary in their department for all employees. en
G
s
t
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 9

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

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 10

Solution 1-1: Additional Practice


Overview

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

Solutions to Additional Practice 1-1 are given as follows.

Tasks
1.

The HR department needs to find data for all the clerks who were hired after 1997.
SELECT *
FROM
employees
WHERE job_id = 'ST_CLERK'
AND hire_date > '31-DEC-1997';

s
) ha

2.

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

4.

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

co

u
ed ide

SELECT last_name, job_id, salary, commission_pct


a
en t Gu
FROM
employees
s
i
m den
WHERE commission_pct IS NOT NULL
@
ila s Stu
ORDER BY salary DESC;
h
c
ar e thi
g
(
us a report on projected raises. The report
ron toneeds
3. For budgeting purposes, the HR
department
d
l
should display those employees
ua who
sdoenot get a commission but who have a 10% raise in
n
G
e
salary (round off the
lic
lasalaries).
i
h
e
l
rc'The rsalary
SELECT
ab of '||last_name||' after a 10% raise is '
A
e
o
f
s || ROUND(salary*1.10) "New salary"
rm
n
e
a
l
r
l
i FROM
n-t employees
Gu noWHERE
commission_pct IS NULL;

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;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 11

5.

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

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

SELECT last_name
FROM
employees
WHERE SUBSTR(last_name, 1,1) IN ('J', 'K', 'L', 'M');
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;
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.

s
) ha

co

eand
edjob title,

d
7. Create a report that displays the department name, location ID, last name,
i
a
u
enthe user
salary of those employees who work in a specific location. Prompt
for a location.
G
s
t
i
m den
Enter 1800 for location_id when prompted.
@
ila s Stu
h
c
SELECT d.department_name, d.location_id,
ar e thi e.last_name, e.job_id,
g
e.salary
(
us d
FROM
employees e JOIN
rondepartments
o
d
t
l
ON
e.department_id
ua =end.department_id
se
G
AND
d.location_id
la le lic = &location_id;
i
h
rc rab
A
onumbersoffeemployees who have a last name that ends with the letter n. Create
8. Findm
the
r
n
e
asolutions.
r
illtwo possible
t
u
n
G noSELECT
COUNT(*)
FROM
WHERE
--or
SELECT
FROM
WHERE
9.

employees
last_name LIKE '%n';
COUNT(*)
employees
SUBSTR(last_name, -1) = 'n';

Create a report that shows the name, location, and number of employees for each
department. Make sure that the report also includes department_IDs without employees.
SELECT d.department_id, d.department_name,
d.location_id,
COUNT(e.employee_id)
FROM
employees e RIGHT OUTER JOIN departments d
ON
e.department_id = d.department_id
GROUP BY d.department_id, d.department_name, d.location_id;
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 12

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

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

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;

s
) ha

co

u
ed ide

a
en t Gu
s
i
n the following
These exercises can be used for extra practice after you have
m discussed
e
d
@
topics: basic SQL SELECT statements, basic SQL Developer
SQL functions,
tu
ila s Scommands,
h
joins, group functions, and subqueries.
c
ar e thi
g
(
us
roinn the tfirst
o
d
12. Show all employees who were
hired
l
a nse half of the month (before the 16th of the
u
month, irrespective of the
year).
G ice
a
l
l
i
SELECT last_name,
hire_date
h
e
l
c
r
FROM A employees
rab
e
o
f
s
WHERE n
TO_CHAR(hire_date,
'DD') < 16;
rm
e
a
l
r
l
i
t
Gu non-

13. Create a report that displays the following for all employees: last name, salary, and salary
expressed in terms of thousands of dollars.
SELECT last_name, salary, TRUNC(salary, -3)/1000
FROM
employees;

Thousands

14. Show all employees who have managers with a salary higher than $15,000. Show the
following data: employee name, manager name, manager salary, and salary grade of the
manager.
SELECT e.last_name, m.last_name manager, m.salary,
j.grade_level
FROM
employees e JOIN employees m
ON
e.manager_id = m.employee_id
JOIN
job_grades j
ON
m.salary BETWEEN j.lowest_sal AND j.highest_sal
AND
m.salary > 15000;
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 13

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

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

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;

s
) ha

co

u
ed ide

a
en t Gu
s
i
m salarydeofnthe department with
16. Create a report to display the department number and lowest
@
the highest average salary.
ila s Stu
h
c
ar e thi
SELECT department_id, MIN(salary)
g
(
FROM
employees
ron to us
d
l
GROUP BY department_id
ua ense
G
HAVING AVG(salary)
=c(SELECT MAX(AVG(salary))
i
a
l
l
i
h
e
FROM
employees
l
c
b
r
a
A
GROUP BY department_id);
er
o
f
s
m
r
ille n-tran
u
G17. Create
no a report that displays the departments where no sales representatives work. Include
the department number, department name, manager ID, 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);

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 14

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:

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

a.

Employs fewer than three employees:


SELECT d.department_id, d.department_name, COUNT(*)
FROM
departments d JOIN employees e
ON
d.department_id = e.department_id
GROUP BY d.department_id, d.department_name
HAVING COUNT(*) < 3;

b. 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);

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
c. Has the lowest number of employees: rch
a e thi
g
(
SELECT d.department_id, n
d.department_name,
COUNT(*)
s
u
o
r
o
FROM
departments dldJOIN employees
e
t
a
e
u
s
ON
d.department_id
G icen= e.department_id
a
l
l
i
GROUP BY hd.department_id,
d.department_name
e
l
c
b
r
HAVING
A COUNT(*)
ra = (SELECT MIN(COUNT(*))
e
o
f
s
FROM
employees
erm -tran
l
l
i
GROUP
BY
department_id);
Gu 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.
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;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 15

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

20. Create an anniversary overview based on the hire date of employees. Sort the
anniversaries in ascending order.
SELECT last_name, TO_CHAR(hire_date, 'Month DD') BIRTHDAY
FROM
employees
ORDER BY TO_CHAR(hire_date, 'DDD');

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 16

Case Study: Online Book Store

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

Overview
In this case study, you build a set of database tables for an online book store (E-Commerce
Shopping Cart). After you create the tables, you insert, update, and delete records in the book
store database and generate a report. The database contains only the essential tables.
The following is a diagram of the table and columns for the online book store application:

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
GNote: nIf oyou want to build the tables, you can execute the commands in the

Online_Book_Store_Create_Table.sql script in SQL Developer. If you want to drop the


tables, you can execute the commands in the Online_Book_Store_Drop_Tables.sql
script in SQL Developer. Then you can execute the commands in the
<<Online_Book_Store_Populate.sql>> script in SQL Developer to create and populate
the tables.
All the three SQL scripts are present in the /home/oracle/labs/sql1/labs folder.

If you use the Online_Book_Store_Create_Table.sql script to build the tables,


start with step 2.

If you use the Online_Book_Store_Drop_Tables.sql script to remove the tables,


start with step 1.

If you use the Online_Book_Store_Populate.sql script to build and populate the


tables, start with step 6.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 17

Practice 1-2

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

Overview
In this practice, you create the tables based on the following table instance charts. Select the
appropriate data types and be sure to add integrity constraints.
Tasks
1.

Table Details
a.

Table Name: AUTHOR

Column
Author_ID
Author_Name
b.

Data type
VARCHAR2
VARCHAR2

Key
PK

Table Dependent Type

Table Name: BOOKS

s
) ha

co

u
ed ide

a
u
n
e
G
s
t
i
FK
AUTHORS
m den
@
tu
ila PUBLISHER
S
h
FK
c
ar e this
g
(
c. Table Name: CUSTOMER
us
ron toKey
d
Column Name
Data type
Table Dependent On
l
a
e
u
s
Customer_ID
VARCHAR2
PK
G icen
a
l
l
Customer_Name
VARCHAR2
hi VARCHAR2
le
c
b
r
Street_Address
a
A
er VARCHAR2
f
City mo
s
r
Phone_Number
VARCHAR2
ille n-tran
u
GCredit_Card_Number
o
VARCHAR2
FK
Credit_Card_Details
n
Column
Book_ID
Book_Name
Author_ID
Price
Publisher_ID

Datatype
VARCHAR2
VARCHAR2
VARCHAR2
NUMBER
VARCHAR2

Key
PK

Table Dependent On

d. CREDIT_CARD_DETAILS

Column Name
Credit_Card_Number
Credit_Card_Type
Expiry_Date
e.

Data type
VARCHAR2
VARCHAR2
DATE

Key
PK

Table Dependent On

Table Name: ORDER_DETAILS

Column
Order_ID
Customer_ID
Shipping_Type
Date_of_Purchase
Shopping_Cart_ID

Data type
NUMBER
VARCHAR2
VARCHAR2
DATE
NUMBER

Key
PK
FK
FK

Table Dependent On
CUSTOMER
SHIPPING_TYPE

FK

SHOPPING_CART

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 18

f.

Table Name: PUBLISHER

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

Column
Publisher_ID
Publisher_Name
g.

Key
PK

Table Dependent Type

Table Name: PURCHASE_HISTORY

Column
Customer_ID
Order_ID
h.

Data type
VARCHAR2
VARCHAR2
Data type
VARCHAR2
NUMBER

Key
FK
FK

Table Dependent Type


CUSTOMER
ORDER_DETAILS

Table Name: SHIPPING_TYPE

Column
Shipping_Type
Shipping_Price

Data type
VARCHAR2
NUMBER

Key
PK

Table Dependent Type

s
) ha

co

u
ed ide

a
en t Gu
BOOKS
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
a constraints
2. Add additional Referential u
Integrity
se to the tables created.
n
G
e
lic properly by checking in the Connections Navigator in
lawerelecreated
3. Verify that the tables
i
h
SQL Developer.
rc rab
A
oa sequence
feto uniquely identify each row in the ORDER_DETAILS table.
4. Create
s
m
r
n
e
ra 100; do not allow caching of the values. Name the sequence
illa. Start
twith
u
n
G noORDER_ID_SEQ.
i. Table Name: SHOPPING_CART
Column
Data type
Shopping_Cart_ID NUMBER
Book_ID
VARCHAR2
Price
NUMBER
Date
DATE
Quantity
NUMBER

Key
PK
FK

Table Dependent On

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 19

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

b.

Verify the existence of the sequences in the Connections Navigator in SQL Developer.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar eof data
thi to be added.
g
5. Add data to the tables. Create a script for
each set
(
ron to us
Add data to the following tables:
d
l
ua ense
G
a. AUTHOR
la le lic
i
h
b. PUBLISHER
rc rab
A
fe
c.o SHIPPING_TYPE
s
m
r
n
e
a
ill nd. -trCUSTOMER
u
G no
e.

CREDIT_CARD_DETAILS

f.

BOOKS

g.

SHOPPING_CART

h.

ORDER_DETAILS

i.

PURCHASE_HISTORY

Note: Save the scripts using the task number. For example, to save the script created
for the BOOKS table, you can save it as labs_apcs_5a_1.sql. Ensure that you save
the scripts in the /home/oracle/labs folder.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 20

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

6.

Create a view named CUSTOMER_DETAILS to show the Customer Name, Customer


Address, and the details of the order placed by the customer. Order the results by
Customer ID.

7.

Make changes to the data in the tables.


a.

s
) ha

Add a new book detail. Verify if the author detail for the book is available in the AUTHOR
table. If not, make an entry in the AUTHOR table.

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G b.noEnter a shopping cart detail for the book details that you just entered in 7(a).

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 21

8.

Create a report that contains each customers history of purchasing books. Be sure to
include the customer name, customer ID, book ID, date of purchase, and shopping cart ID.
Save the commands that generate the report in a script file named lab_apcs_8.sql.

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

Note: Your results may be different.

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 22

Solution 1-2
Overview

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

The solution to Practice 1-2 is given as follows.

Tasks
1.

Table Details
a. AUTHOR
CREATE TABLE AUTHOR
(
Author_ID VARCHAR2 (10) NOT NULL ,
Author_Name VARCHAR2 (20)
)

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
COMMENT ON TABLE AUTHOR IS 'Author'
@
ila s Stu
;
h
c
ar e thi
g
(
on to us
r
d
l
ALTER TABLE AUTHORa
u AUTHOR_PK
se PRIMARY KEY (Author_ID);
n
G
ADD CONSTRAINT
e
la le lic
i
h
rc rab
b. BOOKS
A
o
fe BOOKS
s
m
r
n
CREATE
TABLE
e
ill n-t(ra
u
G no
;

Book_ID VARCHAR2 (10) NOT NULL ,


Book_Name VARCHAR2 (50) ,
Author_ID VARCHAR2 (10) NOT NULL ,
Price NUMBER (10) ,
Publisher_ID VARCHAR2 (10) NOT NULL
)

;
COMMENT ON TABLE BOOKS IS 'Books'
;

ALTER TABLE BOOKS


ADD CONSTRAINT books_PK PRIMARY KEY ( Book_ID );
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 23

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

c.

CUSTOMER
CREATE TABLE CUSTOMER
(
Customer_ID VARCHAR2 (6) NOT NULL ,
Customer_Name VARCHAR2 (40) ,
Street_Address VARCHAR2 (50) ,
City VARCHAR2 (25) ,
Phone_Number VARCHAR2 (15) ,
Credit_Card_Number VARCHAR2 (20) NOT NULL
)
;
COMMENT ON TABLE CUSTOMER IS 'Customer'
;

s
) ha

co

u
ed ide

a
ALTER TABLE CUSTOMER
en t Gu
s
i
n
ADD CONSTRAINT Customer_PK PRIMARY KEY
) ;
m ( Customer_ID
e
d
@
ila s Stu
h
c
ar e thi
g
(
d. CREDIT_CARD_DETAILS ron
us
o
d
t
l
CREATE TABLE CREDIT_CARD_DETAILS
ua ense
G
(
la le lic
i
h
Credit_Card_Number
VARCHAR2 (20) NOT NULL ,
rc rab
A
o Credit_Card_Type
fe
VARCHAR2 (10) ,
s
m
r
n
e
a
DATE
ill n-trExpiry_Date
u
G no )
;

COMMENT ON TABLE CREDIT_CARD_DETAILS IS 'Credit Card Details'


;

ALTER TABLE CREDIT_CARD_DETAILS


ADD CONSTRAINT Credit_Card_Details_PK PRIMARY KEY (
Credit_Card_Number) ;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 24

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

e.

ORDER_DETAILS
CREATE TABLE ORDER_DETAILS
(
Order_ID VARCHAR2 (6) NOT NULL ,
Customer_ID VARCHAR2 (6) NOT NULL ,
Shipping_Type VARCHAR2 (10) NOT NULL ,
Date_of_Purchase DATE ,
Shopping_Cart_ID varchar2(6) NOT NULL
)
;
COMMENT ON TABLE ORDER_DETAILS IS 'Order Details'

s
) ha

;
ALTER TABLE ORDER_DETAILS
ADD CONSTRAINT ORDER_DETAILS_PK PRIMARY KEY (Order_ID ) ;

co

u
f. PUBLISHER
ed ide

a
en t Gu
CREATE TABLE PUBLISHER
s
i
m den
(
@
la NULLSt,u
iNOT
Publisher_ID VARCHAR2 (10)
h
c
r
a(50)
this
Publisher_Name VARCHAR2
g
(
e
)
ron to us
d
l
;
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
COMMENT
ON
e
ill ;n-tra TABLE PUBLISHER IS 'Publisher'
u
G no

ALTER TABLE PUBLISHER


ADD CONSTRAINT PUBLISHER_PK PRIMARY KEY ( Publisher_ID) ;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 25

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

g.

PURCHASE_HISTORY
CREATE TABLE PURCHASE_HISTORY
(
Customer_ID VARCHAR2 (6) NOT NULL ,
Order_ID VARCHAR2 (6) NOT NULL
)
;
COMMENT ON TABLE PURCHASE_HISTORY IS 'Purchase History'
;

h.

SHIPPING_TYPE

s
) ha

co

u
d de
e

i
a
NOT NULL , n
u
e
G
is ent
m
la@ Stud
i
h
rc this
a
g
(
e
s
n
u
o
COMMENT ON TABLE SHIPPING_TYPE
dr e to IS 'Shipping Type'
l
a
;
u ens
G
la le lic
i
h
c SHIPPING_TYPE
ALTER r
TABLE
A
rab SHIPPING_TYPE_PK PRIMARY KEY ( Shipping_Type
e
o
f
ADD
CONSTRAINT
s
rm
) ; ran
e
l
l
i
t
Gu nonCREATE TABLE SHIPPING_TYPE
(
Shipping_Type VARCHAR2 (10)
Shipping_Price NUMBER (6)
)
;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 26

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

i.

SHOPPING _CART
CREATE TABLE SHOPPING_CART
(
Shopping_Cart_ID VARCHAR2 (6) NOT NULL ,
Book_ID VARCHAR2 (10) NOT NULL ,
Price NUMBER (10) ,
Shopping_cart_Date DATE ,
Quantity NUMBER (6)
)
;
COMMENT ON TABLE SHOPPING_CART IS 'Shopping Cart'
;

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
2. Adding Additional Referential Integrity Constraints
tu Created
ila to stheSTable
h
c
r
hi
a
t
g
(
e
a. Include a Foreign Key constraintn
in the BOOKS
s table.
u
o
r
o
ld se t
a
u
ALTER TABLE BOOKS
G icen
a
lBOOKS_AUTHOR_FK FOREIGN KEY
l
ADD CONSTRAINT
i
h
e
l
c
(
Ar ferab
o
s
rm rAuthor_ID
n
e
a
l
l
i
t
Gu non- )REFERENCES AUTHOR

ALTER TABLE SHOPPING_CART


ADD CONSTRAINT SHOPPING_CART_PK PRIMARY KEY (SHOPPING_CART_ID)
;

(
Author_ID
)
;
ALTER TABLE BOOKS
ADD CONSTRAINT BOOKS_PUBLISHER_FK FOREIGN KEY
(
Publisher_ID
)
REFERENCES PUBLISHER
(
Publisher_ID
);
Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 27

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

b.

Include a Foreign Key constraint in the ORDER_DETAILS table.

ALTER TABLE ORDER_DETAILS


ADD CONSTRAINT Order_ID_FK FOREIGN KEY
(
Customer_ID
)
REFERENCES CUSTOMER
(
Customer_ID
)
;

s
) ha

co

u
d de
e

i
a
ALTER TABLE ORDER_DETAILS
u
n
e
G
is KEYent
ADD CONSTRAINT FK_Order_details FOREIGN
m
(
la@ Stud
i
h
Shipping_Type
rc this
a
g
(
e
)
s
n
u
o
REFERENCES SHIPPING_TYPE
dr e to
l
a
u ens
(
G
Shipping_Type
la le lic
i
h
rc rab
)
A
fe
; o
s
m
r
n
e
ill n-tra
u
G no
ALTER TABLE ORDER_DETAILS
ADD CONSTRAINT Order_Details_fk FOREIGN KEY
(
Shopping_Cart_ID
)
REFERENCES SHOPPING_CART
(
Shopping_Cart_ID
)
;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 28

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

c.

Include a Foreign Key constraint in the PURCHASE_HISTORY table.


ALTER TABLE PURCHASE_HISTORY
ADD CONSTRAINT Pur_Hist_ORDER_DETAILS_FK FOREIGN KEY
(
Order_ID
)
REFERENCES ORDER_DETAILS
(
Order_ID
)
;
ALTER TABLE PURCHASE_ HISTORY
ADD CONSTRAINT Purchase_History_CUSTOMER_FK FOREIGN KEY
(
Customer_ID
)
REFERENCES CUSTOMER
(
Customer_ID
) ;

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
a nse
uconstraint
d. Include a Foreign Key
G
e in the SHOPPING_CART table.
c
i
a
l
l
hi ble
c
r
a
ATABLE
rSHOPPING_CART
ALTER
e
o
f
s
erm -tADD
anCONSTRAINT SHOPPING_CART_BOOKS_FK FOREIGN KEY
l
r
l
i
u
G non (
Book_ID
)
REFERENCES BOOKS
(
Book_ID
)

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 29

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

3.

Verify that the tables were created properly by checking in the Connections Navigator in
SQL Developer. In the Connections Navigator, expand Connections > myconnection >
Tables.

4.

Create a sequence to uniquely identify each row in the ORDER DETAILS table.
a. Start with 100; do not allow caching of the values. Name the sequence ORDER_ID_SEQ.
CREATE SEQUENCE order_id_seq
START WITH 100
NOCACHE;
b.

Verify the existence of the sequences in the Connections Navigator in SQL Developer.
In the Connections Navigator, assuming that the myconnection node is expanded,
expand Sequences.
Alternatively, you can also query the user_sequences data dictionary view:

s
) ha

co

u
ed ide

a
5. Add data to the tables.
en t Gu
s
i
m den
a. AUTHOR Table
@
ila s Stu
h
Author_ID
Author_Name
c
ar e thi
g
(
AN0001
sOliver Goldsmith
n
u
o
r
o
AN0002
ald nse t Oscar Wilde
u
G ice
AN0003
George Bernard Shaw
a
l
l
i
h
e
l
AN0004rc
Leo Tolstoy
b
a
A
r
o
fe
AN0005
Percy Shelley
s
m
r
n
e
a
l
r
l
i
Lord Byron
n-t
Gu noAN0006
AN0007
John Keats
SELECT * FROM user_sequences;

AN0008

Rudyard Kipling

AN0009

P. G. Wodehouse

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 30

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

b.

PUBLISHER Table
Publisher_ID

Publisher_Name

PN0001

Elsevier

PN0002

Penguin Group

PN0003

Pearson Education

PN0004

Cambridge University Press

PN0005

Dorling Kindersley

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
c. SHIPPING _TYPE
h
c
ar e thi
g
(
ron to usShipping_Price
Shipping_Type
d
l
ua ense
G
USPS
200
c
i
a
l
l
i
FedEx ch
250
le
b
r
a
A
er
o
DHL
150
f
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 31

d.

CUSTOMER

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

Customer Customer _Name


_ ID

Street
_Address

City

Phone
_number

Credit _Card
_Number

CN0001

VelasquezCarmen 283 King


Street

Seattle

587-99-6666

000-111-222-333

CN0002

Ngao LaDoris

5 Modrany

Bratislav
a

586-355-8882

000-111-222-444

CN0003

Nagayama Midori

68 Via
Centrale

Sao
Paolo

254-852-5764

000-111-222-555

CN0004

Quick-To-See
Mark

6921 King Way Lagos

63-559-777

000-111-222-666

CN0005

Ropeburn Audry

86 Chu Street

Hong
Kong

41-559-87

CN0006

Urguhart Molly

3035 Laurier
Blvd.

Quebec

418-542-9988

s
) ha

000-111-222-777

co 000-111-222-888

u
ed ide

a
u 000-111-222-999
en322-504-2228
G
CN0007
Menchu Roberta
Brussels
s
Boulevard de
t
i
m den
Waterloo 41
@
tu 614-455-9863 000-111-222-222
ila Columbu
S
h
CN0008
Biri Ben
398 High c
St.
ar e ths is
g
(
uSt.s Brisbane 616-399-1411 000-111-222-111
CN0009
Catchpole
ron88 Alfred
o
d
t
l
Antoinetteua
se
n
G
e
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 32

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

e.

CREDIT_CARD_DETAILS
Credit _Card_ Number

Credit _Card _Type

Expiry _Date

000-111-222-333

VISA

17-JUN-2009

000-111-222-444

MasterCard

24-SEP-2005

000-111-222-555

AMEX

11-JUL-2006

000-111-222-666

VISA

22-OCT-2008

000-111-222-777

AMEX

26-AUG-2000

000-111-222-888

MasterCard

15-MAR-2008

000-111-222-999

VISA

4-AUG-2009

000-111-222-111

Maestro

27-SEP-2001

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
illf. BOOKS
tra
u
n
G no
000-111-222-222

AMEX

9-AUG-2004

Book _ID

Book _Name

Author _ID

Price

Publisher _ID

BN0001

Florentine Tragedy

AN0002

150

PN0002

BN0002

A Vision

AN0002

100

PN0003

BN0003

Citizen of the World

AN0001

100

PN0001

BN0004

The Complete Poetical

AN0001

300

PN0001

Works of Oliver
Goldsmith
BN0005

Androcles and the Lion

AN0003

90

PN0004

BN0006

An Unsocial Socialist

AN0003

80

PN0004

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 33

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

BN0007

A Thing of Beauty is a
Joy Forever

AN0007

100

PN0002

BN0008

Beyond the Pale

AN0008

75

PN0005

BN0009

The Clicking of
Cuthbert

AN0009

175

PN0005

BN00010

Bride of Frankenstein

AN0006

200

PN0001

BN00011

Shelley's Poetry and


Prose

AN0005

150

PN0003

s
) ha

co

PN0002
u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no
BN00012

g.

War and Peace

AN0004

150

SHOPPING_CART
Shopping _Cart
_ID

Book _ID

Price

Shopping _Cart
_Date

Quantity

SC0001

BN0002

200

12-JUN-2001

10

SC0002

BN0003

90

31-JUL-2004

SC0003

BN0003

175

28-JUN-2005

SC0004

BN0001

80

14-AUG-2006

SC0005

BN0001

175

21-SEP-2006

SC0006

BN0004

100

11-AUG-2007

SC0007

BN0005

200

28-OCT-2007

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 34

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

SC0008

BN0006

100

25-NOV-2009

SC0009

BN0006

150

18-SPET-2009

s
) ha

co

u
h. ORDER _DETAILS
ed ide

a
u _Cart _ID
en t Shopping
G
Order _ID
Date _of _Purchase
Customer
Shipping_
s
i
m den
_ID
Type
@
ila s Stu
OD0001
CN0001
USPS
12-JUN-2001
SC0002
h
c
i
r
h
a
t
OD0002
CN0002
USPS(g
28-JUN-2005
SC0005
e
s
n
u
o
OD0003
CN0003 ldrFedEx to
31-JUL-2004
SC0007
a nse
u
OD0004
CN0004
14-AUG-2006
SC0004
G iceFedEx
a
l
l
i
OD0005 ch CN0005
21-SEP-2006
SC0003
le FedEx
b
r
a
A
r
eCN0006
o
OD0006
DHL
28-OCT-2007
SC0001
f
s
m
r
n
e
DHL
11-AUG-2007
SC0006
ill OD0007
tra CN0007
u
n
G no
OD0008

CN0008

DHL

18-SEP-2009

SC0008

OD0009

CN0009

USPS

25-NOV-2009

SC0009

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 35

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

i.

PURCHASE_HISTORY
Customer _ID

Order _ID

CN0001

OD0001

CN0003

OD0002

CN0004

OD0005

CN0009

OD0007

s
) ha

co

u
ed ide

a
en t Gu
s
i
CREATE VIEW customer_details AS
m den
@
SELECT
c.customer_name, c.street_address,
ila s Stu o.order_id,
h
o.customer_id, o.shipping_type,
o.date_of_purchase,
c
ar e thi
o.shopping_cart_id
g
(
n order_details
us
FROM
customer cro
JOIN
o
o
d
t
l
ON
c.customer_id
ua ense= o.customer_id;
G
la le lic
i
h
SELECTrc *
A customer_details
rab
e
FROM
o
f
scustomer_id;
rm
n
e
ORDER
BY
a
l
r
l
i
t
Gu non-

6. Create a view named CUSTOMER_DETAILS to show the Customer Name, Customer


Address, and the details of the order placed by the customer. Order the results by Customer
ID.

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 36

7. Make changes to the data in the tables.

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

a.

Add a new book detail. Verify if the author detail for the book is available in the AUTHOR
table. If not, make an entry in the AUTHOR table.
INSERT INTO books(book_id, book_name, author_id, price,
publisher_id)
VALUES ('BN0013','Two States','AN0009','150','PN0005');

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
b. Enter a shopping cart detailnfor the book
s details that you just entered in 7(a).
u
o
r
o
ald nse t
u
G ice
INSERT INTOashopping_cart(shopping_cart_id,
book_id, price,
l
l
i
Shopping_cart_date,quantity)
h
e
l
rc('SC0010','BN0013','200',TO_DATE('12-JUN-2006','DD-MONA
rab
VALUES
e
o
f
s
YYYY'),'12');
rm
n
e
a
l
r
l
i
t
Gu non-

8. Create a report that contains each customers history of purchasing books. Be sure to
include the customer name, customer ID, book ID, date of purchase, and shopping cart ID.
Save the commands that generate the report in a script file named lab_apcs_8.sql.
Note: Your results may be different.

SELECT c.customer_name CUSTOMER, c.customer_id,


s.shopping_cart_id, s.book_id,o.date_of_purchase
FROM customer c
JOIN order_details o
ON
o.customer_id=c.customer_id
JOIN shopping_cart s
ON o.shopping_cart_id=s.shopping_cart_id;

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 37

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

s
) ha

co

u
ed ide

a
en t Gu
s
i
m den
@
ila s Stu
h
c
ar e thi
g
(
ron to us
d
l
ua ense
G
la le lic
i
h
rc rab
A
o
fe
s
m
r
n
e
ill n-tra
u
G no

Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Additional Practices and Solutions


Chapter 12 - Page 38

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