Lecture#08
Lecture#08
Chapter-4
Relational Algebra & Relational Calculus
Join Operations
Today’s Lecture Outlines
• Join Operation
• Various forms / types of join operation Theta
join
– Equijoin (a particular type of Theta join)
– Natural join
– Outer join
• Left Outer Join
• Right Outer Join
• Sample examples
1. Join Operation
• Typically, when we want only combinations of the Cartesian
product that satisfy certain conditions and so we would
normally use a Join operation instead of the Cartesian product
operation
– Join is a derivative of Cartesian product and is often
represented by (
)
• Equivalent to performing a Selection, using join predicate as
selection formula, over Cartesian product of the two operand
relations.
• One of the most difficult operations to implement efficiently in
an RDBMS and one reason why RDBMSs have intrinsic
performance problems.
Types of Joins
• R F S
– We apply the condition through select on one
relation and then only those rows are used in the
cross product with the second relation
– If R and S are two relations then F is the condition,
which is applied for select operation on one relation
and then only selected rows are cross product with all
the rows of second relation
– The predicate F is of the form R.ai S.bi where may
be one of the comparison operators (<, , >, , =, ).
Theta Join Example
FACULTY COURSE
F3456 Tahir CSE 230000 Asso Prof C3456 Database Systems F2345
F4567 Ayesha ENG 270000 Asso Prof C3456 Database Systems F2345
FACULTY (COURSE)
FACULTY.facId = COURSE.facId
F4567 Ayesha ENG 270000 Asso Prof C5678 Money & Capital F4567
Market
F2345 Usman CSE 210000 lecturer C3425 Introduction to F2345
Accounting
1.3 Natural Join
• R S
– This is the most common and general form of join.
If we simply say join, it means the natural join
– The Natural join is an Equijoin of the two relations
R and S over all common attributes x. One
occurrence of each common attribute is
eliminated from the result.
– Same as equijoin with common column appearing
once
Natural Join Example (1)
FACULTY COURSE
FACULTY (COURSE)
FACULTY.facId, COURSE.facId
Natural Join Example (2)
FACULTY (COURSE)
FACULTY.facId, COURSE.facId
F4567 Ayesha ENG 270000 Asso Prof C5678 Money & Capital
Market
R S
• The (left) Outer join is a join in which tuples from R that do
not have matching values in the common attributes of S are
also included in the result relation. Missing values in the
second relation are set to null
– Keep all of the tuples from the “left” relation
– Join with the right relation
– Pad the non-matching tuples with nulls
Left Outer Join Example (1)
FACULTY COURSE
F4567 Ayesha ENG 270000 Asso Prof C5678 Money & Capital Market F4567
. To Accounting
F5678 Samad MNG 320000 Professor
1.4.2 Right Outer Join
R S
• Same as the left, but keep tuples from
the “right” relation
• Keeps every tuple in the right-hand
relation in the result.
Right Outer Join Example
FACULTY COURSE
(MAGAZINE)
MName, Publisher
Practice Exercise (3)
2.Selects tuples from MAGAZINE relation where the Publisher
is ‘Thomas Connoly’ and magazine name is ‘Database Systems’.
OR
OR
MID, MName (MAGAZINE) Topic =‘Motorcycles’ or Topic=‘Cars’(Articles)