Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ORACLE
(SQL Components)
DDL
DML
JL_D.B.
DCL
(Retrieve Data and
Produce Information
from Multiple Tables)
Dr. Chen, Oracle Database System (Oracle)
Chapter 9
Joining Data from Multiple
Tables
(p.284-296; p.312-330)
Jason C. H. Chen, Ph.D.
Professor of MIS
School of Business
Gonzaga University
Spokane, WA 99258 USA
chen@jepson.gonzaga.edu
Dr. Chen, Oracle Database System (Oracle)
Objectives
Objectives (continued)
pk
warehouses
wh_id
NUMBER(2)
pk
warehouses
location
VARCHAR2(12)
Publishe2
ID
NUMBER(2)
pk
Name
VARCHAR2(23)
Contact
VARCHAR2(15)
Phone
VARCHAR2(12)
Publishe3
ID
Name
Contact
Phone
NUMBER(2)
VARCHAR2(23)
VARCHAR2(15)
VARCHAR2(12)
Publishe2
pk
Publishe3
Employees
EMPNO
LNAME
FNAME
JOB
HIREDATE
DEPTNO
MTHSAL
MGR
NUMBER(4)
VARCHAR2(20)
VARCHAR2(15)
VARCHAR2(19)
DATE
NUMBER(2)
NUMBER(7,2)
NUMBER(4)
Purpose of Joins
Joins are used to link tables and reconstruct
data in a relational database
Joins can be created through:
Conditions in a WHERE clause
Use of JOIN keywords in FROM clause
Cartesian Joins
Created by omitting joining condition in the
WHERE clause or through CROSS JOIN
keywords in the FROM clause
Results in every possible row combination
(m * n)
They are useful when
performing certain statistical procedures for
data analysis
Figure 9-3
A: Use of
Equality Joins (or
inner/simple/natu
ral).
-- chapter 9, Figure 9-3; p.288
SELECT title, name
FROM books, publisher;
10
' Count
Figure 9-4
Equality Joins
Link rows through equivalent data that
exists in both tables
Created by:
Creating equivalency condition in the WHERE
clause
Using NATURAL JOIN, JOINUSING, or
JOINON keywords in the FROM clause
12
13
Q: why
Brown is not
on the result?
-- Exatra example
SELECT s_id, s_last, s_first,
student.f_id, f_last
FROM student, faculty
WHERE student.f_id =
faculty.f_id;
Dr. Chen, Oracle Database System (Oracle)
S_ID
-----1
2
3
4
5
6
6 rows
S_LAST
S_FIRST F_ID F_LAST
-------- -------- ---- -------Jones
Tammy
1 Cox
Perez
Jorge
1 Cox
Marsh
John
1 Cox
Smith
Mike
2 Blanchard
Johnson Lisa
4 Sheng
Nguyen
Ni
3 Williams
selected.
14
S_ID
-----1
2
3
4
5
6
6 rows
S_LAST
S_FIRST F_ID F_LAST
-------- -------- ---- -------Jones
Tammy
1 Cox
Perez
Jorge
1 Cox
Marsh
John
1 Cox
Smith
Mike
2 Blanchard
Johnson Lisa
4 Sheng
Nguyen
Ni
3 Williams
selected.
15
Figure 9-6
An equality join
16
Figure 9-8
-- Use aliases
-- chapter 9, Figure 9-9; p.294
SELECT b.title, b.pubid, p.name
FROM books b, publisher p
WHERE b.pubid = p.pubid
AND (b.cost < 15 OR p.pubid = 1)
ORDER BY title;
Dr. Chen, Oracle Database System (Oracle)
18
Customers
Orders
Orderitems
Books
customer# (j)
lastname (d)
firstname (d)
order# (j)
customer# (j)
order# (j)
isbn (j)
isbn (j)
title (d)
j: join
s: search
d: display
Dr. Chen, Oracle Database System (Oracle)
19
Orders
Orderitems
Books
customer# (j)
lastname (d)
firstname (d)
order# (j)
customer# (j)
order# (j)
isbn (j)
isbn (j)
title (d)
j: join
s: search
You can derive your query from the diagram by following these
steps:
d: display
20
Orders
Orderitems
Books
customer# (j)
lastname (d)
firstname (d)
order# (j)
customer# (j)
order# (j)
isbn (j)
isbn (j)
title (d)
j: join
s: search
d: display
21
Joining N
tables
requires
___ join
conditions
22
Orders
Orderitems
Books
customer# (j)
lastname (d)
firstname (d)
order# (j)
customer# (j)
order# (j)
isbn (j)
isbn (j)
title (d)
category (s)
_______
j: join
s: search
d: display
23
24
25
26
Break
Assignments (#2 & #5 on p. 329; see at the end of
the slides)
Figure out how to produce the output with $
displayed (below is a sample output for #5, p.329)
hint:
a) column
b) TO_CHAR (see p.365-367 and learning to learn)
TITLE
PROFIT
------------------------------ -------PAINLESS CHILD-REARING
$37.45
HOW TO MANAGE THE MANAGER
$16.55
PAINLESS CHILD-REARING
$37.45
27
Query: list all author IDs with books in the Family Life category
SELECT ba.authorid
FROM books b JOIN bookauthor ba
USING (isbn)
WHERE category = 'FAMILY LIFE;
Query: list all author IDs with books in the Children category
SELECT ba.authorid
FROM books b JOIN bookauthor ba
USING (isbn)
WHERE category = 'CHILDREN';
28
Query: list all author IDs with books in the Family Life or
Children category
-- chapter 9, Figure 9-28; p. 313(version 2)
SELECT ba.authorid
FROM books b JOIN bookauthor ba
USING (isbn)
WHERE category = FAMILY LIFE
OR category = CHILDREN;
Anything not appropriate in
the output?
How to take care this type of
problem in an easy way?
Dr. Chen, Oracle Database System (Oracle)
29
30
SET THEORY
31
SET THEORY
C
A-B
B-A
=A-B
=B-A
Dr. Chen, Oracle Database System (Oracle)
=A INTERSECT B
All Regions
=A UNION B
(with C counted just once)
32
SET THEORY
A
D
B
A MINUS B=D
A INTERSECT B= C
B = D+C+E
C
A UNION ALL B = D+C+E+__
33
34
Query: list all author IDs with books in the Family Life or
Children category
SELECT ba.authorid
FROM books b JOIN bookauthor ba
USING (isbn)
WHERE category = 'FAMILY LIFE
UNION
SELECT ba.authorid
FROM books b JOIN bookauthor ba
USING (isbn)
WHERE category = 'CHILDREN';
35
36
Query: list all customer numbers and those customers who have
placed an order recently
SELECT customer#
FROM customers
INTERSECT
SELECT customer#
FROM orders;
Figure 9-34 Identifying
overlapping values with the
INTERSECT set operator
37
38
LOCATION
loc_id (j)
bldg_code (s)
39
COURSE_
SECTION
f_id (j)
loc_id (j)
LOCATION
loc_id (j)
bldg_code (s)
40
Office
in BUS
UNION
SELECT f_first, f_last
FROM faculty, location, course_section
WHERE faculty.f_id = course_section.f_id
AND location.loc_id = course_section.loc_id
AND bldg_code = 'BUS';
Dr. Chen, Oracle Database System (Oracle)
Taught
courses
in BUS
41
Office
in BUS
INTERSECT
SELECT f_first, f_last
FROM faculty, location, course_section
WHERE faculty.f_id = course_section.f_id
AND location.loc_id = course_section.loc_id
AND bldg_code = 'BUS';
Dr. Chen, Oracle Database System (Oracle)
Taught
courses
in BUS
42
Taught
courses
in BUS
MINUS
SELECT f_first, f_last
FROM faculty, location, course_section
WHERE faculty.f_id = course_section.f_id
AND location.loc_id = course_section.loc_id
AND bldg_code = 'BUS';
Dr. Chen, Oracle Database System (Oracle)
Office
in BUS
43
44
45
46
5. Next, you should use MS/Word to open the *.txt file and
include/DRAW Query Design Diagram figures for queries with
multiple tables. You may use font of Courier new to align the
output.
6. Save it as *.doc (or docx) file.
Email me with the *.doc (or *.docx) file that includes all required documents
with attachment to:
chen@jepson.gonzaga.edu
with subject title of
bmis441_Oracle_ch9
47
Summary
Data stored in multiple tables regarding a single
entity can be linked together through the use of
joins
A Cartesian join between two tables returns every
possible combination of rows from the tables; the
resulting number of rows is always m * n
An equality join is created when the data joining
the records from two different tables are an exact
match
A non-equality join establishes a relationship based
upon anything other than an equal condition
Self-joins are used when a table must be joined to
itself to retrieve needed data
Dr. Chen, Oracle Database System (Oracle)
48
Summary (continued)
Inner joins are categorized as being equality, nonequality, or self-joins
An outer join is created when records need to be
included in the results without having
corresponding records in the join tables
The record is matched with a NULL record so it will be
included in the output
49