Database Systems
Lec# 13 &14
Topics
Relational Algebra:
• Join
Join
• Join involving cross product, selection, and (sometimes) projection.
• A special form of cross product of two tables
• Types of Join
1. Theta Join
2. Equi Join
3. Natural Join
4. Semi Join
5. Inner Join
6. Outer Join
Theta Join
Apply the condition through select on one relation and then only
those rows are used in the cross product with the second relation
R1 R2

rank
salary
dept
facName
facId
F5678
F4567
F3456
F2345
32000
27000
23000
21000
Professor
MNG
Samad
Asso Prof
ENG
Ayesha
Asso Prof
CSE
Tahir
lecturer
CSE
Usman
COURSE
F2345
Introduction to
Accounting
C3425
F4567
Money & Capital
Market
C5678
Financial
Management
C4567
F2345
Database Systems
C3456
fId
crTitle
crCode
FACULTY
Theta Join Example
(σ rank = ‘Asso Prof’(FACULTY)) X COURSE
F2345
© Virtual University of Pakistan
Asso Prof
27000
ENG
Ayesha
F4567
Asso Prof
27000
ENG
Ayesha
F4567
Asso Prof
27000
ENG
Ayesha
F4567
Asso Prof
27000
ENG
Ayesha
F4567
Asso Prof
23000
CSE
Tahir
F3456
Asso Prof
23000
CSE
Tahir
F3456
Asso Prof
23000
CSE
Tahir
F3456
Asso Prof
23000
CSE
Tahir
F3456
Rank
salary
dept
facName
facId
F2345
Introduction to
Accounting
C3425
F2345
Database Systems
C3456
Financial
Management
C4567
F4567
Money & Capital
Market
C5678
F2345
Introduction to
Accounting
C3425
F4567
Money & Capital
Market
C5678
Financial
Management
C4567
F2345
Database Systems
C3456
fId
crTitle
crCode
(σ rank = ‘Asso Prof’(FACULTY)) X COURSE
Equi Join
• Rows are joined on the basis of values of a common attribute between the
two relations
• Rows having the same value in the common attribute are joined.
• Common attributes appears twice in the output
• Common attribute with the same name is qualified with the relation name
in the output
rank
salary
dept
facName
facId
F5678
F4567
F3456
F2345
32000
27000
23000
21000
Professor
MNG
Samad
Asso Prof
ENG
Ayesha
Asso Prof
CSE
Tahir
lecturer
CSE
Usman
COURSE
F2345
Introduction to
Accounting
C3425
F4567
Money & Capital
Market
C5678
F2345
Database Systems
C3456
fId
crTitle
crCode
FACULTY
Equijoin Example
FACULTY FACULTY.facId = COURSE.fId COURSE
lecturer
21000
CSE
Usman
F2345
Asso Prof
27000
ENG
Ayesha
F4567
lecturer
21000
CSE
Usman
F2345
Rank
salary
dept
facName
facId
F2345
Introduction to
Accounting
C3425
F4567
Money & Capital
Market
C5678
F2345
Database Systems
C3456
fId
crTitle
crCode
Equijoin Example
FACULTY FACULTY.facId = COURSE.fId COURSE
Natural Join
• Also called simply the join, most general form of join
• Same as equijoin with common column appearing once
Natural Join Example
FACULTY FACULTY.facId, COURSE.facId COURSE
rank
salary
dept
facName
facId
F5678
F4567
F3456
F2345
32000
27000
23000
21000
Professor
MNG
Samad
Asso Prof
ENG
Ayesha
Asso Prof
CSE
Tahir
lecturer
CSE
Usman
COURSE
F2345
Introduction to
Accounting
C3425
F4567
Money & Capital
Market
C5678
Financial
Management
C4567
F2345
Database Systems
C3456
facId
crTitle
crCode
FACULTY
lecturer
21000
CSE
Usman
F2345
Asso Prof
27000
ENG
Ayesha
F4567
lecturer
21000
CSE
Usman
F2345
Rank
salary
dept
facName
facId
Introduction to
Accounting
C3425
Money & Capital
Market
C5678
Database Systems
C3456
crTitle
crCode
Natural Join Example
FACULTY FACULTY.facId, COURSE.facId COURSE
Semijoin
• First take the natural join of two tables
• Then take the projection on the attributes of first table
FACULTY COURSE
Semijoin Example
FACULTY COURSE
rank
salary
dept
facName
facId
F4567
F2345
27000
21000
Asso Prof
ENG
Ayesha
lecturer
CSE
Usman
Inner Join
• An inner join includes only those tuples with matching attributes
and the rest are discarded in the resulting relation.
• Theta Join, Equijoin, and Natural Join are called inner joins
Outer Join
• All tuples in the left relation and right relation are
output.
• Denoted by ⟗
• types of outer join depends one the data kept in the
relations
• Left Outer Join
• Right Outer Join
rank
salary
dept
facName
facId
F5678
F4567
F3456
F2345
32000
27000
23000
21000
Professor
MNG
Samad
Asso Prof
ENG
Ayesha
Asso Prof
CSE
Tahir
lecturer
CSE
Usman
COURSE
F2345
Introduction to
Accounting
C3425
F4567
Money & Capital
Market
C5678
Financial
Management
C4567
F2345
Database Systems
C3456
fId
crTitle
crCode
FACULTY
F2345
Outer Join Example
F5678 32000 Professor
MNG
Samad
Financial Management
C4567
Money & Capital Market
.
Money & Capital
Market
rank
salary
dept
facName
facId
F3456
F4567
F2345
F2345
23000
27000
21000
21000
Asso Prof
CSE
Tahir
Asso Prof
ENG
Ayesha
lecturer
CSE
Usman
lecturer
CSE
Usman
F4567
C5678
F2345
Database Systems
C3456
facId
crTitle
crCode
Intro. To Accounting
C3425
F2345
FACULTY OUTER JOIN COURSE
• Keep all of the tuples from the “left” relation
• Join with the right relation
• Pad the non-matching tuples with nulls
• Denoted by
Left Outer Join
Left Outer Join Example
FACULTY LEFT JOIN COURSE
rank
salary
dept
facName
facId
F4567
F3456
F2345
F2345
27000
23000
21000
21000
Asso Prof
ENG
Ayesha
Asso Prof
CSE
Tahir
lecturer
CSE
Usman
lecturer
CSE
Usman
Money & Capital Market
. To Accounting
C5678
Intro. To Accounting
C3425
F2345
Database Systems
C3456
facId
crTitle
crCode
F5678 32000 Professor
MNG
Samad
F4567
Right Outer Join
• Same as the left, but keep tuples from the “right” relation
• Denoted by
Right Outer Join Example
F2345
FACULTY RIGHT OUTER JOIN COURSE
Money & Capital Market
.
Money & Capital
Market
rank
salary
dept
facName
facId
F2345
F4567
F2345
21000
27000
21000
lecturer
CSE
Usman
Asso Prof
ENG
Ayesha
lecturer
CSE
Usman
F2345
F4567
C5678
Database Systems
C3456
facId
crTitle
crCode
Intro. To Accounting
C3425
Financial Management
C4567

IT-243-L13-14-2. pdf