0% found this document useful (0 votes)
82 views48 pages

DBMS Relational Algebra

The document discusses relational algebra operations that can be performed on relations in a database. It describes select, project, cartesian product, and natural join operations using sample relations from a college admissions database. Examples show how each operation can be used to filter and combine information from the relations.

Uploaded by

Arun Kumar Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views48 pages

DBMS Relational Algebra

The document discusses relational algebra operations that can be performed on relations in a database. It describes select, project, cartesian product, and natural join operations using sample relations from a college admissions database. Examples show how each operation can be used to filter and combine information from the relations.

Uploaded by

Arun Kumar Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Module -1

Relational Algebra
• Query (expression) on set of relation will produce
relation as a result.
• Examples: simple college admissions
database
• College(cName,state,enrollment)
• Student(sID,sName,GPA,sizeHS)
• Apply(sID,cName,major,decision)

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 9.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 500 1 William CS N
UoI IL 400 4 Jack 5.0 100 3 MIT CS Y
Simplest query: relation name
Use operators to filter, slice, combine

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 9.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 500 1 William CS N
UoI IL 400 4 Jack 5.0 100 2 William CS Y
Select operator: picks certain rows
Students with GPA>3.7

Students with GPA>3.7 and HS<1000

Applications to Stanford CS major

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Project operator: picks certain columns
ID and decision of all applications

sID dec
3 Y
4 N
1 N
2 Y

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
sID sName GPA HS
To pick both rows and columns… sID sName
1 Victor 8.6 700
1 Victor
ID and name of students with GPA>3.7 2 Mike 2.2 200
3 William
3 William 7.3 1500
4 Jack
4 Jack 5.0 700

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Duplicates Major dec
List of application majors and decisions CS Y
EE N
CS N

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Cross-product: combine two relations
(a.k.a. Cartesian product)

Student.sID sName GPA HS Apply.sID cName Major Dec


1 Victor 8.6 700 3 Stanford CS Y
1 Victor 8.6 700 4 MIT EE N
1 Victor 8.6 700 1 William CS N
1 Victor 8.6 700 2 William CS Y

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 3 Stanford CS Y
1 Victor 8.6 700 4 MIT EE N
1 Victor 8.6 700 1 William CS N
1 Victor 8.6 700 2 William CS Y
2 Mike 2.2 200 3 Stanford CS Y
2 Mike 2.2 200 4 MIT EE N
2 Mike 2.2 200 1 William CS N
2 Mike 2.2 200 2 William CS Y
3 William 7.3 1500 3 Stanford CS Y
3 William 7.3 1500 4 MIT EE N
3 William 7.3 1500 1 William CS N
3 William 7.3 1500 2 William CS Y
4 Jack 5.0 700 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
4 Jack 5.0 700 1 William CS N
4 Jack 5.0 700 2 William CS Y
Cross-product: combine two relations
(a.k.a. Cartesian product)
Names and GPAs of students with HS>1000 who applied to CS
and were rejected

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
Cartesian product
Names and GPAs of students with HS>1000 who applied to CS
and were rejected

Student.sID sName GPA HS Apply.sID cName Major Dec


1 Victor 8.6 700 1 William CS N
2 Mike 2.2 200 2 William CS Y
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
Natural Join
 Enforce equality on all attributes with same name
 Eliminate one copy of duplicate attributes
sID sName GPA HS cName Major Dec
1 Victor 8.6 700 William CS N
2 Mike 2.2 200 William CS Y
3 William 7.3 1500 Stanford CS Y
4 Jack 5.0 700 MIT EE N
Got Mergred No duplicate columns
as in Cartesian product

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Natural Join
Names and GPAs of students with HS>1000 who applied to CS
and were rejected

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Natural Join
Names and GPAs of students with HS>1000 who applied to CS
at
andcollege with enr>20,000 and were rejected
were rejected

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Natural Join

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Theta Join

 Basic operation implemented in DBMS


 Term “join” often means theta join

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Theta Join
Names and GPAs of students with HS>1000 who applied to CS
and were rejected
𝜋𝑠𝑁𝑎𝑚𝑒,𝐺𝑃𝐴
(Student ⋈𝐻𝑆>1000 ∧ 𝑀𝑎𝑗𝑜𝑟=′ 𝐶𝑆 ′ ∧ 𝑑𝑒𝑐=′ 𝑁′ ^ 𝑆𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦)
Student.sID
Student.sID sName
sName GPA
GPA HS
HS Apply.sID
Apply.sID cName
cName Major
Major Dec
Dec
11
2 Victor
Victor
Mike 8.6
8.6
2.2 700
700
1200 11
2 William
William CS
CS N
YN
22 Mike
Mike 2.2
2.2 1200
200 22 William
William CS
CS YY
33 William
William 7.3
7.3 1500
1500 33 Stanford
Stanford CS
CS YY
44 Jack
Jack 5.0
5.0 700
700 44 MIT
MIT EE
EE N
N

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 1200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
EquiJoin
 Join where only comparison operator used is =
