6 Relational Algebra
6 Relational Algebra
RELATIONAL ALGEBRA
• Select operation extract tuples from a relation based on meeting a given condition.
• Represented using the sigma ( ) symbol
• (Relational algebraic expression whose result is a relation)
<selection condition>
Name Department Area Designation Gross Awards Dushmantha Electrical Software Lecturer 146,000 6
Salary
Dushmantha Electrical Software Lecturer 146,000 6 Banjan Mechanical Material Lecturer 146,000 3
Name Department Area Designation Gross Awards Name Department Area Designation Gross Awards
Salary Salary
Dushmantha Electrical Software Lecturer 146,000 6 Dushmantha Electrical Software Lecturer 146,000 6
PROJECT
Name Department Area Designation Gross Salary Awards
TEACHER
relation Dushmantha Electrical Software Lecturer 146,000 6
Meranda Electrical Communication Senior Lecturer 220,000 1
Sajitha Electrical Software Senior Lecturer 240,000 3
Kaman Electrical Communication Senior Lecturer 220,000 1
Banjan Mechanical Material Lecturer 146,000 3
Paridu Electrical Software Lecturer 146,000 1 π Name,Area (TEACHER)
Sithum Electrical Electronics Lecturer 146,000 0
• Project operation extracts specified attributes (columns) of all tuples from a relation
Name Area
• Represented using the pi (π) symbol
Dushmantha Software
• π<Set of Attributes> (Relational algebraic expression whose result is a relation)
Meranda Communication
• Ex: π Name,Area (TEACHER) Sajitha Software
Will retrieve all the tuples from TEACHER relation as a new relation with Kaman Communication
all tuples from the original relation, but having attributes Name and Area Banjan Material
only. Paridu Software
• Here, Degree = No. of Attributes of project operation Sithum Electronics
RENAME
TEACHER Name Department Area Designation Gross Salary Awards
relation Dushmantha Electrical Software Lecturer 146,000 6
Meranda Electrical Communication Senior Lecturer 220,000 1
Sajitha Electrical Software Senior Lecturer 240,000 3
Kaman Electrical Communication Senior Lecturer 220,000 1
Banjan Mechanical Material Lecturer 146,000 3
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0
NEW_RELATION relation
• Rename operation renames a relation or/and attributes of the relation.
• Represented using the rho (ρ) or arrow ( ) symbol
Teaching_area Title
• Ex: ρNEW_RELATION(Teachning_area,Title) ( π Area, Designation(TEACHER)
) Software Lecturer
Communication Senior Lecturer
(π Area, Designation(TEACHER))
• or NEW_RELATION(Teaching_area, Title) Software Senior Lecturer
Material Lecturer
Electronics Lecturer
CARTESIAN PRODUCT
TEACHER DEPARTMENT
Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Dushmantha Electrical Software Lecturer 146,000 6 ElE_1 Sajitha Sector B Yes
Meranda Electrical Communication Senior Lecturer 220,000 1 MME_1 Randitha Sector A Yes
(TEACHER) X (DEPARTMENT)
Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Meranda Electrical Communication Senior Lecturer 220,000 1 ElE_1 Sajitha Sector B Yes
Meranda Electrical Communication Senior Lecturer 220,000 1 MME_1 Randitha Sector A Yes
Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Sajitha Electrical Software Senior Lecturer 240,000 3 EIE_1 Sajitha Sector B Yes
R2 (TEACHER)
R1 Area=Communication (TEACHER)
Awards<3
• Inner UNION, INTERSECTION and MINUS are the basic set operations from the set theory applied in relational
algebra.
• These are binary operators meaning that only two relations are involved.
• Two relations must be type compatible which means they should have same degree (no. of attributes) and
domain of corresponding attributes must be same. Ex: R1 and R2 are type compatible
R1 Area=Communication(TEACHER)
INNER UNION R2 Awards<3(TEACHER)
Name Department Area Designation Gross Salary Awards Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1 Meranda Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1 Kaman Electrical Communication Senior Lecturer 220,000 1
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0
R1 U R2
Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0
• Inner UNION of type compatible relations A and B is the collection of all tuples that are either in A and B and in
both A and B
• Represent by U operator. Resulting relation does not have duplicate tuples
• Ex: R1 U R2
• Union operation is commutative.
INTERSECTION
R1 (TEACHER)
Area=Communication R2 Awards<3(TEACHER)
Name Department Area Designation Gross Salary Awards Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1 Meranda Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1 Kaman Electrical Communication Senior Lecturer 220,000 1
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0
R1 R2
Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1
• INTERSECTION of type compatible relations A and B is the collection of all tuples that are only in both A and B
• Represent by operator. Resulting relation does not have duplicate tuples
• Ex: R1 R2
• Intersection operation is commutative.
MINUS/SET DIFFERENCE
R1 (TEACHER)
Area=Communication R2 Awards<3(TEACHER)
Name Department Area Designation Gross Salary Awards Name Department Area Designation Gross Salary Awards
Meranda Electrical Communication Senior Lecturer 220,000 1 Meranda Electrical Communication Senior Lecturer 220,000 1
Kaman Electrical Communication Senior Lecturer 220,000 1 Kaman Electrical Communication Senior Lecturer 220,000 1
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0
R1 - R2
R2 - R1
Name Department Area Designation Gross Salary Awards Name Department Area Designation Gross Salary Awards
Paridu Electrical Software Lecturer 146,000 1
Sithum Electrical Electronics Lecturer 146,000 0
• For type compatible relations A and B, A MINUS B is the set of all tuples in A that are not in B.
• Represent by - operator.
• Set difference is not commutative.
• Ex: (R1 – R2) != (R2 – R1)
DIVISION
R1 (Z)
Name (Y) Module (X)
R2 (Y)
Meranda EE6201 Name (Y)
Kaman EE6201 Meranda
Dushmantha EE4202 Kaman
Sithum EE3301
Meranda EE4201
Dushmantha EE6206 R (X) = R1 (Z) ÷ R2 (Y)
Kaman EE2301
Sajitha EE4301
Module (X)
Meranda EE8201
Sithum EE4102 EE6201
Kaman EE8201
EE8201
• If R1 (Z) and R2 (Y) are relations with attributes Z and Y such that Z = (X U Y), then R1 (Z) ÷ R2 (Y) produces
relation R (X) which contains all tuples t (X) that appear in R1 (Z) in combination with every tuple of R2 (Y).
• Represent by ÷ operator.
• Not commutative
• Ex: R (X) = R1 (Z) ÷ R2 (Y)
RELATIONAL ALGEBRA - Extensions
TEACHER
OUTER JOIN DEPARTMENT
Name Department Area Designation Gross Salary Awards Dept_ID Head_name Location Accreditation
Dushmantha Electrical Software Lecturer 146,000 6 ElE_1 Sajitha Sector B Yes
Meranda Electrical Communication Senior Lecturer 220,000 1 MME_1 Randitha Sector A Yes
Sajitha Electrical Software Senior Lecturer 240,000 3 CEE_1 Layanthi Sector C Yes
Natural Join and Ɵ join are all inner joins which only joins tuples with matching condition.
In outer join, tuples from one or both relations appear in the result which do not meet the join condition.
Outer join is named according to the relation in which all tuples are kept.
Left outer join keeps all tuples of the left/first relation regardless of join condition. Unmatched tuples in the second/right
relation is padded with nulls. Same is true for right outer join
In full outer join, all tuples from left and right relations are kept. If no matching condition is found, the corresponding
entries will be padded with null values.
OUTER JOIN
TEACHER(R1) MANAGER(R2)
Name Department Area Designation Name Department Head_ID Experience
Dushmantha Electrical Software Lecturer Sajitha Electrical H005 16
R1 outer union R2
Meranda Electrical Communication Senior Lecturer Ranjitha Mechanical H002 18
Sajitha Electrical Software Senior Lecturer Layanthi Civil H003 13
• Inner UNION is defined for type compatible relations while outer UNION is defined for relations consisting of
some common attributes but not type compatible. i.e. partially compatible
• Two tuples in partially type compatible relations are said to match if their common attributes are equal and will
have values for all its attributes.
• In outer union result, matching tuples appear only once.