Beruflich Dokumente
Kultur Dokumente
ADVANCED SQL
(PART I)
JOINS
Outer join–a join in which
rows that do not have matching values in
common columns are nonetheless
included in the result table
(as opposed to inner join, in which rows must
have matching values in order to appear in
the result table)
• Structure: Discouraged
• FROM Table1 JOIN Table2 ON (need ( )
Table1.PK = Table2.FK when N >=3)
• Compare last slide:
• FROM Table1, Table2 Recommend
WHERE Table1.PK = Table2.FK ed
Copyright © 2016 Pearson Education, Inc. 6-14
EQUI-JOIN EXAMPLE – ALTERNATIVE
SYNTAX
SELECT E.EmpNo, E.EName, E.DeptNo,
D.DeptNo, D.Loc
FROM TblEmployee E INNER JOIN
TblDepartment D ON E.DeptNo=D.DeptNo;
Results
EVERY
row in
CUST Unlike
table INNER
[even w/o join, this
will
order], include
PLUS customer
Those rows with
matching no
rows in matching
order
ORDER rows
Refer
Copyright © 2016 back
Pearson to Fig
Education, Inc. 6-2 for
Figure 6-2
Visualization of different join types with results returned
in shaded area
Do NOT
use outer
join except
when the
biz
scenario Customer_T Order_T
calls for it
CUSTOMER_T is the outer table – ”all records returned
from it” even there is no matching: those who did not
place an order …
Had we reversed the order the tables were listed, …
Copyright © 2016 Pearson Education, Inc. 6-20
FIGURE “6-2” (EXTENDED) - RIGHT OUTER JOIN:
WATCH FOR THE SEQUENCE OF TABLE
Logically,
A LEFT join B
=
B RIGHT join
A:
All rows in A,
plus matching
rows in B
A LEFT JOIN B
B…ARIGHT
Put A on right, and RIGHT JOIN JOIN
Copyright © 2016 Pearson Education, Inc. A 6-23
Don’t abuse outer
DISCUSSION: OUTER JOIN (3) join: discussion 7
Similarly: Mirror
B LEFT JOIN A produces the same result as
A RIGHT JOIN B
ALL rows in B (including unmatched rows) are
displayed, while A only has matched rows displayed
Copyright © 2016 Pearson Education, Inc. 6-27
DISCUSSION (*7*): “GOLDEN RULES” OF OUTER JOIN
Rule 1:
OUTER JOIN can only join 2
tables ! !
*IF*attempt to joinofmore
5-10% than
students twothis
made tables in
outer join, unexpectedmistake
outcomes would
result.
Rule 2:
Do NOT abuse outer join: UNLESS you
truly want ALL rows ~15-20% from one table
of students made this
(including unmatched rows),mistake
Copyright © 2016 Pearson Education, Inc.
do NOT 6-28
DISCUSSION (*7.2*): “GOLDEN RULES” OF OUTER
JOIN
NEVER use OUTER JOIN
…Unless you are told/hinted to do so.
OUTER JOIN is not a natural thing you
should think first:
Ithas VERY specific application scenario,
which is:
Among the two tables involved, only when
you want… (complete the sentence)
Unless the intention in the above bullet is
stated, NEVER use OUTER JOIN
Abuse of outer join is very absurd: “out of
Copyright © 2016 Pearson Education, Inc. 6-29
NOT (!!)
outer
MULTIPLE TABLE JOIN EXAMPLE join!
Assemble all information necessary to create an invoice for
order number 1006
Four tables
involved in
this join
“Middle” table
must be joined,
even no filed listed
Copyright © 2016 Pearson Education, Inc. 6-31
MULTIPLE TABLE JOIN - *NOTE 1*
“Bridge” table(s) must be joined, even there are no fields
from these tables are selected to display.
NOT (!!)
SELECT A.field1, B.field2, B.field3, D.field1 outer
FROM A, B, C, D join!
WHERE…
A B C D
Employe’s
supervisorID is From Chapter 2
Supervisor’s
EmployID
Unary
1:N
Employee’s Employee’s
EmployeeID SupervisorID
= =
Supervisor’s Supervisor’s
EmployeeID EmployeeID
Copyright © 2016 Pearson Education, Inc. 6-43
SELF-JOIN EXAMPLE
Join version
Subquery version