Professional Documents
Culture Documents
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans Oracle Database 12c R2: SQL
G n-t
no Workshop I
Activity Guide
D80190GC20
Edition 2.0 | November 2016 | D98631
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
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.
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:
s a
Trademark Notice
) h a
c o m
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
e ฺ
owners.
e ฺ i d
Author r a cl t Gu
@ o en
Apoorva Srinivas
s a d
S t ud
Technical Contributors and Reviewers ฺ p ra this
Nancy Greenberg, Suresh Rajan, Peckkwai l u
r Bryan
uYap, s eRoberts, Sharath Bhujani
a u
( g idd se to
d oracletutor
This book was publishedausing:
s c e n
ra ble l i
u P
a l ur sfera
d n
Gid n-tra
no
Table of Contents
Course Practice Environment: Security Credentials ............................................................................... I-1
Course Practice Environment: Security Credentials.................................................................................. I-2
Practices for Lesson 1: Introduction ........................................................................................................ 1-1
Practices for Lesson 1: Overview............................................................................................................. 1-2
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
r a cl t Gu 4-3
Practice 4-1: Using Single-Row Functions to Customize Output................................................................
@ o en
Solution 4-1: Using Single-Row Functions to Customize Output................................................................ 4-8
d
a Expressions d
tu ............................. 5-1
Practices for Lesson 5: Using Conversion Functions and Conditional
r a s S
ฺpExpressions is
h....................................................
Practices for Lesson 5: Overview.............................................................................................................
u t 5-2
Practice 5-1: Using Conversion Functions and Conditional
l r
u Expressions s e 5-3
Solution 5-1: Using Conversion Functions and
d d a
Conditional
t o u .................................................... 5-8
i
(g nData seUsing the Group Functions................................... 6-1
a d
Practices for Lesson 6: Reporting Aggregated
e
r a
Practices for Lesson 6: Overview
e icby Using Group Functions......................................................... 6-2
s .............................................................................................................
l
u P ab
Practice 6-1: Reporting Aggregatedl Data 6-3
Solution 6-1:u
l r
Reporting
f e r
Aggregated Data by Using Group Functions......................................................... 6-6
d a
dfor Lesson s
n7: Displaying Data from Multiple Tables Using Joins ............................................ 7-1
G i
Practices
t r a
for-Lesson 7: Overview............................................................................................................. 7-2
Practices n
no7-1: Displaying Data from Multiple Tables by Using Joins............................................................ 7-3
Practice
Solution 7-1: Displaying Data from Multiple Tables by Using Joins............................................................ 7-8
Practices 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: Managing Tables Using DML Statements ........................................................ 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
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
Course Practice
@ o en
a
Environment:d t d
uSecurity
s
ra this S
ฺ p
Credentials
u
a l ur use
( g idd se Chapter
to I
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
For connection-specific credentials used in this course, see the following table:
Connection-Specific Credentials
Connection_Name Username Password
s a
myconnection ora1
) h a ora1
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
Practices for o en 1:
Lesson
Introduction
a d @ t ud
s
ra this S
r u ฺ p
Chapter 1
l u s e
i d da to u
d (g nse
r a s a l i ce
r u P able
d a lu sfer
n
Gid n-tra
no
Practice Overview
In this practice, you start Oracle SQL Developer, create a new database connection, and
browse your HR tables. You also set some SQL Developer preferences.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
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.
Note
• 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. s a
• ) h a
For any query, the sequence of rows retrieved from the database may differ from the
screenshots shown.
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Overview
This is the first of many practices in this course. The solutions (if you require them) can be found
at the end of the practices for each lesson. The practices are intended to cover most of the
topics that are presented in the corresponding lesson.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks s a
1. Start Oracle SQL Developer by Using the SQL Developer Desktop Icon )h a
c m
o deฺ
2. Create a New Oracle SQL Developer Database Connection
e ฺ i
l right-click
u
a
a. To create a new database connection, in the Connections Navigator,c G
Connections and select New Connection from the context
@ ormenu.eThe
n t New / Select
Database Connection dialog box appears.
s a d
S t ud
a
rfollowing
b. Create a database connection by using the
r u ฺ p
e t hisinformation:
Connection Name: myconnection
d a lu us
Username: ora1
( g id se to
s a d password
Password: Enter the
c e n from the Course Practice Environment:
ra ble
Security Credentials l i document
u P
r fera
Hostname: localhost
a l u1521
i d d rans
Port:
G Service
o n -t Name: PDBORCL
nEnsure that you select the Save Password check box.
3. Test the Oracle SQL Developer Database Connection and Connect to the Database
a. Test the new connection.
b. If the status is Success, connect to the database by using this new connection.
4. Browse 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
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da a New
2. Create r a n s SQL Developer Database Connection
Oracle
-t a new database connection, in the Connections Navigator, right-click
Ga. Toncreate
o
n Connections and select New Connection from the context menu.
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
b. Create a database connection by using the following information:
i. Connection Name: myconnection d@
o en
s a S t ud
ii. Username: ora1
ฺ p ra this
iii. Password: Enter
l u r u the
s e
password from the Course
d d a to u
Practice Environment: Security Credentials document
iv.
d (gi nlocalhost
Hostname: se
v.Port:
a a
s 1521 lic e
r
Pvi. abService le Name: PDBORCL
r u
lu Ensure er you select the Save Password check box.
d a n s f that
Gid n-tra
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
b. If the status is Success, connect to the database by @
o new
using thisd e nconnection.
d
sa s Stu
r a
ฺp e thi
r
lu us u
d a
( g id se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
4. Browse the Tables in the Connections Navigator
r objects
uthe s ethat are available to you in the Tables
a. In the Connections Navigator, viewa u
( g iddtables
node. Verify that the following
e topresent:
are
COUNTRIES d
s a c e ns
ra ble li
DEPARTMENTS
P
l u u era
EMPLOYEES
rJOB_GRADES
d a JOB_HISTORY
n s f
d
Gi n-trJOBS a
no LOCATIONS
REGIONS
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
Practices for o en 2:
Lesson
Retrievinga @
d Datat udUsing the
s
raSELECT S
r SQL
u ฺ p
e t his Statement
d a lu Chapter
us 2
d t o
d (gi nse
r a sa lice
r u P able
d a lu sfer
n
Gid n-tra
no
Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 1
Practices for Lesson 2: Overview
Practice Overview
This practice covers the following topics:
• Selecting all data from different tables
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
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
In this practice, you write simple SELECT queries. The queries cover most of the SELECT
clauses and operations that you learned in this lesson.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Task 1
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:
s a
SELECT *
)h a
FROM job_grades;
c m
o deฺ
l e ฺ u i
a c G
True/False
@ oyour identify
e n t
3. There are four coding errors in the following statement.dCan
a t u d them?
SELECT employee_id, last_name s
ra this S
ฺ
ru sep
sal x 12 ANNUAL SALARY
employees; da
l u u
FROM
i d t o
d (g nse
Task 2
r a s a l i ce
u P before
Note the following points
a b leyou begin with the practices:
• Save r
lu all your f er files at the following location:
practice
d a s
G•id Enter r an
/home/oracle/labs/sql1/labs
- t
n your SQL statements in a SQL Worksheet. To open a new worksheet, click File
nomenu, select New. A New Gallery dialog window appears. Click Database Files under
Database Tier on the left pane. Select SQL File on the right pane and click OK.
• 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 Run 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 data from the Human Resources tables.
Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 3
4. Your first task is to determine the structure of the DEPARTMENTS table and its contents.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
5. Your next task is to determine the structure of the EMPLOYEES table and its contents.
a. Determine the structure of the EMPLOYEES table.
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 4
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
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 5
6. The HR department wants a query to display all unique job IDs from the EMPLOYEES table.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
)h a
Task 3
c m
o deฺ
If you have time, complete the following exercises: l e ฺ u i
7. The HR department wants more descriptive column headings a c
r its report G
t on employees.
@ ofor e n
Copy the statement from lab_02_5b.sql to a new SQL
s a dThen runtuthed query again.
Worksheet. Name the columns
ฺ p ra this S
Emp #, Employee, Job, and Hire Date, respectively.
l u ru se
i d da to u
d (g nse
r a s a l i ce
r u P able
d a lu sfer
n
Gid n-tra
no
Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 6
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
)h a
c m
o deฺ
l e ฺ u i
If you want an extra challenge, complete the following exercise:r a c G
t to display all
@
9. To familiarize yourself with the data in the EMPLOYEES table,
ocreate ea n
query
s
the data from that table. Separate each column output a S t ud Name the column
dby a comma.
THE_OUTPUT. ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
…
no
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
Test your knowledge:
1. The following SELECT statement executes successfully:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
True/False
2. The following SELECT statement executes successfully:
SELECT *
FROM job_grades;
True/False
s a
3. There are four coding errors in the following statement. Can you identify them? )h a
c m
o deฺ
SELECT employee_id, last_name
l e ฺ u i
sal x 12 ANNUAL SALARY
a c G
FROM employees;
@ or ent
• The EMPLOYEES table does not contain a column a d The column is called
d calledtusal.
s
ra this S
SALARY.
u ฺ p
r x as shown
• The multiplication operator is *,lu
a not
u s e in line 2.
• The ANNUAL SALARY alias d include
idcannot to spaces. The alias should read
( g
ANNUAL_SALARY ordshould benenclosed s e within double quotation marks.
s a l i c e
a leafter the LAST_NAME column.
• A comma isrmissing
r u P ab
Task 2 a l u f e r
i d d a n s
G have nbeen
You r
-tbased
hired as a SQL programmer for Acme Corporation. Your first task is to create
n o
some reports on 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;
Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 8
5. Your next task is to determine the structure of the EMPLOYEES table and its contents.
a. Determine the structure of the EMPLOYEES table.
DESCRIBE employees
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
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
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. The HR department wants a query to display all unique job IDs from the EMPLOYEES table.
SELECT DISTINCT job_id
FROM employees;
s a
)h a
Task 3 c m
o deฺ
l e ฺ u i
If you have time, complete the following exercises:
a c
r its report G
t on employees.
7. The HR department wants more descriptive column headings
@ ofor e n
Copy the statement from lab_02_5b.sql to a newa
s SQL
S t ud Name the columns
d Worksheet.
Emp #, Employee, Job, and Hire Date, respectively.
ฺ p ra thThenis run the query again.
SELECT employee_id "Emp #", l u
ur last_name
s e "Employee",
d
job_id "Job",dhire_datea u
to "Hire Date"
( g i e
FROM employees;
s a d cens
8. The HR department
P r l e li a report of all employees and their job IDs. Display the
a has requested
l u ru feand
last name concatenated
r abTitle.
with the job ID (separated by a comma and space) and name the
column
d a Employee
dSELECT n s
i
G n-t r a last_name||', '||job_id "Employee and Title"
noFROM employees;
If you want an extra challenge, complete the following exercise:
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;
Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 9
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Practices for Lesson 2: Retrieving Data Using the SQL SELECT Statement
Chapter 2 - Page 10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
Practices for o en 3:
Lesson
Restrictinga @
d andt udSorting Data
s
ra this S
r u ฺ p
Chapter 3
l u s e
i d da to u
d (g nse
r a s a l i ce
r u P able
d a lu sfer
n
Gid n-tra
no
Practices Overview
This practice covers the following topics:
• Selecting data and changing the order of the rows that are displayed
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Overview
In this practice, you build reports by using statements with the WHERE clause and the ORDER BY
clause. You make the SQL statements more reusable and generic by including the ampersand
substitution.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
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 a
)h a
c m
o deฺ
l e ฺ u i
a c G
2. Open a new SQL Worksheet. Create a report that displays @ orlast name
the e n t and department
number for employee number 176. s a d
S t ud
ฺ p ra this
l u
ur use
a
3. The HR department needs (
idd se to and low-salaried employees. Modify
togfind high-salaried
lab_03_01.sql tos a d theclast
display e nname and salary for any employee whose salary is not
rathrough l i
u P
in the range $5,000
a b le$12,000. Save your SQL statement as lab_03_03.sql.
r
lu sfer
d a n
Gid n-tra
no
5. Display the last name and department ID of all employees in department 20 or department
50 in ascending alphabetical order by last_name.
s a
h a
) earn
6. Modify lab_03_03.sql to display the last name and salary of employees
c m
who
oLabeldthe
e ฺ
e ฺ i
between $5,000 and $12,000, and are in department 20 or department
columns Employee and Monthly Salary, respectively. Savera cl t Gu as
50.
lab_03_03.sql
lab_03_06.sql. Run the statement in lab_03_06.sql. @ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
7. The HR department needs ( ag idd that
report e to the last name and hire date of all
displays
s a d in 2010.
employees who were hired
c e ns
P ra ble li
l u ru fera
i d da rans
G n-t
no a report to display the last name and job title of all employees who do not have a
8. Create
manager.
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 column’s numeric position in the ORDER BY clause.
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 manager’s employees. The HR department wants the ability to sort the
report on a selected column. You can test the data with the following values:
s a
) h a
manager_id = 103, sorted by last_name: m
ฺ c o d e ฺ
e
cl t Gu i
r a
o en
a d @ t ud
s
ra this S
u ฺ p
manager_id = 201, sorted by salary:
a l ur use
( g idd se to
s a d cen
ra blby e li
manager_id = P
124, sorted employee_id:
l u ru fera
i d da rans
G n-t
no
13. Display the last names of all employees who have both an “a” and an “e” in their last name.
15. Modify lab_03_06.sql to display the last name, salary, and commission for all
employees whose commission is 20%. Save lab_03_06.sql as lab_03_15.sql. Rerun
the statement in lab_03_15.sql.
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
8. Create a report to display the last name and job title of all employees who do not have a
manager.
SELECT last_name, job_id
FROM employees
WHERE manager_id IS NULL;
9. 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
column’s numeric position in the ORDER BY clause.
SELECT last_name, salary, commission_pct
s a
FROM employees
)h a
WHERE commission_pct IS NOT NULL
c m
o deฺ
l e ฺ u i
ORDER BY 2 DESC, 3 DESC;
a c
r the queries G
t that you are
10. Members of the HR department want to have more flexibilityo with n
@
d andtusalary
writing. They would like a report that displays the last name e
d of employees who
s a S
earn more than an amount that the user specifies
ฺ p
created in Task 1 and modify it.) Save thisuquery
rato a file
after
t h
a prompt.
i s
named
(You can use the query
l u r s e lab_03_10.sql.
SELECT last_name, salary
employees(gi
dda e to u
ns
FROM
s a d> &sal_amt;
c e
WHERE salary
P rapromptedl e li
Enter 12000 when
r u r a b for a value in a dialog box. Click OK.
u
al nsfe
d d
Gi n-tra
no
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 manager’s employees. The HR department wants the ability to sort the
report on a selected column. You can test the data with the following values:
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;
ฺ p a lab_03_06.sql
employees whose commission amount is 20%.rSave is as
u t h
a l ur use
lab_03_15.sql. Rerun the statement in lab_03_15.sql.
SELECT
idd se to salary "Monthly Salary",
last_name "Employee",
( g
s a d cen
commission_pct
ra ble li = .20;
FROM employees
WHERE Pcommission_pct
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
Practices for
@ orLessone n t 4: Using
Single-Row
s a S t ud
d Functions to
r a
ฺp e this Output
Customize
r u
lu Chapter
d d a t o us 4
d (gi nse
r a sa lice
r u P able
d a lu sfer
n
Gid n-tra
no
Practice Overview
This practice covers the following topics:
• Writing a query that displays the system date
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
• Creating queries that require the use of numeric, character, and date functions
• Performing calculations of years and months of service for an employee
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Overview
In this practice, you use 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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
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. The HR department needs a report to display the employee number, last name, salary, and
s
salary increased by 15.5% (expressed as a whole number) for each employee. Label the a
) h a
column New Salary. Save your SQL statement in a file named lab_04_02.sql.
3. Run your query in the lab_04_02.sql file. c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra tasks:
5. Perform the following l e li
aru r b the last name (with the first letter in uppercase and all the
adisplays
a. Writel u e
query that
a letters f
sin lowercase) and the length of the last name for all employees whose
i d dother r a n
G name
o n -tstarts with the letters “A,” or “M.” Give each column an appropriate label. Sort the
n results by the employees’ last names.
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.”
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
9. Create a query to display
s a e n and the number of weeks employed for all
dthe lastcname
r a le
employees in department 90. l i
Label the number of weeks column as TENURE. Truncate the
P
rutenure.value
number of weeks atob0 decimal places. Show the records in descending order of the
l
employee’su f e r
i d daTherTENURE
Note: a n s value will differ because it depends on the date on which you run the
Gquery.n-t
no
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
FROM employees
WHERE department_id = 90
ORDER BY TENURE DESC;
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
Practices for
@ orLessone n t 5: Using
Conversion
s a S t ud
d Functions and
r a
ฺp e this Expressions
Conditional
r u
lu Chapter
d d a t o us 5
d (gi nse
r a sa lice
r u P able
d a lu sfer
n
Gid n-tra
no
Practice Overview
This practice covers the following topics:
• Creating queries that use the TO_CHAR and TO_DATE functions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
• Creating queries that use conditional expressions such as CASE , searched CASE, and
DECODE
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Overview
In this practice, you use the TO_CHAR and TO_DATE functions, and conditional expressions
such as CASE, searched CASE, and DECODE.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
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 a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
urlast names
s eand commission amounts. If an
a
3. Create a query that displays employees’ u
( g idd sshow
employee does not earn commission,
e to“No Commission.” Label the column COMM.
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
IT_PROG C
SA_REP D
ST_CLERK E
None of the above 0
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
u P column,
value of the JOB_ID
a b le using the following data:
Job lu r f er Grade
d a s
id -tran
AD_PRES
GST_MAN
A
non
IT_PROG
B
C
SA_REP D
ST_CLERK E
None of the above 0
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
Practices for o en 6:
Lesson
Reporting a @ t ud
d Aggregated Data
r a s S
r u ฺp ethe
Using t hisGroup Functions
d a lu Chapter
us 6
d t o
d (gi nse
r a sa lice
r u P able
d a lu sfer
n
Gid n-tra
no
Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 1
Practices for Lesson 6: Overview
Practice Overview
This practice covers the following topics:
• Writing queries that use group functions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
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
In this practice, you use group functions and select groups of data.
Tasks
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, 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
The HR department needs the following reports:
s
4. Find the highest, lowest, sum, and average salary of all employees. Label the columns a
)h a
Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest
whole number. Save your SQL statement as lab_06_04.sql. Run the query.
c m
o deฺ
l e ฺ u i
a c G
@ or ent
5. Modify the query in lab_06_04.sql to display a s
the minimum, tud sum, and average
ad Smaximum,
salary for each job type. Save lab_06_04.sqlฺ p r as lab_06_05.sql.
t his Run the statement in
u r u e
lab_06_05.sql.
d d al to us
d (gi nse
r a sa lice
r u P able
d a lu sfer
n
Gid n-tra
no
Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 3
6. Write a query to display the number of people with the same job.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
Generalize the query so that a user in the HR department is prompted for a job a
) h title.when
Save the script to a file named lab_06_06.sql. Run the query. Enter
c m
o deฺ
IT_PROG
prompted. l e ฺ u i
a c G
@ or ent
s a d
S t ud
7. Determine the number of managers without listing
ฺ p ra them.
t h s the column Number of
iLabel
Managers.
l u
ur use
a
( g i d setotdetermine
Hint: Use the MANAGER_IDdcolumn o the number of managers.
s a d cen
P ra ble li
8. Find thelu ru febetween
difference ra the highest and lowest salaries. Label the column
d a n s
Gid n-tra
DIFFERENCE.
no
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.
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:
10. Create a query to display the total number of employees and, of that total, the number of
employees hired in 2009, 2010, 2011, and 2012. Create appropriate column headings.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
11. Create a matrix query to display the job, the salary for that job based on the department
numbers 20, 50, 80, and 90, and the total salary for that job. Ensure to give each column an
appropriate heading.
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
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
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
True/False
3. The WHERE clause restricts rows before inclusion in a group calculation.
True/False
The HR department needs the following reports:
4. Find the highest, lowest, sum, and average salary of all employees. Label the columns
Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest
whole number. Save your SQL statement as lab_06_04.sql. Run the query.
SELECT ROUND(MAX(salary),0) "Maximum",
ROUND(MIN(salary),0) "Minimum",
ROUND(SUM(salary),0) "Sum", s a
ROUND(AVG(salary),0) "Average" )h a
FROM employees; c m
o deฺ
e ฺ
l sum, u i
a c
5. Modify the query in lab_06_04.sql to display the minimum, maximum, G and average
or enRun
salary for each job type. Save lab_06_04.sql as lab_06_05.sql.
@
t the statement in
lab_06_05.sql.
s a d
S t ud
SELECT job_id, ROUND(MAX(salary),0) ฺ p ra "Maximum",
t his
r u
lu us "Sum",
ROUND(MIN(salary),0) e "Minimum",
d a
( id se to
ROUND(SUM(salary),0)
g
ROUND(AVG(salary),0) "Average"
a d e n
ras ble lic
FROM employees
GROUP BYPjob_id;
u ru to display
6. Write alquery
f e ra the number of people with the same job.
a s
d ranjob_id, COUNT(*)
Gid SELECT
n -t employees
n o
FROM
GROUP BY job_id;
Generalize the query so that a 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.
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;
Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 6
8. Find the difference between the highest and lowest salaries. Label the column
DIFFERENCE.
SELECT MAX(salary) - MIN(salary) DIFFERENCE
FROM employees;
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.
SELECT manager_id, MIN(salary)
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING MIN(salary) > 6000
ORDER BY MIN(salary) DESC;
s a
) h a
If you want an extra challenge, complete the following exercises:
c m
o deฺ
e ฺ
lthat total, u i number of
10. Create a query that displays the total number of employees and, of
a c G the
employees hired in 2009, 2010, 2011, and 2012. Create appropriate
@ or ecolumn n t headings.
SELECT COUNT(*) total,
s a d
S t ud
SUM(DECODE(TO_CHAR(hire_date,
ฺ p ra th'YYYY'),2009,1,0))"2009",
is
r u e
lu us 'YYYY'),2011,1,0))"2011",
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),2010,1,0))"2010",
d a
d e to
( g i
SUM(DECODE(TO_CHAR(hire_date,
s a d cens
SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),2012,1,0))"2012"
FROM
P ra ble li
employees;
l u ru query
f e rato display the job, the salary for that job based on the department
a
11. Create a matrixs
idd -t20,
Gnumbers r n 80, and 90, and the total salary for that job. Ensure to give each column an
a50,
non
appropriate heading.
SELECT job_id "Job",
SUM(DECODE(department_id , 20, salary)) "Dept 20",
SUM(DECODE(department_id , 50, salary)) "Dept 50",
SUM(DECODE(department_id , 80, salary)) "Dept 80",
SUM(DECODE(department_id , 90, salary)) "Dept 90",
SUM(salary) "Total"
FROM employees
GROUP BY job_id;
Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 7
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Practices for Lesson 6: Reporting Aggregated Data Using the Group Functions
Chapter 6 - Page 8
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
Practices for
@ orLessone n t 7:
Displaying
s a d Data
S t udfrom Multiple
p
Tables
ฺ ra tUsing
his Joins
r u
lu Chapter e
d d a t o us 7
d (gi nse
r a sa lice
r u P able
d a lu sfer
n
Gid n-tra
no
Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 1
Practices for Lesson 7: Overview
Practice Overview
This practice covers the following topics:
• Joining tables using an equijoin
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
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
In this practice, you extract data from multiple tables using SQL:1999–compliant joins.
Tasks
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, 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.
2. The HR department needs a report of all employees with corresponding departments. Write
s a
h a
a query to display the last name, department number, and department name for these
)
employees.
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
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.
Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 3
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 4
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
6. Create a report for the HR department that displays a
s d
employee
S udnames,
last
t department
numbers, and all the employees who work in the
ฺ p rascriptthtoisa file named lab_07_06.sql.
same department as a given employee.
l u ru se
Give each column an appropriate label. Save the
i d da to u
d (g nse
r a s a l i ce
r u P able
d a lu sfer
id -tran
G…
non
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.
Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 5
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
If you want an extra challenge, complete
l u
urthe following
s e exercises:
a u
( g
Davies. Create a query to display iddthe sname
8. The HR department wants to determine
e
theonames of all employees who were hired after
t and hire date of any employee hired after
employee Davies. a d e n
r a s
e l ic
u P ab l
l u r f er
d a n s
Gid n-tra
no
Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 6
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
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
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,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
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 these
employees.
SELECT last_name, department_id, department_name
FROM employees
JOIN departments
s a
USING (department_id);
) h a
3. The HR department needs a report of employees in Toronto. Display the o
c m ฺ
last name, job,
e
e ฺ i d
department number, and department name for all employees who work
r a cl d.department_name
in
t
Toronto.
G u
SELECT e.last_name, e.job_id, e.department_id, o en
employees e JOIN departmentsadd@ ud
FROM
s S t
ON (e.department_id = d.department_id)
ฺ p ra this
JOIN locations l
l u
ur use
USING (location_id) d a to
( g id= 'toronto';
e
WHERE LOWER(l.city)
s a d cens
4. Create a report tora
P l e li
display employees’ last names and employee numbers along with their
managers’ last
r u names
r a b
and manager numbers. Label the columns Employee, Emp#,
u
al andnMgr#,
Manager,
s fe respectively. Save your SQL statement as lab_07_04.sql. Run the
d d
Gi SELECT
query.
n - tra w.last_name "Employee", w.employee_id "EMP#",
no 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;
Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 8
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
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 9
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Practices for Lesson 7: Displaying Data from Multiple Tables Using Joins
Chapter 7 - Page 10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
Practices for
@ orLesson
e n t 8: Using
Subqueriesa d
d totuSolve Queries
r a s S
r u ฺp e8 this
Chapter
d a lu us
( g id se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Practice Overview
This practice covers the following topics:
• Creating subqueries to query values based on unknown criteria
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
• Using subqueries to find values that exist in one set of data and not in another
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Overview
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. The HR department needs a query that prompts the user for an employee’s 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 a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
dd e to
( g i
2. Create a report that displays
who earn more thans a daverage c e ns Sortnumber,
the employee last name, and salary of all employees
r a the
e l i salary. the results in ascending order by salary.
u P ab l
l u r f er
d a n s
Gid n-tra
no
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.
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
Modify the query so that the user
( g iddis prompted
e to for a location ID. Save this to a file named
lab_08_04.sql.
s d displays
athat c e nthes last name and salary of every employee who
5. Create a report foraHR
reports to King.Pr l e li
l u ru ferab
i d da rans
G n-t
no
6. Create a report for HR that displays the department number, last name, and job ID for every
employee in the Executive department.
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
If you have time, complete the following exercise:
8. Modify the query in lab_08_03.sql to displayra
a d
semployee
the S tud last name, and
number,
p average
ฺthe t s and who work in a
hisalary,
salary of all employees who earn more thanr u e
lulast nameuscontains the letter “u.” Save
department with any employee whose
d a to Run the statement in lab_08_08.sql.
lab_08_03.sql as lab_08_08.sql
( g id seagain.
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
UNDEFINE name
d d a to u
who earn more than the average salary. in ascending order by salary.
SELECT employee_id,
d (gi last_name,
n s e salary
FROM sa lice
employees
r a
WHERE salary
u e
P a>bl(SELECT AVG(salary)
r
lu sfer FROM employees)
d a
G id ORDER- t r aBYn salary;
ona query that displays the employee number and last name of all employees who work
3. Write
n
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%');
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);
P ra ble li 'Executive');
l u ru from
7. Create a report
f e r a a list60.of all employees whose salary is more than the salary of
that displays
d a
any employee
dSELECT n s department
i
G n-t r a last_name FROM employees
noWHERE salary > ANY (SELECT salary
FROM employees
WHERE department_id=60);
FROM employees
WHERE department_id IN (SELECT department_id
FROM employees
WHERE last_name like '%u%')
AND salary > (SELECT AVG(salary)
FROM employees);
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
Practices for
@ orLessone n t 9: Using
the Set s a S t ud
dOperators
ฺ p ra this
l u r u
Chapter
s
9
e
i d da to u
d (g nse
r a s a l i ce
r u P able
d a lu sfer
n
Gid n-tra
no
Practice Overview
In this practice, you create reports by using the following set operators:
• UNION
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
• INTERSECT
• MINUS
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Overview
In this practice, you write queries by using the set operators UNION, INTERSECT, and MINUS.
Tasks
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, 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.
s a
)h a
c m
o indthem.
e ฺ
e ฺ i
2. The HR department needs a list of countries that have no departments
Display the country IDs and the names of the countries. Use the r a cl operators
set
located
t G uto create this
report. @ o en
s a d
S t ud
ฺ p ra this
r u
luwork se
d a
3. Produce a list of all the employeesdwho
t o inudepartments 50 and 80. Display the
i
(g nse
employee ID, job ID, and department ID by using the set operators.
s a d ce
r a l i
r u P able
d a lu sfer
n
Gid n-tra
no
4. Create a report that lists the details of all employees who are sales representatives and are
currently working in the sales department.
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
MINUS
SELECT department_id
FROM employees
WHERE job_id = 'ST_CLERK';
2. 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 s a
MINUS ) h a
c o m e ฺ
SELECT l.country_id,c.country_name
e ฺ i d
FROM locations l JOIN countries c
r a cl t Gu
ON (l.country_id = c.country_id)
@ o en
JOIN departments d
s a d
S t ud
ON d.location_id=l.location_id; ฺ p ra this
3. Produce a list of all the employees whol u
r in departments
uwork s e 50 and 80. Display the
d a o u
( g id se t
employee ID, job ID, and department ID by using the set operators.
SELECT employee_id,
s a d cjob_id,
e n department_id
FROM EMPLOYEESra ble l i
u P
a l ur ALLsfera
WHERE department_id=50
d ran
UNION
Gid SELECT
n -t employee_id, job_id, department_id
o
n 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;
SELECT last_name,department_id,TO_CHAR(null)dept_name
FROM employees
UNION
SELECT TO_CHAR(null),department_id,department_name
FROM departments;
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
Practices for o en 10:
Lesson
Managing a @ t ud Using DML
d Tables
s
ra this S
u ฺ p
Statements
a l ur use
( g idd se Chapter
to 10
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Lesson Overview
This practice covers the following topics:
• Inserting rows into tables
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Note
• For all DML statements, use the Run Script icon (or press F5) to execute the query.
You can 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 the cleanup_10.sql script from /home/oracle/labs/sql1/code_ex
/cleanup_scripts/ before performing the following tasks.
s a
)h a
Tasks
c m
o deฺ
1. Run the lab_10_01.sql script from /home/oracle/labs/sql1/labs/ l e ฺ utoi create the
a c G
@ or table
MY_EMPLOYEE table. Describe the structure of the MY_EMPLOYEE
e n t identify the
to
column names.
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
4 Newman Chad cnewman
)h a 750
c m
o deฺ
5 Ropeburn Audrey aropebur l e ฺ 1550
u i
a c G
@ or ent
3. Populate the MY_EMPLOYEE table with the seconds a
row S t ud data from the
dof the sample
ฺ p ra in the
preceding list. This time, list the columns explicitly t is clause.
hINSERT
4. Confirm your addition to the table. alu
r u e
d d t o us
d (gi nse
r a sa lice
r u P able
5. Writea
d an er
luINSERTsfstatement in a dynamic reusable script file to load the next two rows of
i d n
trainto
Gsamplen
data
- the MY_EMPLOYEE table. The script should prompt for all the columns (ID,
no
LAST_NAME,
file.
FIRST_NAME, USERID, and SALARY). Save this script to a lab_10_06.sql
6. 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.
7. Confirm your additions to the table.
25. Confirm that the new row was added with the correct USERID.
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
DESCRIBE my_employee
2. 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.
UNDEFINE first_name
UNDEFINE last_name
24. Run the lab_10_24.sql script to insert the following record:
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
Practices for o en 11:
Lesson
Introduction
a d @ to
t udData
s S
ra thisLanguage
u ฺ p
Definition
a l ur use
( g idd se Chapter
to 11
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Lesson Overview
This practice covers the following topics:
• Creating new tables
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
Note: Before starting this practice, execute the
) h a
/home/oracle/labs/sql1/code_ex/cleanup_scripts/cleanup_11.sql script.
m
ฺ c o d e ฺ
e
cl t Gu i
r a
o en
a d @ t ud
s
ra this S
u ฺ p
a l ur use
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Notes
• For all 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 a
)h a
Tasks
c m
o statement
e ฺ in
e ฺ i d
the lab_11_01.sql script, and then execute the statement inra
cl
1. Create the DEPT table based on the following table instance chart. Save the
the scriptttoG
u
create the
@ o en
table. Confirm that the table is created.
s a d
S t ud
Column Name ID
ฺ p ra this NAME
Key Type r
Primarylukey u s e
a u
Nulls/Unique
( g idd se to
FK Table s a d cen
FK Column P
ra ble li
l u r u era
d d a type nsf
Data NUMBER VARCHAR2
Gi Length
n - tra 7 25
n o
Nulls/Unique
FK Table DEPT
FK Column ID
Data type NUMBER VARCHAR2 VARCHAR2 NUMBER
Length 7 25 25 7
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or dataetype,
n t
3. Modify the EMP table. Add a COMMISSION column of the
a dNUMBER
t u d with precision 2
and scale 2. Confirm your modification. s
ra this S
ฺ p
ru se
l u
i d da to u
d (g nse
r a s a l i ce
r u P able
d a lu sfer
n
Gid n-tra
no
4. Modify the EMP table to allow for longer employee last names. Confirm your modification.
6. In the EMP table, mark the DEPT_ID column as UNUSED. Confirm your modification by
checking the description of the table.
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
7. Drop all of the UNUSED columns from the EMPฺp ra this
table.
8. Create the EMPLOYEES2 table basedaon l u
r structure
uthe s e of the EMPLOYEES table. Include only
u
the EMPLOYEE_ID, FIRST_NAME,
( g iddLAST_NAME,
e to SALARY, and DEPARTMENT_ID columns.
s FIRST_NAME, LAST_NAME, SALARY, and
Name the columns in your
s a dnew table
c e nID,
P ra Confirm
DEPT_ID, respectively.
l e li that the table is created.
l u ru ferab
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
)h a
CREATE TABLE dept
c m
o deฺ
l e ฺ u i
(id NUMBER(7)CONSTRAINT department_id_pk PRIMARY
a c KEY,
G
name VARCHAR2(25));
@ or ent
a d issue
To confirm that the table was created and to view its structure,
s S t udthe following command:
DESCRIBE dept; ฺ p ra this
l u
ur table s e
2. Create the EMP table based on the
d d a
following
t o u instance chart. Save the statement in a
script called lab_11_02.sql, i
(g andnthenseexecute the statement in the script to create the
d
table. Confirm that theatable e
is created.
r a s
e l ic
u P ab l
u r
l Name
Column f erID
d a s LAST_NAME FIRST_NAME DEPT_ID
Gid Key - t r
Typean
n
noNulls/Unique
FK Table DEPT
FK Column ID
Data type NUMBER VARCHAR2 VARCHAR2 NUMBER
Length 7 25 25 7
DESCRIBE emp
4. Modify the EMP table to allow for longer employee last names. Confirm your modification.
ALTER TABLE emp
MODIFY last_name VARCHAR2(50);
DESCRIBE emp
s a
5. Drop the FIRST_NAME column from the EMP table. Confirm your modification by checking
) h a
the description of the table. m
ฺ c o d e ฺ
ALTER TABLE emp e
cl t Gu i
DROP COLUMN first_name; r a
o en
DESCRIBE emp
a d @ t ud
6. In the EMP table, mark the DEPT_ID column as r s
a Confirm S
u ฺ p UNUSED.
t h is your modification by
checking the description of the table.
a l ur use
ALTER TABLE emp ( g idd se to
s a d(dept_id);c e n
SET
ra ble
UNUSED l i
u P
r feremp a
a l uDESCRIBE
s
iddall of-trthe
7.GDrop anUNUSED columns from the EMP table.
non ALTER TABLE emp
DROP UNUSED COLUMNS;
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;
again.
Now, because the table is assigned a READ WRITE status, you will be allowed to truncate
rows in the table.
ALTER TABLE employees2 READ WRITE;
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
Additional Practices
o en and
Solutions a d @ t ud
s
ra this S
r u ฺ p
Chapter 12
l u s e
i d da to u
d (g nse
r a s a l i ce
r u P able
d a lu sfer
n
Gid n-tra
no
Practices Overview
In these practices, you will be working on extra exercises that are based on the following topics:
• Basic SQL SELECT statement
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. The HR department needs to find data for all the clerks who were hired after 2010.
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. s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
3. For budgeting purposes, the HR department
l u
ur noneeds
s ea reportbut
on projected raises. The report
should display those employees who
d d ahave
t o u
commission, who have a 10% raise in
i
(g nse
salary (round off the salaries).
s a d ce
r a l i
r u P able
d a lu sfer
n
Gid n-tra
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
5. Show those employees who have a last
l u
urnameustarting
s e with the letters “K,” “L,” or “M.”
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
a be used
rcan e i
lfor
These exercises
u P a b l extra practice after you have discussed the following
r
u SQL
topics: lbasic
f er statements, basic SQL Developer commands, SQL functions,
SELECT
d a s
ran
joins, and group functions.
id
7.GCreatena-treport that displays the department name, location ID, last name, job title, and
no of those employees who work in a specific location. Prompt the user for a location.
salary
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.
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 a
)h a
c m
o deฺ
l e ฺ u i
a c G
11. Create a report that displays the jobs that are found in the@ or ent and Executive
Administration
departments. Also display the number of employeesa
s for
S t udShow the job with the
dthese jobs.
highest number of employees first.
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d daexercises
These
r a n scan be used for extra practice after you have discussed the following
-t SQL SELECT statements, basic SQL Developer commands, SQL functions,
Gtopics:nbasic
o
n
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).
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
14. Show all the employees
s a dwho have c e n
managers with a salary higher than $15,000. Show the
ra bname,
following data: employee l i
e manager name, manager salary, and salary grade of the
manager. u P l
a l ur sfera
d n
Gid n-tra
no
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
idd se number
16. Create a report to display the department
( g to and lowest salary of the department with
the highest average salary.
s a d cen
P ra ble li
l u ru fera
d da a report
17. Create
i r a n sthat displays departments where no sales representatives work. Include the
Gdepartment
o n -t number, department name, manager ID, and location in the output.
n
s a
a
) hnumber,
19. Create a report that displays the employee number, last name, salary, department
and the average salary in their department for all employees.
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Overview
Solutions to Additional Practice 1-1 are given as follows.
Tasks
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
1. The HR department needs to find data for all the clerks who were hired after 2010.
SELECT *
FROM employees
WHERE job_id = 'ST_CLERK'
AND hire_date > '31-DEC-2010';
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.
s a
SELECT last_name, job_id, salary, commission_pct
)h a
FROM employees
c m
o deฺ
WHERE commission_pct IS NOT NULL
l e ฺ u i
a c G
ORDER BY salary DESC;
@ or ent
3. For budgeting purposes, the HR department needs a report
should display those employees who do not get a s ad on
a commissionSbut
udwho have
tprojected raises. The report
a 10% raise in
salary (round off the salaries). ฺ p r this
l u ru se after a 10% raise is '
i d da to u"New salary"
SELECT 'The salary of '||last_name||'
(g nse
|| ROUND(salary*1.10)
d
FROM
s
employees
a a l i c e
r
P able IS NULL;
WHERE commission_pct
r u
4. Create laureport offe r
employees and the duration of their employment. Show the last names of
d a n s
id have
all
Gthey
employees,
t r a together with the number of years and the number of completed months that
- been employed. Order the report by the duration of their employment. The
non who has been employed the longest should appear at the top of the list.
employee
SELECT last_name,
TRUNC(MONTHS_BETWEEN(SYSDATE, hire_date) / 12) YEARS,
TRUNC(MOD(MONTHS_BETWEEN(SYSDATE, hire_date), 12))
MONTHS
FROM employees
ORDER BY years DESC, MONTHS desc;
5. Show those employees who have a last name that starts with the letters “K,” “L,” or “M.”
SELECT last_name
FROM employees
WHERE SUBSTR(last_name, 1,1) IN ('K', 'L', 'M');
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.
7. Create a report that displays the department name, location ID, last name, job title, and
salary of those employees who work in a specific location. Prompt the user for a location.
Enter 1800 for location_id when prompted.
SELECT d.department_name, d.location_id, e.last_name, e.job_id,
e.salary
s a
FROM employees e JOIN departments d
)h a
ON e.department_id = d.department_id
c m
o deฺ
AND d.location_id = &location_id;
l e ฺ u i
a c G
or with ethenletter
8. Find the number of employees who have a last name that ends
@
t “n.” Create
two possible solutions.
s a d
S t ud
SELECT COUNT(*)
ฺ p ra this
FROM employees u
lur use
WHERE last_name LIKE d a
'%n';
--or ( g id se to
s a d cen
ra ble li
SELECT COUNT(*)
FROM P
employees
ru SUBSTR(last_name,
l
WHERE
a u f e ra -1) = 'n';
d
id a-treport s
an that shows the name, location, and number of employees for each
9. Create
Gdepartment.r
n Make sure that the report also includes department_IDs without employees.
noSELECT 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;
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.
SELECT DISTINCT job_id
FROM employees
WHERE department_id IN (10, 20);
These exercises can be used for extra practice after you have discussed the following
topics: basic SQL SELECT statements, basic SQL Developer commands, SQL functions,
joins, group functions, and subqueries.
s
12. Show all employees who were hired in the first half of the month (before the 16th of thea
month, irrespective of the year). )h a
c m
o deฺ
SELECT last_name, hire_date
l e ฺ u i
FROM employees
a c G
WHERE TO_CHAR(hire_date, 'DD') < 16;
@ or ent
a d salary, and salary
d lasttuname,
s
13. Create a report that displays the following for all employees:
expressed in terms of thousands of dollars. ฺpra
S
r u e t his
SELECT last_name, salary,luTRUNC(salary, s
employees; id d a t o u -3)/1000 Thousands
FROM
( g
dhave managers s e
n with a salary higher than $15,000. Show the
14. Show all employees who
s a l i c e
ra bname,
following data: employee
l e manager name, manager salary, and salary grade of the
manager. ru P
a l f ra
u e.last_name,
e
i d SELECT
a n s
dj.grade_level m.last_name manager, m.salary,
G n-t r
noFROM 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;
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 s a
ORDER BY d.department_id, employees; )h a
c m
o deฺ
e ฺ
l of the G u i
r a c
16. Create a report to display the department number and lowest salary
t
department with
the highest average salary.
@ o en
SELECT department_id, MIN(salary) a
s d
S t ud
FROM employees
ฺ p ra this
GROUP BY department_id lur u s e
HAVING AVG(salary) =id(SELECTd a u
toMAX(AVG(salary))
( g
d ceFROM e
ns employees
s a
P ra ble li GROUP BY department_id);
l u ru that
17. Create a report
f e ra department
displays the departments where no sales representatives work. Include
d a
the department
d ran s
number, name, manager ID, and location in the output.
i
G SELECT -t *
o n
n FROM departments
WHERE department_id NOT IN(SELECT department_id
FROM employees
WHERE job_id = 'SA_REP'
AND department_id IS NOT NULL);
18. Create the following statistical reports for the HR department. Include the department
number, department name, and the number of employees working in each department that:
a. Employs fewer than three employees:
SELECT d.department_id, d.department_name, COUNT(*)
FROM departments d JOIN employees e
ON d.department_id = e.department_id
GROUP BY d.department_id, d.department_name
HAVING COUNT(*) < 3;
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
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
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 a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G youn-want
Note: If o
t to build the tables, you can execute the commands in the
n
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.
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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Table Details
a. Table Name: AUTHOR
Column Data type Key Table Dependent Type
Author_ID VARCHAR2 PK
Author_Name VARCHAR2
b. Table Name: BOOKS
Column Datatype Key
s Table Dependent On a
Book_ID VARCHAR2 PK
) h a
Book_Name VARCHAR2
c o m eฺ
AUTHORS leฺ i d
Author_ID VARCHAR2 FK
r a c t Gu
Price NUMBER
@ o en
Publisher_ID VARCHAR2 FK
s a d t ud
PUBLISHER
S
c. Table Name: CUSTOMER
ฺ p ra this
Column Name Data type l u
ur Key s e Table Dependent On
a
dd e toPK u
Customer_ID VARCHAR2
( g i
Customer_Name
s a d
VARCHAR2
c e ns
Street_Address
P ra VARCHAR2
l e li
City b
ru feraVARCHAR2
VARCHAR2
l u
i d da rans VARCHAR2
Phone_Number
G n-t
Credit_Card_Number FK Credit_Card_Details
d.noCREDIT_CARD_DETAILS
Column Name Data type Key Table Dependent On
Credit_Card_Number VARCHAR2 PK
Credit_Card_Type VARCHAR2
Expiry_Date DATE
e. Table Name: ORDER_DETAILS
Column Data type Key Table Dependent On
Order_ID NUMBER PK
Customer_ID VARCHAR2 FK CUSTOMER
Shipping_Type VARCHAR2 FK SHIPPING_TYPE
Date_of_Purchase DATE
Shopping_Cart_ID NUMBER FK SHOPPING_CART
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
5. Add data to the tables.
s a d acscript
Create e n for each set of data to be added.
ra btables: l i
Add data to thePfollowing
u a le
r
a.lu AUTHORfer
d d a n s
i a
b. rPUBLISHER
G nc. -tSHIPPING_TYPE
nod. CUSTOMER
e. CREDIT_CARD_DETAILS
f. BOOKS
g. SHOPPING_CART
h. ORDER_DETAILS
i. PURCHASE_HISTORY
Note: Save the scripts by using the task number. For example, to save the script created
for the AUTHOR table, you can save it as labs_apcs_5a_1.sql. Ensure that you save
the scripts in /home/oracle/labs/sql1/labs folder.
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u raushopping
f e racart detail for the book details that you just entered in 7(a).
d a
b. Enter
n s
Gid n-tra
no
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no
Overview
The solution to Practice 1-2 is given as follows.
Tasks
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
1. Table Details
a. AUTHOR
CREATE TABLE AUTHOR
(
Author_ID VARCHAR2 (10) NOT NULL ,
Author_Name VARCHAR2 (20)
)
;
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
COMMENT ON TABLE AUTHOR IS 'Author'
@ o en
;
s a d
S t ud
ฺ p ra this
l u
ur use
a
ALTER TABLE AUTHOR
ADD CONSTRAINT ( g idd se toPRIMARY KEY (Author_ID);
AUTHOR_PK
s a d cen
b. BOOKS Pra l e li
l u ru TABLE
CREATE
f e r abBOOKS
i d da (rans
G n-t Book_ID VARCHAR2 (10) NOT NULL ,
no Book_Name VARCHAR2 (50) ,
Author_ID VARCHAR2 (10) NOT NULL ,
Price NUMBER (10) ,
Publisher_ID VARCHAR2 (10) NOT NULL
)
;
l u u era
rCredit_Card_Type VARCHAR2 (15) ,
d a n s f
Gid n-t)ra
Expiry_Date DATE
no;
COMMENT ON TABLE CREDIT_CARD_DETAILS IS 'Credit Card Details'
;
g. PURCHASE_HISTORY
CREATE TABLE PURCHASE_HISTORY
(
Customer_ID VARCHAR2 (6) NOT NULL ,
Order_ID VARCHAR2 (6) NOT NULL
)
;
h. SHIPPING_TYPE
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
)
;
Customer_ID
)
REFERENCES CUSTOMER
(
Customer_ID
)
;
s a
ALTER TABLE ORDER_DETAILS
) h a
ADD CONSTRAINT FK_Order_details FOREIGN KEY
c o m e ฺ
e ฺ i d
(
r a cl t Gu
Shipping_Type
@ o en
)
s a d
S t ud
REFERENCES SHIPPING_TYPE pra
r u ฺ e this
(
Shipping_Type idd
alu to us
) d (g nse
; r a s a l i ce
r u P able
d a lu sfer
n ORDER_DETAILS
Gid ALTER
- t r aTABLE
non ADD CONSTRAINT Order_Details_fk FOREIGN KEY
(
Shopping_Cart_ID
)
REFERENCES SHOPPING_CART
(
Shopping_Cart_ID
)
;
d. Include a Foreign Key constraint in the PURCHASE_HISTORY table.
;
ALTER TABLE PURCHASE_HISTORY
ADD CONSTRAINT Purchase_History_CUSTOMER_FK FOREIGN KEY
(
Customer_ID
)
REFERENCES CUSTOMER
(
Customer_ID
s a
) ;
) h a
c o m eฺ
e. Include a Foreign Key constraint in the SHOPPING_CART table.leฺ i d
r a c t Gu
@ o en
ALTER TABLE SHOPPING_CART
s a d
S t ud
ฺ p
ADD CONSTRAINT SHOPPING_CART_BOOKS_FKra this FOREIGN KEY
( l u
ur use
a
dd e to
Book_ID
( g i
)
s a d cens
r a BOOKS
REFERENCES
P l e li
l u
(u
r Book_ID
f e r ab
i d da )rans
G n-t
no;
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.
b. PUBLISHER Table
Publisher_ID Publisher_Name
PN0001 Elsevier
PN0002 Penguin Group
PN0003 Pearson Education
PN0004 Cambridge University Press
PN0005 Dorling Kindersley
c. SHIPPING _TYPE
Shipping_Type Shipping_Price
USPS 200
FedEx 250
DHL 150 s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
d. CUSTOMER
l u
ur use
a
( g idd se to
Customer Customer
s a d _Name c e nStreet City Phone Credit _Card
_ ID
ra ble l i _Address _number _Number
P
ru VelasquezCarmen
CN0001
a l u f e ra 283 King Seattle 587-99-6666 000-111-222-333
d ran s Street
Gid CN0002
n -t Ngao LaDoris 5 Modrany Bratislav 586-355-8882 000-111-222-444
n o a
CN0003 Nagayama Midori 68 Via Sao 254-852-5764 000-111-222-555
Centrale Paolo
CN0004 Quick-To-See 6921 King Way Lagos 63-559-777 000-111-222-666
Mark
CN0005 Ropeburn Audry 86 Chu Street Hong 41-559-87 000-111-222-777
Kong
CN0006 Urguhart Molly 3035 Laurier Quebec 418-542-9988 000-111-222-888
Blvd.
CN0007 Menchu Roberta Boulevard de Brussels 322-504-2228 000-111-222-999
Waterloo 41
CN0008 Biri Ben 398 High St. Columbu 614-455-9863 000-111-222-222
s
CN0009 Catchpole 88 Alfred St. Brisbane 616-399-1411 000-111-222-111
Antoinette
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
e. CREDIT_CARD_DETAILS
r a
000-111-222-222s l c
iPayM 9-AUG-2024
P l e
l u ru ferab
i d da rans
G n-t
no
f. BOOKS
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
s a
BN0009 The Clicking of AN0009 175 h
PN0005
) a
Cuthbert
c o m e ฺ
e ฺ i d
r a cl t Gu
BN00010 Bride of Frankenstein AN0006 o 200 n PN0001
d @ d e
r a sa s Stu
BN00011 Shelley's Poetry and ฺp AN0005
r u e t hi 150 PN0003
Prose u
al to us
d d
d ( gi nse
BN00012
a e
saWar andlicPeace AN0004 150 PN0002
r
P able
r u
lu sfer
d a n
Gid n-tra
no
g. SHOPPING_CART
Shopping _Cart Book _ID Price Shopping _Cart Quantity
_ID _Date
SC0001 BN0002 200 12-JUN-2011 10
s a
)h a
c m
o deฺ
l e ฺ u i
a c G
@ or ent
s a d
S t ud
ฺ p ra this
l u
ur use
a
h. ORDER _DETAILS ( g idd se to
s a n
d ceShipping_
Order _ID
r a Customer
e l i Date _of _Purchase Shopping _Cart _ID
u P ab _ID l Type
l u r f r
eCN0001
d d a
OD0001
n s USPS 12-JUN-2011 SC0002
Gi OD0002
n - tra CN0002 USPS 28-JUN-2015 SC0005
o
n OD0003 CN0003 FedEx 31-JUL-2014 SC0007
OD0004 CN0004 FedEx 14-AUG-2016 SC0004
OD0005 CN0005 FedEx 21-SEP-2016 SC0003
OD0006 CN0006 DHL 28-OCT-2012 SC0001
OD0007 CN0007 DHL 11-AUG-2012 SC0006
OD0008 CN0008 DHL 18-SEP-2013 SC0008
OD0009 CN0009 USPS 25-NOV-2013 SC0009
i. PURCHASE_HISTORY
s a
Customer _ID Order _ID
) h a
CN0001 OD0001 c o m e ฺ
l e ฺ u i d
CN0003 OD0002rac G
o en t
CN0004
a d @
OD0005
t ud
s
ra tOD0007 S
CN0009
r u ฺ p
e his
d a lu us
( g id se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G na-view t named CUSTOMER_DETAILS to show the Customer Name, Customer
n o
6. Create
Address, and details of the order placed by the customer. Order the results by Customer ID.
CREATE VIEW customer_details AS
SELECT c.customer_name, c.street_address, o.order_id,
o.customer_id, o.shipping_type, o.date_of_purchase,
o.shopping_cart_id
FROM customer c JOIN order_details o
ON c.customer_id = o.customer_id;
SELECT *
FROM customer_details
ORDER BY customer_id;
b. Enter a shopping cart detail for the book details that you just entered in 7(a).
s a
) h a
c o m e ฺ
e ฺ i d
r a cl t Gu
@ o en
s a d
S t ud
ฺ p ra this
l u
ur use
a
( g idd se to
s a d cen
P ra ble li
l u ru fera
i d da rans
G n-t
no