0% found this document useful (0 votes)
2 views

Lecture#08

The document discusses various types of join operations in relational algebra, including Theta join, Equijoin, Natural join, and Outer joins (Left and Right). It explains the purpose of joins in relational databases, how they differ from Cartesian products, and provides examples for each type of join. Additionally, it includes practice exercises for applying relational algebra to specific queries involving magazine and article relations.

Uploaded by

wajabaloch7860
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture#08

The document discusses various types of join operations in relational algebra, including Theta join, Equijoin, Natural join, and Outer joins (Left and Right). It explains the purpose of joins in relational databases, how they differ from Cartesian products, and provides examples for each type of join. Additionally, it includes practice exercises for applying relational algebra to specific queries involving magazine and article relations.

Uploaded by

wajabaloch7860
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture-8

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

• Various forms of join operation:


– Theta join
– Equijoin (a particular type of Theta join)
– Natural join
– Outer join
• Left Outer Join
• Right Outer Join
1.1 Theta join (-join)

• 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

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 210000 lecturer C3456 Database Systems F2345

F3456 Tahir CSE 230000 Asso Prof C4567 Financial

F4567 Ayesha ENG 270000 Asso Prof Management

C5678 Money & Capital F4567


F5678 Samad MNG 320000 Professor
Market

C3425 Introduction to F2345


Accounting

(σ rank = ‘Asso Prof’(FACULTY)) X COURSE


(σ rank = ‘Asso Prof’(FACULTY)) X COURSE

facId facName dept salary Rank crCode crTitle fId

F3456 Tahir CSE 230000 Asso Prof C3456 Database Systems F2345

F3456 Tahir CSE 230000 Asso Prof C4567 Financial


Management
F3456 Tahir CSE 230000 Asso Prof C5678 Money & Capital F4567
Market
F3456 Tahir CSE 230000 Asso Prof C3425 Introduction to F2345
Accounting

F4567 Ayesha ENG 270000 Asso Prof C3456 Database Systems F2345

F4567 Ayesha ENG 270000 Asso Prof C4567 Financial


Management
F4567 Ayesha ENG 270000 Asso Prof C5678 Money & Capital F4567
Market
F4567 Ayesha ENG 270000 Asso Prof C3425 Introduction to F2345
Accounting
1.2 Equijoin
• Equijoin is a special type of join in which we use only an
equality operator.
• Rows are joined on the basis of values of a common attribute
between the two relations
– It means that relations are joined on the basis of common
attributes between them; which are meaningful. This
means on the basis of primary key, which is a foreign key
in another relation. So, rows having the same value in the
common attributes are joined.
• It means that if primary and foreign keys of two relations are
having the same names and if we take the equijoin of both
then in the output relation the relation name will precede the
attribute name.
Equijoin Example (1)
FACULTY COURSE

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 210000 lecturer C3456 Database Systems F2345

F3456 Tahir CSE 230000 Asso Prof C4567 Financial


F4567 Ayesha ENG 270000 Asso Prof Management

C5678 Money & Capital F4567


F5678 Samad MNG 320000 Professor
Market

C3425 Introduction to F2345


Accounting

FACULTY FACULTY.facId = COURSE. facId (COURSE)


Equijoin Example (2)

FACULTY (COURSE)
FACULTY.facId = COURSE.facId

FACULTY. crCode crTitle COURSE.


facName dept salary Rank
facId facId
F2345 Usman CSE 210000 lecturer C3456 Database Systems F2345

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

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 210000 lecturer C3456 Database Systems F2345

F3456 Tahir CSE 230000 Asso Prof C4567 Financial


F4567 Ayesha ENG 270000 Asso Prof Management

C5678 Money & Capital F4567


F5678 Samad MNG 320000 Professor
Market

C3425 Introduction to F2345


Accounting

FACULTY (COURSE)
FACULTY.facId, COURSE.facId
Natural Join Example (2)

FACULTY (COURSE)
FACULTY.facId, COURSE.facId

facId facName dept salary Rank crCode crTitle

F2345 Usman CSE 210000 lecturer C3456 Database Systems

F4567 Ayesha ENG 270000 Asso Prof C5678 Money & Capital
Market

F2345 Usman CSE 210000 lecturer C3425 Introduction to


Accounting
1.4 Outer join
• Often in joining two relations, a tuple in one
relation does not have a matching tuple in the
other relation; in other words, there is no matching
value in the join attributes.
• We may want tuples from one of the relations to
appear in the result even when there are no
matching values in the other relation.
• This may be accomplished using the Outer join
which has been classified as Left Outer Join and
Right Outer Join
1.4.1 Left Outer Join

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

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 210000 lecturer C3456 Database Systems F2345

F3456 Tahir CSE 230000 Asso Prof C4567 Financial


F4567 Ayesha ENG 270000 Asso Prof Management

C5678 Money & Capital F4567


F5678 Samad MNG 320000 Professor
Market

C3425 Introduction to F2345


Accounting
Left Outer Join Example (2)

FACULTY LEFT JOIN COURSE

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 210000 lecturer C3456 Database Systems F2345

F2345 Usman CSE 210000 lecturer C3425 Intro. To Accounting

F3456 Tahir CSE 230000 Asso Prof

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

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 210000 lecturer C3456 Database Systems F2345

F3456 Tahir CSE 230000 Asso Prof C4567 Financial


F4567 Ayesha ENG 270000 Asso Prof Management

C5678 Money & Capital F4567


F5678 Samad MNG 320000 Professor
Market

C3425 Introduction to F2345


Accounting
Right Outer Join Example

FACULTY RIGHT OUTER JOIN COURSE

facId facName dept salary rank crCode crTitle facId

F2345 Usman CSE 210000 lecturer C3456 Database Systems F2345

C4567 Financial Management


Money & Capital
F4567 Ayesha ENG 270000 Asso Prof C5678 Market& Capital Market F4567
Money
.
F2345 Usman CSE 210000 lecturer C3425 Intro. To Accounting F2345
Practice Exercise (1)
Given the relational schema including the following tables
(primary keys are underlined):

MAGAZINE (MId, MName, Publisher)


ARTICLE (AId, Title, Topic, MId)
Express the following queries in relational algebra
Practice Exercise (2)
1. Produce a list for all magazines, showing only the Mname
and Publisher details.

(MAGAZINE)
 MName, Publisher
Practice Exercise (3)
2.Selects tuples from MAGAZINE relation where the Publisher
is ‘Thomas Connoly’ and magazine name is ‘Database Systems’.

Publisher = ‘Thomas Connoly’ and Mname = ‘Database Systems’ (MAGAZINE)


Practice Exercise (4)
3. Find the names of the magazines that have published at least
one article about motorcycles.

OR

 MID, MName (MAGAZINE) (  Topic = ‘Motorcycles’ (Articles)


Practice Exercise (5)
4. Find the names of the magazines that publish articles about
motorcycles or cars.

OR
 
MID, MName (MAGAZINE) Topic =‘Motorcycles’ or Topic=‘Cars’(Articles)

You might also like