• The result of equijoin always have one or more pair of
attributes ( whose names need not be identical ) that have
identical tuples
• Special case: condition c contains only conjunction of
equalities

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
EquiJoin
Names and GPAs of students who applied to CS and were rejected
𝜋𝑠𝑁𝑎𝑚𝑒,𝐺𝑃𝐴
(Student ⋈𝑀𝑎𝑗𝑜𝑟=′ 𝐶𝑆 ′ ∧ 𝑆𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 ∧ 𝑑𝑒𝑐=′ 𝑁′ 𝐴𝑝𝑝𝑙𝑦)
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 1 William CS N
2 Mike 2.2 1200 2 William CS Y
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Join
Natural Join (𝑹 ⋈ 𝑺)
 Apply on same name attribute
 Same name attribute get merged
 Condition called implicitly of equality of same name
attribute ( need not to write in query)
Theta Join (𝑹 ⋈𝜽 𝑺)
 Here 𝜽 is condition on which join will take place
 Condition will be made up of {<,>,≥,≤,≠,=} and Boolean
operators {AND, OR, NOT}
 No same name attribute merging takes place
Equijoin (𝐑 ⋈<𝒄𝒐𝒏𝒅 𝒐𝒇 𝒆𝒒𝒖𝒂𝒍𝒊𝒕𝒚> 𝑺)
 Special case of theta join
 Condition is made up of {=,AND,OR,NOT} i.e. = will be
only comparative operator
 No attribute merging of same name takes place
Query (expression) on set of relations produces
relation as a result
 Simplest query: relation name
 Use operators to filter, slice, combine
 Operators so far: select, project, cross-product,
natural join, theta join
Union operator cName
William
List of college and student names No
Duplicates Stanford
MIT
UoI
Victor
Mike
Jack
𝜋𝑐𝑁𝑎𝑚𝑒 𝐶𝑜𝑙𝑙𝑒𝑔𝑒 ∪ 𝜋𝑠𝑁𝑎𝑚𝑒 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Difference operator
IDs of students who didn’t apply anywhere

sID
2

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
Difference operator
IDs and names of
of students students
who who didn’t
didn’t apply apply anywhere
anywhere

𝜋𝑠𝐼𝐷,𝑠𝑁𝑎𝑚𝑒 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 − 𝜋𝑠𝐼𝐷 (𝐴𝑝𝑝𝑙𝑦)


Not Union
Compatible
𝜋𝑠𝑁𝑎𝑚𝑒 𝜋𝑠𝐼𝐷 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 − 𝜋𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦 ⋈ 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
sID sName
2 Mike
College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
Intersection operator
Names that are both a college name and a student name

cName
William

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Intersection doesn’t add expressive power (1)

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Intersection doesn’t add expressive power (2)

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Rename operator
1.

2.

3.

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Rename operator C
To unify schemas for set operators
List of college and student names William
Stanford
MIT
UoI
Victor
Mike
Jack
College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Rename operator
For disambiguation in “self-joins”
Pairs of colleges in same state

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Division operator
For query that need information of ‘all’
List of names of college where all student who apply in CS
major have applied

𝜋𝑠𝐼𝐷,𝑐𝑁𝑎𝑚𝑒 𝐴𝑝𝑝𝑙𝑦 ÷ π𝑠𝐼𝐷 (𝜎𝑀𝑎𝑗𝑜𝑟=′ 𝐶𝑆 ′ 𝐴𝑝𝑝𝑙𝑦)

Apply

sID cName Major dec


College Student
3 Stanford CS Y
cName state enr sID sName GPA HS 4 MIT EE N

William CA 500 1 Victor 8.6 700 1 William CS N

Stanford CA 400 2 Mike 2.2 200 2 William CS Y

MIT MA 300 3 William 7.3 1500 2 Stanford CS N

UoI IL 400 4 Jack 5.0 700 1 Stanford CS Y


