Вы находитесь на странице: 1из 45

Displaying Data from

Multiple Tables
Chapter 4
1
Objectives
After completing this lesson, you should be able to do the following:
Write SELE! statements to access
data from more than one table using
e"uality and none"uality joins
#iew data that generally does not meet a
join condition by using outer joins
$oin a table to itself
Lesson Aim
This lesson covers how to obtain data from more than one table, using the
different methods available.
2
Cartesian roduct
! " Cartesian product is formed when#
" $oin condition is omitted
" $oin condition is invalid
"ll rows in the first table are $oined to
all rows in the second table
To avoid a Cartesian product, always include a valid $oin condition in a
%&'(' clause.
)
*enerating a Cartesian roduct
+','CT ename, dname
-(.M emp, dept/
'0"M' D0"M'
1,"2' "CC.30T40*
+M4T& "CC.30T40*
",,'0 "CC.30T40*
56 (ows +elected
4
What %s a $oin&
'se a join to "uery data from more than one table(
Old Synta)
Write the join condition in the W*E+E clause(
+','CT tablel.column, table2. column2
-(.M table1, table2
%&'(' tablel. columnl 7 table2. column2/
A,S% Synta)
Write the join condition in the O, clause(
+','CT tablel.column, table2. column2
-(.M table1 400'( 8.40 table2
.0 tablel. columnl 7 table2. column2/
-refi) the column name with the table name when the same column name appears
in more than one table(
Defining 8oins
%hen data from more than one table in the database is re9uired, a join condition
is used. (ows in one table can be $oined to rows in another table according to
common values e:isting in corresponding columns, that is, usually primary and
foreign ;ey columns.
T. display data from two or more related tables, write a simple $oin condition in
the %&'(' clause, in the synta:#
Table1.column1 denotes the table and column from which data is retrieved
Table1. column1 = table2. column2 is the condition that $oins <or relates= the
tables together.
5
!ypes of $oins
E"uijoin
,on.e"uijoin
Outer join
Self join
6
Types of 8oins
There are two main types of $oin conditions#
'9ui$oins
0on>e9ui$oins
"dditional $oin methods include the following
.uter$oins
+elf$oins
+et .perators
0ote# +et operators are not covered in this course . They are covered in another
+?, course.
@
What %s an E"uijoin&
'9ui$oins
To determine the name of an employeeAs department, you compare the value in
the D'T0. column in the 'M table with the D'T0. values in the D'T
table.
The relationship between the 'M and D'T table is an e9ui$oin > that is,
values in the D'T0. column on both tables must be e9ual.
-re9uently, this type of $oin involves primary and foreign ;ey complements.
0ote# '9ui$oins are also called simple $oins or inner$oins.
Obtaining /ata from 0ultiple !ables
+','CT e.empno, e.deptno, d.loc
-(.M emp e, dept d
%&'(' e.deptno 7 d.deptno/
Data from Multiple Tables
+ometimes you need to use data from more than one table. 4n the slide e:ample,
the report displays data from two separate tables.
'M0. e:ists in the 'M table
D'T0. e:ists in both the 'M and D'T the Tables.
,.C e:ists in the D'T table.
To prodBBce the report. you need to lin; 'M and D'T tables and access data
from both of them.
C
A,S% Synta)
SELECT e.empno, e.deptno, d.loc
FROM emp e inner join dept d
on e.deptno = d.deptno;
D
+etrieving +ecords with E"uijoins
SELE! E0-(E0-,O, E0-(E,A0E, E0-(/E-!,O,
/E-!(/E-!,O, /E-!(LO
1+O0 E0-, /E-!
W*E+E E0-(/E-!,O 2 /E-!(/E-!,O
E0-,O E,A0E /E-!,O /E-!,O LO
@6DC 1,"2' )E )E C&4C"*.
@)6D +M4T& 2E 2E D",,"+
@4DD ",,'0 )E )E C&4C"*.
14 rows selected.
(etrieving (ecords with '9ui$oins
in the slide e:amaple.
! The +','CT clause specifies the column names to retrieve#
> employee name, employee number, and department number, which
are columns in the emp table
> department number, department name, and location, which are
columns in the D'T table.
The -(.M clause specifies the two tables that the database must access#
'M table
D'T table
The %&'(' clause specifies how the tables are to be $oined#
'M.D'T0.7D'T.D'T0.
1E
Oualifying Ambiguous olumn ,ames
'se table prefi)es to "ualify column names that are in multiple tables(
%mprove performance by using table prefi)es(
/istinguish columns that have identical names but reside in different
tables by using column aliases(
?ualifying "mbiguous Column 0ames
Fou need to gualify the names of the columns in the %&'(' clause GGitli the
table names to avoid ambiguity without the table prefi:es. the D'T0. column
could be from either the D'T table or the 'M table. 4t is necessary to add the
table prefi: to e:ecute your 9uery.
4f there are no common column names between the two tables, there is no need
to 9ualify the columns. &owevwr, you will gain improved performance by using
the table prefi: because you tell the .racle +erver e:actly where to find the
columns.
11
E3'%$O%,
SELE! emp(empno, emp(ename, emp(deptno,
dept(deptno, dept(loc
1+O0 emp, dept
W*E+E emp(deptno 2 /ept(deptno4
E0-,O E,A0E /E-!,O /E-!,O LO
@6DC 1,"2' )E )E C&4C"*.
@)6D +M4T& 2E 2E D",,"+
14 rows selected.
12
3sing Table "liases
The following two scripts are e9uivalent. 4n the second one table aliases are used.
SELE! e(ename, e(deptno, d(dname
1+O0 emp e , dept d
W*E+E e(deptno 2 d(deptno 4
E,A0E /E-!,O /,A0E
1,"2' )E +",'+
+M4T& 2E ('+'"(C&
14 rows selected.
1)
Additional Search onditions
'sing the A,/ Operator
"dditional +earch Conditions
4n addition to the $oin, you may have criteria for your %&'(' clause. -or
e:ample, to display 2ingHs employee number, name, department number, and
departments localion, you need an additional condition in the %&'(' clause.
SELE! E0-(E0-,O, E0-(E,A0E, E0-(/E-!,O,
/E-!(/E-!,O, /E-!(LO
1+O0 E0-, /E-!
W*E+E E0-(/E-!,O 2 /E-!(/E-!,O
A,/ %,%!A-5ename6 2 78ing7 4
E0-,O E,A0E /E-!,O /E-!,O LO
@C)D 240* 1E 1E 0'% F.(2

14
A,S% Ssynta)
O+ALE9da %nner $oin :i yapan
1+O0 emp e, dept d W*E+E e(deptno 2 d(deptno
yerine
1+O0 emp e %,,E+ $O%, dept d
inner Join koulu yazldktan sonra iki tablo arasnda balanty kuran
O, e(deptno 2 d(deptno
koulu yazlr.
Eer, ayrca satrlardan yeni szme yaplacaksa WHERE ile istenen koul konulabilir:
W*E+E %,%!A-5ename6 2 78ing7 4
i!imindeki koul yazlabilir.
;rne<
SELE! e(empno, e(ename, e(deptno, d(deptno, d(loc
1+O0 emp e %,,E+ $O%, dept d
O, e(deptno 2 d(deptno
W*E+E %,%!A-5ename6 2 78ing7 4
EMPNO ENAME DEPTNO DEPTNO LOC
7839 KING 10 10 NEW YORK
15
,on.E"uijoins
The relationship between the 'M table and the +",*("D' table is a non>e9ui$oin,
meaning that no column in the 'M table corresponds directly to a column in the
+",*("D' table.
The relationship between the two tables is that the +", column in the 'M table is between
the ,.+", and &4+", column of the +",*("D' table.
The relationship is obtained using an operator other than e9ual <7=.
+','CT I
-(.M emp /
E0-,O E,A0E $O= 0>+ *%+E/A!E SAL O00 /E-!,O
@6DC 1,"2' M"0"*'( @C)D E1JE5J1DC1 2C5E )E
14 rows selected.
+','CT I
-(.M salgrade /
GRADE LOSAL HISAL
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
5 rows selected.
16
(etreive records where +alary in the 'M table is between low salary and high
salary in the +",*("D' table.
SELE! e(ename, e(sal, s(grade
1+O0 E0- e, SAL>+A/E s
W*E+E e(sal
=E!WEE, s(losal A,/ s(hisal 4
E,A0E SAL >+A/E
+M4T& CEE 1
8"M'+ D5E 1
14 rows selected.
1@
A,S% Synta)
SELE! e(ename, e(sal, s(grade
1+O0 E0- e %,,E+ $O%, SAL>+A/E s
O, e(sal
=E!WEE, s(losal A,/ s(hisal 4
E,A0E SAL >+A/E
+M4T& CEE 1
8"M'+ D5E 1
14 rows selected.
1C
$oining 0ore !han !wo !ables
SELE! e(ename, e(deptno, d(dname, s(grade
1+O0 emp e, dept d, salgrade s
W*E+E e(deptno 2 d(deptno A,/
e(sal =E!WEE, s(losal A,/ hisal4
E,A0E /E-!,O /,A0E >+A/E
240* 1E "CC.30T40* 5
C,"(2 1E "CC.30T40* 4
M4,,'( 1E "CC.30T40* 2
-.(D 2E ('+'"(C& 4
+C.TT 2E ('+'"(C& 4
8.0'+ 2E ('+'"(C& 4
"D"M+ 2E ('+'"(C& 1
+M4T& 2E ('+'"(C& 1
1,"2' )E +",'+ 4
",,'0 )E +",'+ )
T3(0'( )E +",'+ )
M"(T40 )E +",'+ 2
%"(D )E +",'+ 2
8"M'+ )E +",'+ 1
14 rows selected.
1D
A,S% Synta)
SELE! e(ename, e(deptno, d(dname, s(grade
1+O0 salgrade s, emp e %,,E+ $O%, dept d
O, e(deptno 2 d(deptno
W*E+E
e(sal =E!WEE, s(losal A,/ s(hisal4
E,A0E /E-!,O /,A0E >+A/E
240* 1E "CC.30T40* 5
C,"(2 1E "CC.30T40* 4
M4,,'( 1E "CC.30T40* 2
14 rows selected.
2E
(etrieving (ecords with 0on>'9ui$oins
SELE! e(ename, e(sal, s(grade
1+O0 E0- e, SAL>+A/E s
W*E+E e(sal ?e(comm @ s(hisal
E,A0E SAL >+A/E
T3(0'( 15EE 1
%"(D 125E 1
",,'0 16EE 1
M"(T40 125E 1
T3(0'( 15EE 2
%"(D 125E 2
",,'0 16EE 2
M"(T40 125E 2
M"(T40 125E )
9 rows selected.
Non-Equijoins (continued)
The slide e:ample creates a non>e9ui$oin to evaluate an employeeHs salary grade. The salary
must be between any pair of the low and high salary ranges.
4t is important to note that all employees appear e:actly once when this 9uery is
e:ecuted. 0o employee is repeated in the list. There are two reasons for this#
0one of the rows in the salary grade table contain grades that overlap. That is, the
salary value for an employee can only lie between the low salary and high salary
values of one of the rows in the salary grade table.
"ll of the employeesH salaries lie within the limits provided by the salary grade table.
That is, no employee earns less than the lowest value contained in the ,.+", column or
more than the highest value contained in the &4+", column.
,ote: .ther operators such as K7 and L7 could be used, but 1'T%''0 is the simplest.
(emember to specify the low value first and the high value last when using 1'T%''0.
Table aliases have been specified for performance reasons, not because of possible ambiguity.
21
A,S% Synta) 5,on E"uijoin6
SELE! e(ename, e(sal, s(grade
1+O0 E0- e %,,E+ $O%, SAL>+A/E s
O, e(sal ?e(comm @ s(hisal 4
ENAME SAL GRADE
ALLEN 100 1
WAR! 1250 1
"AR#IN 1250 1
#$RNER 1500 1
ALLEN 100 2
WAR! 1250 2
"AR#IN 1250 2
#$RNER 1500 2
"AR#IN 1250 3
9 rows selected.
22
Outer joins
%hen two tables are $oined with an inner $oin, data will only be returned if
matching data e:ists in both tables. "n outer $oin is li;e saying Gand also include
the rows from one table if there are no matching rows in the other one.G
%ith an outer $oin the columns from the table where data is GmissingG are
returned as 03,, values.
.uter $oins come in two basic flavours, called ,eft and (ight. ,eft outer $oins
mean that the data must be contained in the table defined to the left side of the
e9uivalence, but not necessarily the right hand side. (ight outer $oins, of course,
wor; the other way around.
To illustrate this, cut and paste the code below into a ?uery "nalyser window
and try running it. 4 have used the newer "0+4 synta: here, and the older
e9uivalents are included but commented out using the G>>G comment notation.
Comment them bac; in if you want to try them.
2)
Outer $oins
(eturning (ecords with 0o Direct Match with .uter 8oins
4f a row does not satisfy a $oin condition, the row will not appear in the 9uery
result. -or e:ample, in the e9ui$oin condition of 'M and D'T tables,
department .'("T4.0+ does not appear because no one wor;s in that
department.
+','CT e.ename , e.deptno, d.dname
-(.M emp e, dept d
%&'(' e.deptno 7 d.deptno/
E,A0E /E-!,O /,A0E
1,"2' )E +",'+
+M4T& 2E ('+'"(C&
",,'0 )E +",'+
%"(D )E +",'+
8.0'+ 2E ('+'"(C&
M"(T40 )E +",'+
C,"(2 1E "CC.30T40*
+C.TT 2E ('+'"(C&
240* 1E "CC.30T40*
T3(0'( )E +",'+
"D"M+ 2E ('+'"(C&
8"M'+ )E +",'+
-.(D 2E ('+'"(C&
M4,,'( 1E "CC.30T40*
14 rows selected.
No em!o"ee in t#e OPERATIONS de$%tment
24
Outer $oins
Retu%nin& Reco%ds 'it# No Di%ect M$tc# 'it# Oute% (oins
4f a row does not satisfy a $oin condition, the row will not appear in the 9uery
result. -or e:ample, in the e9ui$oin condition of 'M and D'T tables,
department .'("T4.0+ does not appear because no one wor;s in that
department.
.uter 8oins <.ld usage=
Aou use an outer join to also see rows that do not usually meet the
join condition(
Outer join operator is the plus sign 5?6(
SELE! tablel(column, tableB( column
1+O0 tablel, tableB
W*E+E tablel(column5?6 2 tableB(column4
SELE! tablel(column, tableB(column
1+O0 tablel, tableB
W*E+E tablel(column( 2 tableB( column C?6 4
(eturning (ecords with 0o Direct Match with .uter 8oins
The missing row<s= can be returned if an outer$oin operator is used in the $oin
condition. The operator is a plus sign enclosed in parenthesis <M=, and it is placed
on the GsideG of the e9uality that the $oin rhctt a defBcient in mfNrmcBnun. This
operator has the effect of creating one or more mil l rows, to which one or more
rows from the nondeficient table can be $oined in the synta:.
4n the condition that $oins <or relates= the lables together, is the outer $oin
symbol, which can be placed on either side of the %&'(' clause condition, but
not on both sides <lace the outer $oin symbol following the name of the column
in the table without the matching rows.=
25
.ld 3sage
+','CT e.ename, e.deptno, d.dname
-(.M emp e, dept d
%&'(' e.deptno<M= 7 d.deptno/
ENAME DEPTNO DNAME
%"I#& 20 RE%EAR'&
ALLEN 30 %ALE%
WAR! 30 %ALE%
(ONE% 20 RE%EAR'&
"AR#IN 30 %ALE%
)LAKE 30 %ALE%
'LARK 10 A''O$N#ING
%'O## 20 RE%EAR'&
KING 10 A''O$N#ING
#$RNER 30 %ALE%
A!A"% 20 RE%EAR'&
(A"E% 30 %ALE%
*OR! 20 RE%EAR'&
"ILLER 10 A''O$N#ING
O+ERA#ION%
15 rows selected.
26
A,S% +ight Outer $oin Synta)
SELE! e(ename, e(deptno, d(dname
1+O0 emp e +%>*! O'!E+ $O%, dept d
O, e(deptno 2 d(deptno4
ENAME DEPTNO DNAME
%"I#& 20 RE%EAR'&
ALLEN 30 %ALE%
WAR! 30 %ALE%
(ONE% 20 RE%EAR'&
"AR#IN 30 %ALE%
)LAKE 30 %ALE%
'LARK 10 A''O$N#ING
%'O## 20 RE%EAR'&
KING 10 A''O$N#ING
#$RNER 30 %ALE%
A!A"% 20 RE%EAR'&
(A"E% 30 %ALE%
*OR! 20 RE%EAR'&
"ILLER 10 A''O$N#ING
O+ERA#ION%
15 rows selected.
2@
A,S% Left Outer $oin Synta)
SELE! e(ename, e(deptno, d(dname
1+O0 emp e +%>*! O'!E+ $O%, dept d
O, e(deptno 2 d(deptno4
ENAME DEPTNO DNAME
%"I#& 20 RE%EAR'&
ALLEN 30 %ALE%
WAR! 30 %ALE%
(ONE% 20 RE%EAR'&
"AR#IN 30 %ALE%
)LAKE 30 %ALE%
'LARK 10 A''O$N#ING
%'O## 20 RE%EAR'&
KING 10 A''O$N#ING
#$RNER 30 %ALE%
A!A"% 20 RE%EAR'&
(A"E% 30 %ALE%
*OR! 20 RE%EAR'&
"ILLER 10 A''O$N#ING
14 rows selected.
2C
.ld usage
+','CT e.ename, e.deptno, d.dname
-(.M emp e, dept d
%&'(' e.deptno 7 d.deptno<M=/
ENAME DEPTNO DNAME
%"I#& 20 RE%EAR'&
ALLEN 30 %ALE%
WAR! 30 %ALE%
(ONE% 20 RE%EAR'&
"AR#IN 30 %ALE%
)LAKE 30 %ALE%
'LARK 10 A''O$N#ING
%'O## 20 RE%EAR'&
KING 10 A''O$N#ING
#$RNER 30 %ALE%
A!A"% 20 RE%EAR'&
(A"E% 30 %ALE%
*OR! 20 RE%EAR'&
"ILLER 10 A''O$N#ING
14 rows selected.
2D
O'!E+ $O%,
reviously, we had loo;ed at left $oin, or inner $oin, where we select rows common to the
participating tables to a $oin. %hat about the cases where we are interested in selecting
elements in a table regardless of whether they are present in the second tableO %e will now
need to use the S3L O'!E+ $O%, command.
The synta: for performing an outer $oin in +?, is database>dependent. -or e:ample, in
.racle, we will place an G<M=G in the W*E+E clause on the other side of the table for which
we want to include all the rows.
,etHs assume that we have the following two tables,
Table Store_Information
storePname +ales Date
,os "ngeles Q15EE 8an>E5>1DDD
+an Diego Q25E 8an>E@>1DDD
,os "ngeles Q)EE 8an>EC>1DDD
1oston Q@EE 8an>EC>1DDD
Table Geography
regionPname storePname
'ast 1oston
'ast 0ew For;
%est ,os "ngeles
%est +an Diego
and we want to find out the sales amount for all of the stores. 4f we do a regular $oin, we will
not be able to get what we want because we will have missed G0ew For;,G since it does not
appear in the Store_Information table. Therefore, we need to perform an outer $oin on the
two tables above#
)E
O'!E+ $O%,
SELE! AD(storeEname, S'05AB(Sales6 SALES
1+O0 >eography AD, StoreE%nformation AB
W*E+E AD(storeEname 2 AB(storeEname 5?6
>+O'- =A AD(storeEname
0ote that in this case, we are using the .racle synta: for outer $oin.
Result:
store_name SALES
Boston $700
New York
Los Angeles $1800
San Diego $!0
0ote# 03,, is returned when there is no match on the second table. 4n this case, G0ew For;G
does not appear in the table Store_Information, thus its corresponding G+",'+G column is
03,,.
)1
O+/E+ =A usage in O'!E+ $O%,
SELE! e(ename, d(/E-!,O, d(dname
1+O0 emp e, dept d
W*E+E e(deptno5?6 2 d(deptno
O+/E+ =A e(deptno4
E,A0E /E-!,O /,A0E
",,'0 )E +",'+
%"(D )E +",'+
4E .'("T4.0+
15 rows selected.
)2
A,S% Synta) for O'!E+ $O%,
SELE! e(ename, d(/E-!,O, d(dname
1+O0 emp e +%>*! O'!E+ $O%, dept d
O, e(deptno 2 d(deptno
O+/E+ =A e(deptno4
ENAME DEPTNO DNAME
'LARK 10 A''O$N#ING
"ILLER 10 A''O$N#ING
KING 10 A''O$N#ING
(ONE% 20 RE%EAR'&
%"I#& 20 RE%EAR'&
%'O## 20 RE%EAR'&
*OR! 20 RE%EAR'&
A!A"% 20 RE%EAR'&
WAR! 30 %ALE%
#$RNER 30 %ALE%
ALLEN 30 %ALE%
(A"E% 30 %ALE%
"AR#IN 30 %ALE%
)LAKE 30 %ALE%
40 O+ERA#ION%
15 rows selected.
))
3sing .uter 8oins
+','CT e.ename, d.D'T0., d.dname
-(.M emp e, dept d
%&'(' e.deptno<M= 7 d.deptno
.(D'( 1F e.deptno/
E,A0E /E-!,O /,A0E
M4,,'( 1E "CC.30T40*
240* 1E "CC.30T40*
C,"(2 1E "CC.30T40*
+M4T& 2E ('+'"(C&
-.(D 2E ('+'"(C&
"D"M+ 2E ('+'"(C&
+C.TT 2E ('+'"(C&
8.0'+ 2E ('+'"(C&
T3(0'( )E +",'+
8"M'+ )E +",'+
",,'0 )E +",'+
M"(T40 )E +",'+
1,"2' )E +",'+
%"(D )E +",'+
4E .'("T4.0+
15 rows selected.
)4

8oining a Table to 4tself
S3L $O%,
0ow we want to loo; at $oins. To do $oins correctly in +?, re9uires many of the elements we
have introduced so far. ,etHs assume that we have the following two tables,
Table Store_Information
storePname +ales Date
,os "ngeles Q15EE 8an>E5>1DDD
+an Diego Q25E 8an>E@>1DDD
,os "ngeles Q)EE 8an>EC>1DDD
1oston Q@EE 8an>EC>1DDD
Table Geography
regionPname storePname
'ast 1oston
'ast 0ew For;
%est ,os "ngeles
%est +an Diego
and we want to find out sales by region. %e see that table Geography includes information on
regions and stores, and table Store_Information contains sales information for each store. To
get the sales information by region, we have to combine the information from the two tables.
':amining the two tables, we find that they are lin;ed via the common field, GstorePnameG.
%e will first present the +?, statement and e:plain the use of each segment later#
)5
8oining a Table to 4tself
SELE! AD(regionEname +E>%O,, S'05AB(Sales6 SALES
1+O0 >eography AD, StoreE%nformation AB
W*E+E AD(storeEname 2 AB(storeEname
>+O'- =A AD(regionEname
Result:
+E>%O, SALES
East FGHH
West FBHIH
The first two lines tell +?, to select two fields, the first one is the field GregionPnameG from
table Geography <aliased as ('*4.0=, and the second one is the sum of the field G+alesG
from table Store_Information <aliased as +",'+=. 0otice how the table aliases are used here#
*eography is aliased as "1, and +toreP4nformation is aliased as "2. %ithout the aliasing, the
first line would become
SELE! >eography(regionEname +E>%O,,
S'05StoreE%nformation(Sales6 SALES
which is much more cumbersome. 4n essence, table aliases ma;e the entire +?, statement
easier to understand, especially when multiple tables are included.
0e:t, we turn our attention to line ), the W*E+E statement. This is where the condition of
the $oin is specified. 4n this case, we want to ma;e sure that the content in GstorePnameG in
table *eography matches that in table Store_Information, and the way to do it is to set them
e9ual. This W*E+E statement is essential in ma;ing sure you get the correct output. %ithout
the correct W*E+E statement, a Cartesian 8oin will result. Cartesian $oins will result in the
9uery returning every possible combination of the two <or whatever the number of tables in
the 1+O0 statement= tables. 4n this case, a Cartesian $oin would result in a total of 4 : 4 7 16
rows being returned.
)6
Self $oins
0>+ in the WO+8E+ table is e"ual to E0-,O in the 0A,A>E+ table(
SELE! WO+8E+(ename, WO+8E+(empno ,
0A,A>E+(ename, 0A,A>E+(empno
1+O0 emp WO+8E+, emp 0A,A>E+
W*E+E WO+8E+(mgr 2 0A,A>E+(empno 4
E,A0E E0-,O E,A0E E0-,O
8"M'+ @DEE 1,"2' @6DC
T3(0'( @C44 1,"2' @6DC
M"(T40 @654 1,"2' @6DC
%"(D @521 1,"2' @6DC
",,'0 @4DD 1,"2' @6DC
-.(D @DE2 8.0'+ @566
+C.TT @@CC 8.0'+ @566
M4,,'( @D)4 C,"(2 @@C2
"D"M+ @C@6 +C.TT @@CC
C,"(2 @@C2 240* @C)D
8.0'+ @566 240* @C)D
1,"2' @6DC 240* @C)D
+M4T& @)6D -.(D @DE2
1) rows selected.
)@
Self $oins
'se two alias for emp:
SELE! e(ename, e(empno , m(ename, m(empno
1+O0 emp e, emp m
W*E+E e(mgr 2 m(empno 4
E,A0E E0-,O E,A0E E0-,O
8"M'+ @DEE 1,"2' @6DC
T3(0'( @C44 1,"2' @6DC
M"(T40 @654 1,"2' @6DC
%"(D @521 1,"2' @6DC
",,'0 @4DD 1,"2' @6DC
-.(D @DE2 8.0'+ @566
+C.TT @@CC 8.0'+ @566
M4,,'( @D)4 C,"(2 @@C2
"D"M+ @C@6 +C.TT @@CC
C,"(2 @@C2 240* @C)D
8.0'+ @566 240* @C)D
1,"2' @6DC 240* @C)D
+M4T& @)6D -.(D @DE2
1) rows selected.
)C
A,S% Synta) for SEL1 $O%,
SELE! e(ename, e(empno , m(ename, m(empno
1+O0 emp e %,,E+ $O%, emp m
O, e(mgr 2 m(empno 4
ENAME EMPNO ENAME EMPNO
%"I#& 739 *OR! 7902
ALLEN 7499 )LAKE 798
WAR! 7521 )LAKE 798
(ONE% 75 KING 7839
"AR#IN 754 )LAKE 798
)LAKE 798 KING 7839
'LARK 7782 KING 7839
%'O## 7788 (ONE% 75
#$RNER 7844 )LAKE 798
A!A"% 787 %'O## 7788
(A"E% 7900 )LAKE 798
*OR! 7902 (ONE% 75
"ILLER 7934 'LARK 7782
13 rows selected.
)D
SEL1 $O%,S
SELE! wor<er(ename JJ 7wor<s for 7 JJ manager(ename
1+O0 emp wor<er, emp manager
W*E+E wor<er(mgr 2 manager(empno4
)OR*ER+ENAME,,-)OR*S.OR-,,MANAGER+ENAME
%"I#&wor,s -or *OR!
ALLENwor,s -or )LAKE
WAR!wor,s -or )LAKE
(ONE%wor,s -or KING
"AR#INwor,s -or )LAKE
)LAKEwor,s -or KING
'LARKwor,s -or KING
%'O##wor,s -or (ONE%
#$RNERwor,s -or )LAKE
A!A"%wor,s -or %'O##
(A"E%wor,s -or )LAKE
*OR!wor,s -or (ONE%
"ILLERwor,s -or 'LARK
13 rows selected.
(oinin& $ T$/!e to Itse!0 (continued)
The slide e:ample $oins the 'M table to itself. To simulate two tables in the
-(.M clause, there are two aliases, namely %.(2'( and M"0"*'(, for
the same table 'M.
4n this e:ample, the %&'(' clause contains the $oin that means HHwhere a
wor;erHs manager number matches the employee number for the manager.
4E
A,S% synta) for SEL1 $O%,S
SELE! wor<er(ename JJ 7wor<s for 7 JJ manager(ename
1+O0 emp wor<er %,,E+ $O%, emp manager
O, wor<er(mgr 2 manager(empno4
)OR*ER+ENAME,,-)OR*S.OR-,,MANAGER+ENAME
%"I#&wor,s -or *OR!
ALLENwor,s -or )LAKE
WAR!wor,s -or )LAKE
(ONE%wor,s -or KING
"AR#INwor,s -or )LAKE
)LAKEwor,s -or KING
'LARKwor,s -or KING
%'O##wor,s -or (ONE%
#$RNERwor,s -or )LAKE
A!A"%wor,s -or %'O##
(A"E%wor,s -or )LAKE
*OR!wor,s -or (ONE%
"ILLERwor,s -or 'LARK
13 rows selected.
41
Se!0 (oins
+','CT e.ename, e.empno , m.ename, m.empno
-(.M emp e, emp m
%&'(' e.mgr 7 m.empno/
E,A0E E0-,O E,A0E E0-,O
8"M'+ @DEE 1,"2' @6DC
T3(0'( @C44 1,"2' @6DC
1) rows selected.
(oinin& $ T$/!e to Itse!0
+ometimes you need to $oin a table to itself. To find the name of each
employeeHs manager, you need to $oin the 'M table to itself, or perform a self
$oin. -or e:ample, to find the name of 1la;eHs manager, you need to#
-ind 1la;e in the 'M table by loo;ing at the '0"M' column.
-ind the manager number for 1la;e by loo;ing at the M*( column.
1la;eHs manager number is @C)D.
-ind the name of the manager with 'M0. @C)D by loo;ing at the
'0"M' column. 2ingHs employee number is @C)D, so 2ing is 1la;eHs
manager.
4n this process, you loo; in the table twice. The first time you loo; in the table to
find 1la;e in the '0"M' column and M*( value of @C)D. The second time you
loo; in the 'M0. column to find @C)D and the '0"M' column to find 2ing
A,S% Synta) for Self $oin
SELE! e(ename, e(empno , m(ename, m(empno
1+O0 emp e %,,E+ $O%, emp m
O, e(mgr 2 m(empno4
42
Summary
SELE! tablel( Column , table2. column
1+O0 tablel , table2
W*E+E tablel. columnl 2 tableB( column2;
Equijoin
Non-equijoin
Oute% join
Se!0 join
Summ$%"
There are multiple ways to $oin tables. The common thread, though, is that you
want to lin; them through a condition in the %&'(' clause. The method you
choose will be based on the re9uired result and the data structures that you are
using.
4)
':ercices
+olution 1
+','CT e.ename, e.deptno, d.dname
-(.M emp e , dept d
%&'(' e.deptno 7 d.deptno /
+olution 2
+','CT e.$ob, d.loc
-(.M emp e , dept d
%&'(' e.deptno 7 d.deptno
"0D e.deptno 7 )E/
+olution )
+','CT e.ename, d.dname, d.loc
-(.M emp e , dept d
%&'(' comm 4+ 0.T 03,,
"0D e.deptno 7 d.deptno /
+olution 4
+','CT e.ename, d.dname, d.loc
-(.M emp e , dept d
%&'(' comm 4+ 0.T 03,,
"0D e.deptno 7 d.deptno /
+olution 5
+','CT e.ename, e.$ob, e.deptno, d.dname
-(.M emp e, dept d
%&'(' e.deptno 7 d.deptno
"0D
d.loc 7 HD",,"+H /
+olution 6
+','CT e.ename GRSTiG , e.empno GRSTi 0oG ,
m.ename GManagerG , m.empno GMgr 0oG
-(.M emp e, emp m
%&'(' e.mgr 7 m.empno /
+olution @
+','CT e.ename GRSTiG , e.empno GRSTi 0oG ,
m.ename GManagerG , m.empno GMgr 0oG
-(.M emp e, emp m
%&'(' e.mgr 7 m.empno<M= /
44
+olution Ca
+','CT e.deptno G1UlNmG , e.ename GRSTiG ,
b.ename
-(.M emp e , emp b
%&'(' e.deptno 7 b.deptno
.(D'( 1F e.empno /
+olution Cb
+','CT e.deptno G1UlNmG , e.ename GRSTiG ,
b.ename
-(.M emp e , emp b
%&'(' e.deptno 7 b.deptno
"0D e.ename V7 b.ename
.(D'( 1F e.empno /
+olution Cc
+','CT e.deptno G1UlNmG , e.ename GRSTiG , b.ename
-(.M emp e , emp b
%&'(' e.deptno 7 b.deptno
"0D e.ename KL b.ename
.(D'( 1F e.deptno /
+olution Da
D'+C salgrade/
+olution Db
+','CT e.ename GRSTiG , e.$ob GRSiG , d.dname G1UlNmNG ,
e.sal GMaaSG , s.grade G1aremG
-(.M emp e, dept d , salgrade s
%&'('
e.deptno 7 d.deptno
"0D e.sal 1'T%''0 s.losal "0D s.hisal /
+olution 1E
+','CT e.ename GRSTiG , e.hiredate GRSe *iriS TarihiG , b.hiredate G1la;eG
-(.M emp e, emp b
%&'('
e.hiredate L b.hiredate
"0D b.ename 7 H1,"2'H /
+olution 11
+','CT e.ename GRSTiG , e.hiredate GRSe *iriS TarihiG , m.ename GManageriG ,
m.hiredate GManagerin *iriS TarG
-(.M emp e, emp m
%&'('
e.hiredate K m.hiredate
"0D e.mgr 7 m.empno /
45

Вам также может понравиться