Division operator
List of colleges where all student who apply in CS major have
applied
𝜋𝑠𝐼𝐷,𝑐𝑁𝑎𝑚𝑒 𝐴𝑝𝑝𝑙𝑦 ÷ 𝜋𝑠𝐼𝐷 (𝜎𝑀𝑎𝑗𝑜𝑟=′ 𝐶𝑆 ′ 𝐴𝑝𝑝𝑙𝑦)

3 Stanford 1 1
4 MIT 2 2
1 William 3 3
Apply
2 William
2 Stanford sID cName Major dec
College 1 Student
Stanford
3 Stanford CS Y
cName state enr sID sName GPA HS sID
4 cName
MIT Major
EE dec
N

William CA 500 1 Victor 8.6 700 1


3 William
Stanford CS N
Y
Stanford CA 400 2 Mike 2.2 200 2
4 William
MIT CS
EE Y
N
MIT MA 300 3 William 7.3 1500 2
1 Stanford
William CS N
UoI IL 400 4 Jack 5.0 700 1
2 Stanford
William CS Y
Aggregate functions and Grouping
For summarizing information from the database
tuple
Total number of students
𝔉𝐶𝑂𝑈𝑁𝑇𝑠𝐼𝐷 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
4

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database tuple
Other Aggregate Functions
• Sum – find total of a particular attribute like total HSsize
• Count – Count will count no. of tuples like no. of Student
• Maximum - find the maximum among the particular set like
maximum GPA
• Minimum – find the minimum like minimum GPA
• Average – find average among a set like average GPA

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database tuple
Resulting table will have column name
<function>_<attribute>
Eg. COUNT_sID, AVERAGE_GPA
Duplicates are not eliminated when aggregate function is
applied

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database
Find Total High School size
𝔉 𝑇𝑂𝑇𝐴𝐿 𝐻𝑆 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 3100
Find Maximum, Minimum & Average GPA of students
𝔉𝑀𝐴𝑋𝐼𝑀𝑈𝑀𝐺𝑃𝐴 ,𝑀𝐼𝑁𝐼𝑀𝑈𝑀𝐺𝑃𝐴 ,𝐴𝑉𝐸𝑅𝐴𝐺𝐸𝐺𝑃𝐴 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
MAXIMUM_GPA MINIMUM_GPA AVERAGE_GPA
8.6 2.2 5.8

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database
Grouping will group tuples by value of some
attributes
And then applying aggregate function independently
to each group

𝑎<𝑔𝑟𝑜𝑢𝑝𝑖𝑛𝑔 𝑎𝑡𝑡𝑟𝑖𝑏𝑢𝑡𝑒> 𝔉<𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑙𝑖𝑠𝑡> 𝑅𝑒𝑙𝑎𝑡𝑖𝑜𝑛


College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database
Find number of colleges in each state and their average
enrollment
𝑎𝑠𝑡𝑎𝑡𝑒 𝔉𝐶𝑂𝑈𝑁𝑇𝑐𝑁𝑎𝑚𝑒,𝐴𝑉𝐸𝑅𝐴𝐺𝐸𝑒𝑛𝑟 College
state COUNT_cName AVERAGE_enr
CA 2 450
MA 1 300
IL 1 400
College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Aggregate functions and Grouping
For summarizing information from the database
Find number of apply in colleges at California
𝑎𝑠𝑡𝑎𝑡𝑒 𝔉𝐶𝑂𝑈𝑁𝑇𝑠𝐼𝐷 𝐶𝑜𝑙𝑙𝑒𝑔𝑒 ⋈ 𝐴𝑝𝑝𝑙𝑦
𝜎𝑠𝑡𝑎𝑡𝑒=′ 𝐶𝐴′ (𝑠𝑡𝑎𝑡𝑒 𝔉𝐶𝑂𝑈𝑁𝑇𝑠𝐼𝐷 (𝐶𝑜𝑙𝑙𝑒𝑔𝑒 ⋈ 𝐴𝑝𝑝𝑙𝑦))
state COUNT_sID
state COUNT_sID
CA 3
CA 3
MA 1
IL 0

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700 2 William CS Y
Outer Join
For including tuples that not satisfying the join condition
List all students and mention College if they apply in a college
𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ℎ𝑠𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 1 William CS N
2 Mike 2.2 200 NULL NULL NULL NULL
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N

College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
Left Outer Join: For including tuples of LEFT RELATION that
not satisfying the join condition
List all students and mention College if they apply in a college
𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ℎ𝑠𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 1 William CS N
2 Mike 2.2 200 NULL NULL NULL NULL
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
4 Jack 5.0 700 4 Stanford CS Y
College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 4 Stanford CS Y
UoI IL 400 4 Jack 5.0 700 1 William CS N
Right Outer Join: For including tuples of Right RELATION that
not satisfying the join condition
List all College and student details who have applied there
𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ℎ𝑠𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦
Student.sID sName GPA HS Apply.sID cName Major Dec
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
1 Victor 8.6 700 1 William CS N
NULL NULL NULL NULL 5 UoI MBA Y
3 William 7.3 1500 3 MIT CS Y
Apply
College Student
sID cName Major dec
cName state enr sID sName GPA HS
3 Stanford CS Y
William CA 500 1 Victor 8.6 700 4 MIT EE N
Stanford CA 400 2 Mike 2.2 200 1 William CS N
MIT MA 300 3 William 7.3 1500 5 UoI MBA Y
UoI IL 400 4 Jack 5.0 700 3 MIT CS Y
Full Outer Join: For including tuples of RELATIONs on both
side of join that not satisfying the join condition
List all students and mention College if they apply in a college
𝑆𝑡𝑢𝑑𝑒𝑛𝑡 ℎ𝑠𝑡𝑢𝑑𝑒𝑛𝑡.𝑠𝐼𝐷=𝐴𝑝𝑝𝑙𝑦.𝑠𝐼𝐷 𝐴𝑝𝑝𝑙𝑦
Student.sID sName GPA HS Apply.sID cName Major Dec
1 Victor 8.6 700 1 William CS N
2 Mike 2.2 200 NULL NULL NULL NULL
3 William 7.3 1500 3 Stanford CS Y
4 Jack 5.0 700 4 MIT EE N
NULL NULL NULL NULL 5 UoI MBA Y
College Student Apply

cName state enr sID sName GPA HS sID cName Major dec
William CA 500 1 Victor 8.6 700 3 Stanford CS Y
Stanford CA 400 2 Mike 2.2 200 4 MIT EE N
MIT MA 300 3 William 7.3 1500 1 William CS N
UoI IL 400 4 Jack 5.0 700
5 UoI MBA Y
Outer Join: For including tuples of RELATIONs that not
satisfying the join condition

Left Outer Join: include every tuple of first or Left


relation R in R S ; if no matching tuple is found in
S, then attribute of S in the join result are filled or
‘padded’ with null values.

Right Outer Join: keeps every tuple in second or right


relation S in the result of R S

Full Outer Join: keeps all tuples of left and right


relations when no matching tuples are found,
padding them with null values as needed R S
Left Outer Join
PEOPLE people.food = menu.food MENU

Name Age people.Food menu.Food Day


Alice 21 Hamburger Hamburger Tuesday
Bill 24 Pizza Pizza Monday
Carl 23 Beer NULL NULL
Dina 19 Shrimp NULL NULL

PEOPLE: MENU:
Name Age Food Food Day
Alice 21 Hamburger Pizza Monday
Bill 24 Pizza Hamburger Tuesday
Chicken Wednesday
Carl 23 Beer
Pasta Thursday
Dina 19 Shrimp
Tacos Friday
Right Outer Join
PEOPLE people.food = menu.food MENU

Name Age people.Food menu.Food Day


Bill 24 Pizza Pizza Monday
Alice 21 Hamburger Hamburger Tuesday
NULL NULL NULL Chicken Wednesday
NULL NULL NULL Pasta Thursday
NULL NULL NULL Tacos Friday
PEOPLE: MENU:
Name Age Food Food Day
Alice 21 Hamburger Pizza Monday
Bill 24 Pizza Hamburger Tuesday
Chicken Wednesday
Carl 23 Beer
Pasta Thursday
Dina 19 Shrimp
Tacos Friday
Full Outer Join
PEOPLE people.food = menu.food MENU

Name Age people.Food menu.Food Day


Alice 21 Hamburger Hamburger Tuesday
Bill 24 Pizza Pizza Monday
Carl 23 Beer NULL NULL
Dina 19 Shrimp NULL NULL
NULL NULL NULL Chicken Wednesday
NULL NULL NULL Pasta Thursday
NULL NULL NULL Tacos Friday
PEOPLE: MENU:
Name Age Food Food Day
Alice 21 Hamburger Pizza Monday
Bill 24 Pizza Hamburger Tuesday
Chicken Wednesday
Carl 23 Beer
Pasta Thursday
Dina 19 Shrimp
Tacos Friday

You might also like