SlideShare a Scribd company logo
1 of 34
Relational Algebra 
• The basic set of operations for the relational 
model is the relational algebra. 
– enable the specification of basic retrievals 
• The result of a retrieval is a new relation, which 
may have been formed from one or more 
relations. 
– algebra operations thus produce new relations, 
which can be further manipulated the same algebra. 
• A sequence of relational algebra operations 
forms a relational algebra expression, 
– the result will also be a relation that represents the 
result of a database query (or retrieval request). 
2
3 
What is an Algebra? 
• A language based on operators and a 
domain of values 
• Operators map values taken from the 
domain into other domain values 
• Hence, an expression involving operators 
and arguments produces a value in the 
domain 
• When the domain is a set of all relations we 
get the rreellaattiioonnaall aallggeebbrraa
Relational Algebra Definitions 
• Domain: set of relations 
• Basic operators: sseelleecctt, pprroojjeecctt, uunniioonn, sseett 
ddiiffffeerreennccee, CCaarrtteessiiaann (cross) pprroodduucctt 
• Derived operators: sseett iinntteerrsseeccttiioonn, ddiivviissiioonn, 
jjooiinn 
• Procedural: Relational expression specifies 
query by describing an algorithm (the sequence 
in which operators are applied) for determining 
the result of an expression 
4
Unary Relational Operations 
• SELECT Operation: used to select a subset 
of the tuples from a relation that satisfy a selection 
condition. It is a filter that keeps only those tuples 
that satisfy a qualifying condition. 
Examples: 
	 	 	 sDNO = 4 (EMPLOYEE) 
sSALARY > 30,000 (EMPLOYEE) 
– denoted by s <selection condition>(R) where the symbol s (sigma) is 
used to denote the select operator, and the selection 
condition is a Boolean expression specified on the 
attributes of relation R 
5
SELECT Operation Properties 
The SELECT operation s <selection condition>(R) produces a relation S 
that has the same schema as R 
The SELECT operation s is commutative; i.e., 
6 
  s <condition1>(s < condition2> ( R)) = s <condition2> (s < condition1> ( R)) 
A cascaded SELECT operation may be applied in any 
order; i.e., 
  s <condition1>(s < condition2> (s <condition3> ( R)) 
  = s <condition2> (s < condition3> (s < condition1> ( R))) 
A cascaded SELECT operation may be replaced by a single 
selection with a conjunction of all the conditions; i.e., 
  s <condition1>(s < condition2> (s <condition3> ( R)) 
  = s <condition1> AND < condition2> AND < condition3> ( R)))
Selection Condition 
7 
• Operators: <, £, ³, >, =, ¹ 
• Simple selection condition: 
– <attribute> operator <constant> 
– <attribute> operator <attribute> 
– <condition> AND <condition> 
– <condition> OR <condition> 
– NOT <condition>
Id Name Address Hobby 
8 
Select Examples 
PPeerrssoonn 
1123 John 123 Main stamps 
1123 John 123 Main coins 
5556 Mary 7 Lake Dr hiking 
9876 Bart 5 Pine St stamps 
s Id>3000 OR Hobby=‘hiking’ (PPeerrssoonn) 
s Id>3000 AND Id <3999 (PPeerrssoonn) 
s NOT(Hobby=‘hiking’) (PPeerrssoonn) 
s Hobby¹‘hiking’ (PPeerrssoonn)
Unary Relational Operations (cont.) 
• PROJECT Operation: selects certain columns 
from the table and discards the others. 
Example:   
	 	 	pLNAME, FNAME,SALARY(EMPLOYEE) 
The general form of the project operation is: 
p<attribute list>(R) where p is the symbol used 
to represent the project operation and <attribute 
list> is the desired list of attributes. 
PROJECT removes duplicate tuples, so the result 
is a set of tuples and hence a valid relation. 
9
PROJECT Operation Properties 
The number of tuples in the result of p <list> (R) is 
always less or equal to the number of tuples in R. 
If attribute list includes a key of R, then the number of 
tuples is equal to the number of tuples in R. 
p <list1> (p <list2> (R) ) = p <list1> (R) as long 
as <list2> contains the attributes in <list1>  
10
SELECT and PROJECT Operations 
(a) s(DNO=4 AND SALARY>25000) OR (DNO=5 AND 
SALARY>30000)(EMPLOYEE) 
(b) pLNAME, FNAME, SALARY(EMPLOYEE) 
(c) pSEX, SALARY(EMPLOYEE)
Relational Algebra Operations 
from Set Theory 
• The UNION, INTERSECTION, and 
MINUS Operations 
• The CARTESIAN PRODUCT (or 
CROSS PRODUCT) Operation
13 
Set Operators 
• A relation is a set of tuples, so set 
operations apply: 
	 	 	 Ç, È, - (set difference) 
• Result of combining two relations with a 
set operator is a relation => all elements 
are tuples with the same structure
14 
UNION Operation 
Denoted by R È S 
Result is a relation that includes all tuples that are either 
in R or in S or in both. Duplicate tuples are eliminated. 
Example: Retrieve the SSNs of all employees who either 
work in department 5 or directly supervise an employee 
who works in department 5: 
DEP5_EMPS ¬ sDNO=5 (EMPLOYEE) 
RESULT1 ¬ p SSN(DEP5_EMPS) 
RESULT2(SSN) ¬ p SUPERSSN(DEP5_EMPS) 
RESULT ¬ RESULT1 È RESULT2 
The union operation produces the tuples that are in either 
RESULT1 or RESULT2 or both. The two operands must 
be “type compatible”.
UNION Operation 
Type (Union) Compatibility 
The operand relations R1(A1, A2, ..., An) and R2(B1, 
B2, ..., Bn) must have the same number of 
attributes, and the domains of corresponding 
attributes must be compatible, i.e. 
15 
– dom(Ai) = dom(Bi) for i=1, 2, ..., n.
16 
Example 
Tables: 
PPeerrssoonn (SSN, Name, Address, Hobby) 
PPrrooffeessssoorr (Id, Name, Office, Phone) 
are not union compatible. 
But 
p Name (PPeerrssoonn) and p Name (PPrrooffeessssoorr) 
are union compatible so 
p Name (PPeerrssoonn) - p Name (PPrrooffeessssoorr) 
makes sense.
17 
UNION Example 
STUDENT È INSTRUCTOR: 
What would STUDENT Ç INSTRUCTOR be?
Set Difference Operation 
Set Difference (or MINUS) Operation 
The result of this operation, denoted by R - S, is a 
relation that includes all tuples that are in R but 
not in S. The two operands must be "type 
compatible”. 
18
Set Difference Example 
S1 S2 
SID SName Age 
202 Rusty 21 
403 Marcia 20 
914 Hal 24 
192 Jose 22 
881 Stimpy 19 
19 
SID SName Age 
473 Popeye 22 
192 Jose 22 
715 Alicia 28 
914 Hal 24
Relational Algebra Operations From 
20 
Set Theory (cont.) 
• Union and intersection are commutative operations: 
R È S = S È R, and R Ç S = S Ç R 
• Both union and intersection can be treated as n-ary 
operations applicable to any number of relations as both 
are associative operations; that is 
R È (S È T) = (R È S) È T, and 
(R Ç S) Ç T = R Ç (S Ç T) 
• The minus operation is not commutative; that is, in general 
R - S ≠ S – R
Cartesian (Cross) Product 
• If RR and SS are two relations, RR ´ SS is the set of all 
concatenated tuples <x,y>, where x is a tuple in RR and y 
is a tuple in SS 
21 
– RR and SS need not be union compatible 
• RR ´ SS is expensive to compute: 
– Factor of two in the size of each row; Quadratic in the number 
of rows 
A B C D A B C D 
x1 x2 y1 y2 x1 x2 y1 y2 
x3 x4 y3 y4 x1 x2 y3 y4 
x3 x4 y1 y2 
RR SS x3 x4 y3 y4 
RR´ SS
Cartesian Product Example 
• We want a list of COMPANY’s female 
employees dependents.
Binary Relational Operations: 
JOIN and DIVISION 
• The JOIN Operation 
• The EQUIJOIN and NATURAL JOIN 
variations of JOIN 
• The DIVISION Operation
25 
JOIN Operation 
• Cartesian product followed by select is commonly 
used to identify and select related tuples from two 
relations => called JOIN. It is denoted by a 
– This operation is important for any relational database 
with more than a single relation, because it allows us to 
process relationships among relations. 
– The general form of a join operation on two relations 
R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is: 
R <join condition>S 
where R and S can be any relations that result from 
general relational algebra expressions.
The Binary Join Operation 
DEPT_MGR ¬ 
DEPARTMENT MGRSSN=SSN EMPLOYEE
EQUIJOIN & NATURAL JOIN 
27 
• EQUIJOIN 
– most common join: join conditions with equality 
comparisons only. 
– in the result of an EQUIJOIN we always have one or 
more pairs of attributes (whose names need not be 
identical) that have identical values in every tuple. 
– The JOIN in the previous example was EQUIJOIN. 
• NATURAL JOIN 
– Because one of each pair of attributes with identical 
values is superfluous, a new operation called natural 
join—denoted by *—was created to get rid of the 
second (superfluous) attribute. 
– The standard definition of natural join requires that each 
pair of corresponding join attributes, have the same 
name in both relations. If this is not the case, a 
renaming operation is applied.
Natural Join Operations 
(a) PROJ_DEPT ¬ PROJECT * DEPT 
(b) DEPT_LOCS ¬ DEPARTMENT * 
DEPT_LOCATIONS
The DIVISION Operation 
(a) Dividing SSN_PNOS by SMITH_PNOS. 
(b) T ¬ R ÷ S.
Additional Relational 
Operations 
• Aggregate Functions and Grouping 
• Recursive Closure Operations 
• The OUTER JOIN Operation
Aggregate Functions 
E.g. SUM, AVERAGE, MAX, MIN, COUNT
Recursive Closure Example
33 
OUTER JOINs 
• In NATURAL JOIN tuples without a matching (or related) tuple 
are eliminated from the join result. Tuples with null in the join 
attributes are also eliminated. This loses information. 
• Outer joins, can be used when we want to keep all the tuples in R, 
all those in S, or all those in both relations 
– regardless of whether they have matching tuples in the other relation. 
• The left outer join operation keeps every tuple in the first or left 
relation R in R S; if no matching tuple is found in S, then 
the attributes of S in the join result are “padded” with null values. 
• A similar operation, right outer join, keeps every tuple in the 
second or right relation S in the result of R S. 
• A third operation, full outer join, denoted by keeps all 
tuples in both the left and the right relations when no matching 
tuples are found, padding them with null values as needed.
Left Outer Join 
E.g. List all employees and the department 
they manage, if they manage a department.
Examples of Queries in 
Relational Algebra 
• Work through Query 1 (p. 171) and Query 
2 (p. 172) of E&N 
• Show all intermediate relations as well as 
the final result

More Related Content

What's hot

Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusemailharmeet
 
Dbms ii mca-ch5-ch6-relational algebra-2013
Dbms ii mca-ch5-ch6-relational algebra-2013Dbms ii mca-ch5-ch6-relational algebra-2013
Dbms ii mca-ch5-ch6-relational algebra-2013Prosanta Ghosh
 
Relational Algebra & Calculus
Relational Algebra & CalculusRelational Algebra & Calculus
Relational Algebra & CalculusAbdullah Khosa
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Raj vardhan
 
5 the relational algebra and calculus
5 the relational algebra and calculus5 the relational algebra and calculus
5 the relational algebra and calculusKumar
 
Relational Algebra-Database Systems
Relational Algebra-Database SystemsRelational Algebra-Database Systems
Relational Algebra-Database Systemsjakodongo
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational AlgebraAmin Omi
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculusSalman Vadsarya
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages uoitc
 
Lesson 1 functions as models
Lesson 1 functions as modelsLesson 1 functions as models
Lesson 1 functions as modelsAddisonPascua
 
7_Intro_to_Functions
7_Intro_to_Functions7_Intro_to_Functions
7_Intro_to_Functionsnechamkin
 
DBMS : Relational Algebra
DBMS : Relational Algebra DBMS : Relational Algebra
DBMS : Relational Algebra Sridhar Baithi
 
Relational algebra
Relational algebraRelational algebra
Relational algebrashynajain
 
Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)yourbookworldanil
 

What's hot (18)

Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
 
Dbms ii mca-ch5-ch6-relational algebra-2013
Dbms ii mca-ch5-ch6-relational algebra-2013Dbms ii mca-ch5-ch6-relational algebra-2013
Dbms ii mca-ch5-ch6-relational algebra-2013
 
Relational Algebra & Calculus
Relational Algebra & CalculusRelational Algebra & Calculus
Relational Algebra & Calculus
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
 
5 the relational algebra and calculus
5 the relational algebra and calculus5 the relational algebra and calculus
5 the relational algebra and calculus
 
Relational Algebra-Database Systems
Relational Algebra-Database SystemsRelational Algebra-Database Systems
Relational Algebra-Database Systems
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculus
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages
 
Relational+algebra (1)
Relational+algebra (1)Relational+algebra (1)
Relational+algebra (1)
 
Lesson 1 functions as models
Lesson 1 functions as modelsLesson 1 functions as models
Lesson 1 functions as models
 
7_Intro_to_Functions
7_Intro_to_Functions7_Intro_to_Functions
7_Intro_to_Functions
 
DBMS : Relational Algebra
DBMS : Relational Algebra DBMS : Relational Algebra
DBMS : Relational Algebra
 
Chapter i
Chapter iChapter i
Chapter i
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)
 

Similar to 354 ch6

Module 2-2.ppt
Module 2-2.pptModule 2-2.ppt
Module 2-2.pptShylaja40
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)Mukund Trivedi
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational modelATS SBGI MIRAJ
 
3._Relational_Algebra.pptx:Basics of relation algebra
3._Relational_Algebra.pptx:Basics of relation algebra3._Relational_Algebra.pptx:Basics of relation algebra
3._Relational_Algebra.pptx:Basics of relation algebraZakriyaMalik2
 
Chapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdfChapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdfTamiratDejene1
 
Unit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdfUnit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdfajajkhan16
 
Relational operation final
Relational operation finalRelational operation final
Relational operation finalStudent
 
Relational Algebra.ppt
Relational Algebra.pptRelational Algebra.ppt
Relational Algebra.pptSreenivas R
 
1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdfKavinilaa
 
ML111 Lecture 5 Relational Algebra and Advanced SQL.pdf
ML111 Lecture 5 Relational Algebra and Advanced SQL.pdfML111 Lecture 5 Relational Algebra and Advanced SQL.pdf
ML111 Lecture 5 Relational Algebra and Advanced SQL.pdftory10027
 
Relational algebr
Relational algebrRelational algebr
Relational algebrVisakh V
 
RelationalAlgebra-RelationalCalculus-SQL.pdf
RelationalAlgebra-RelationalCalculus-SQL.pdfRelationalAlgebra-RelationalCalculus-SQL.pdf
RelationalAlgebra-RelationalCalculus-SQL.pdf10GUPTASOUMYARAMPRAK
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational AlgebraPyingkodi Maran
 
Relational algebra
Relational algebraRelational algebra
Relational algebraVENNILAV6
 
5th chapter Relational algebra.pptx
5th chapter Relational algebra.pptx5th chapter Relational algebra.pptx
5th chapter Relational algebra.pptxkavitha623544
 

Similar to 354 ch6 (20)

Module 2-2.ppt
Module 2-2.pptModule 2-2.ppt
Module 2-2.ppt
 
Chapter6
Chapter6Chapter6
Chapter6
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational model
 
3._Relational_Algebra.pptx:Basics of relation algebra
3._Relational_Algebra.pptx:Basics of relation algebra3._Relational_Algebra.pptx:Basics of relation algebra
3._Relational_Algebra.pptx:Basics of relation algebra
 
Chapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdfChapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdf
 
Unit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdfUnit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdf
 
Relational operation final
Relational operation finalRelational operation final
Relational operation final
 
Relational Algebra.ppt
Relational Algebra.pptRelational Algebra.ppt
Relational Algebra.ppt
 
1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf1695304562_RELATIONAL_ALGEBRA.pdf
1695304562_RELATIONAL_ALGEBRA.pdf
 
Relational Algebra-23-04-2023.pdf
Relational Algebra-23-04-2023.pdfRelational Algebra-23-04-2023.pdf
Relational Algebra-23-04-2023.pdf
 
ML111 Lecture 5 Relational Algebra and Advanced SQL.pdf
ML111 Lecture 5 Relational Algebra and Advanced SQL.pdfML111 Lecture 5 Relational Algebra and Advanced SQL.pdf
ML111 Lecture 5 Relational Algebra and Advanced SQL.pdf
 
Relational algebr
Relational algebrRelational algebr
Relational algebr
 
RelationalAlgebra-RelationalCalculus-SQL.pdf
RelationalAlgebra-RelationalCalculus-SQL.pdfRelationalAlgebra-RelationalCalculus-SQL.pdf
RelationalAlgebra-RelationalCalculus-SQL.pdf
 
Relational Database and Relational Algebra
Relational Database and Relational AlgebraRelational Database and Relational Algebra
Relational Database and Relational Algebra
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
Ch2
Ch2Ch2
Ch2
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Cs501 rel algebra
Cs501 rel algebraCs501 rel algebra
Cs501 rel algebra
 
5th chapter Relational algebra.pptx
5th chapter Relational algebra.pptx5th chapter Relational algebra.pptx
5th chapter Relational algebra.pptx
 

354 ch6

  • 1. Relational Algebra • The basic set of operations for the relational model is the relational algebra. – enable the specification of basic retrievals • The result of a retrieval is a new relation, which may have been formed from one or more relations. – algebra operations thus produce new relations, which can be further manipulated the same algebra. • A sequence of relational algebra operations forms a relational algebra expression, – the result will also be a relation that represents the result of a database query (or retrieval request). 2
  • 2. 3 What is an Algebra? • A language based on operators and a domain of values • Operators map values taken from the domain into other domain values • Hence, an expression involving operators and arguments produces a value in the domain • When the domain is a set of all relations we get the rreellaattiioonnaall aallggeebbrraa
  • 3. Relational Algebra Definitions • Domain: set of relations • Basic operators: sseelleecctt, pprroojjeecctt, uunniioonn, sseett ddiiffffeerreennccee, CCaarrtteessiiaann (cross) pprroodduucctt • Derived operators: sseett iinntteerrsseeccttiioonn, ddiivviissiioonn, jjooiinn • Procedural: Relational expression specifies query by describing an algorithm (the sequence in which operators are applied) for determining the result of an expression 4
  • 4. Unary Relational Operations • SELECT Operation: used to select a subset of the tuples from a relation that satisfy a selection condition. It is a filter that keeps only those tuples that satisfy a qualifying condition. Examples: sDNO = 4 (EMPLOYEE) sSALARY > 30,000 (EMPLOYEE) – denoted by s <selection condition>(R) where the symbol s (sigma) is used to denote the select operator, and the selection condition is a Boolean expression specified on the attributes of relation R 5
  • 5. SELECT Operation Properties The SELECT operation s <selection condition>(R) produces a relation S that has the same schema as R The SELECT operation s is commutative; i.e., 6 s <condition1>(s < condition2> ( R)) = s <condition2> (s < condition1> ( R)) A cascaded SELECT operation may be applied in any order; i.e., s <condition1>(s < condition2> (s <condition3> ( R)) = s <condition2> (s < condition3> (s < condition1> ( R))) A cascaded SELECT operation may be replaced by a single selection with a conjunction of all the conditions; i.e., s <condition1>(s < condition2> (s <condition3> ( R)) = s <condition1> AND < condition2> AND < condition3> ( R)))
  • 6. Selection Condition 7 • Operators: <, £, ³, >, =, ¹ • Simple selection condition: – <attribute> operator <constant> – <attribute> operator <attribute> – <condition> AND <condition> – <condition> OR <condition> – NOT <condition>
  • 7. Id Name Address Hobby 8 Select Examples PPeerrssoonn 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps s Id>3000 OR Hobby=‘hiking’ (PPeerrssoonn) s Id>3000 AND Id <3999 (PPeerrssoonn) s NOT(Hobby=‘hiking’) (PPeerrssoonn) s Hobby¹‘hiking’ (PPeerrssoonn)
  • 8. Unary Relational Operations (cont.) • PROJECT Operation: selects certain columns from the table and discards the others. Example: pLNAME, FNAME,SALARY(EMPLOYEE) The general form of the project operation is: p<attribute list>(R) where p is the symbol used to represent the project operation and <attribute list> is the desired list of attributes. PROJECT removes duplicate tuples, so the result is a set of tuples and hence a valid relation. 9
  • 9. PROJECT Operation Properties The number of tuples in the result of p <list> (R) is always less or equal to the number of tuples in R. If attribute list includes a key of R, then the number of tuples is equal to the number of tuples in R. p <list1> (p <list2> (R) ) = p <list1> (R) as long as <list2> contains the attributes in <list1> 10
  • 10. SELECT and PROJECT Operations (a) s(DNO=4 AND SALARY>25000) OR (DNO=5 AND SALARY>30000)(EMPLOYEE) (b) pLNAME, FNAME, SALARY(EMPLOYEE) (c) pSEX, SALARY(EMPLOYEE)
  • 11. Relational Algebra Operations from Set Theory • The UNION, INTERSECTION, and MINUS Operations • The CARTESIAN PRODUCT (or CROSS PRODUCT) Operation
  • 12. 13 Set Operators • A relation is a set of tuples, so set operations apply: Ç, È, - (set difference) • Result of combining two relations with a set operator is a relation => all elements are tuples with the same structure
  • 13. 14 UNION Operation Denoted by R È S Result is a relation that includes all tuples that are either in R or in S or in both. Duplicate tuples are eliminated. Example: Retrieve the SSNs of all employees who either work in department 5 or directly supervise an employee who works in department 5: DEP5_EMPS ¬ sDNO=5 (EMPLOYEE) RESULT1 ¬ p SSN(DEP5_EMPS) RESULT2(SSN) ¬ p SUPERSSN(DEP5_EMPS) RESULT ¬ RESULT1 È RESULT2 The union operation produces the tuples that are in either RESULT1 or RESULT2 or both. The two operands must be “type compatible”.
  • 14. UNION Operation Type (Union) Compatibility The operand relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) must have the same number of attributes, and the domains of corresponding attributes must be compatible, i.e. 15 – dom(Ai) = dom(Bi) for i=1, 2, ..., n.
  • 15. 16 Example Tables: PPeerrssoonn (SSN, Name, Address, Hobby) PPrrooffeessssoorr (Id, Name, Office, Phone) are not union compatible. But p Name (PPeerrssoonn) and p Name (PPrrooffeessssoorr) are union compatible so p Name (PPeerrssoonn) - p Name (PPrrooffeessssoorr) makes sense.
  • 16. 17 UNION Example STUDENT È INSTRUCTOR: What would STUDENT Ç INSTRUCTOR be?
  • 17. Set Difference Operation Set Difference (or MINUS) Operation The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S. The two operands must be "type compatible”. 18
  • 18. Set Difference Example S1 S2 SID SName Age 202 Rusty 21 403 Marcia 20 914 Hal 24 192 Jose 22 881 Stimpy 19 19 SID SName Age 473 Popeye 22 192 Jose 22 715 Alicia 28 914 Hal 24
  • 19. Relational Algebra Operations From 20 Set Theory (cont.) • Union and intersection are commutative operations: R È S = S È R, and R Ç S = S Ç R • Both union and intersection can be treated as n-ary operations applicable to any number of relations as both are associative operations; that is R È (S È T) = (R È S) È T, and (R Ç S) Ç T = R Ç (S Ç T) • The minus operation is not commutative; that is, in general R - S ≠ S – R
  • 20. Cartesian (Cross) Product • If RR and SS are two relations, RR ´ SS is the set of all concatenated tuples <x,y>, where x is a tuple in RR and y is a tuple in SS 21 – RR and SS need not be union compatible • RR ´ SS is expensive to compute: – Factor of two in the size of each row; Quadratic in the number of rows A B C D A B C D x1 x2 y1 y2 x1 x2 y1 y2 x3 x4 y3 y4 x1 x2 y3 y4 x3 x4 y1 y2 RR SS x3 x4 y3 y4 RR´ SS
  • 21. Cartesian Product Example • We want a list of COMPANY’s female employees dependents.
  • 22.
  • 23. Binary Relational Operations: JOIN and DIVISION • The JOIN Operation • The EQUIJOIN and NATURAL JOIN variations of JOIN • The DIVISION Operation
  • 24. 25 JOIN Operation • Cartesian product followed by select is commonly used to identify and select related tuples from two relations => called JOIN. It is denoted by a – This operation is important for any relational database with more than a single relation, because it allows us to process relationships among relations. – The general form of a join operation on two relations R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is: R <join condition>S where R and S can be any relations that result from general relational algebra expressions.
  • 25. The Binary Join Operation DEPT_MGR ¬ DEPARTMENT MGRSSN=SSN EMPLOYEE
  • 26. EQUIJOIN & NATURAL JOIN 27 • EQUIJOIN – most common join: join conditions with equality comparisons only. – in the result of an EQUIJOIN we always have one or more pairs of attributes (whose names need not be identical) that have identical values in every tuple. – The JOIN in the previous example was EQUIJOIN. • NATURAL JOIN – Because one of each pair of attributes with identical values is superfluous, a new operation called natural join—denoted by *—was created to get rid of the second (superfluous) attribute. – The standard definition of natural join requires that each pair of corresponding join attributes, have the same name in both relations. If this is not the case, a renaming operation is applied.
  • 27. Natural Join Operations (a) PROJ_DEPT ¬ PROJECT * DEPT (b) DEPT_LOCS ¬ DEPARTMENT * DEPT_LOCATIONS
  • 28. The DIVISION Operation (a) Dividing SSN_PNOS by SMITH_PNOS. (b) T ¬ R ÷ S.
  • 29. Additional Relational Operations • Aggregate Functions and Grouping • Recursive Closure Operations • The OUTER JOIN Operation
  • 30. Aggregate Functions E.g. SUM, AVERAGE, MAX, MIN, COUNT
  • 32. 33 OUTER JOINs • In NATURAL JOIN tuples without a matching (or related) tuple are eliminated from the join result. Tuples with null in the join attributes are also eliminated. This loses information. • Outer joins, can be used when we want to keep all the tuples in R, all those in S, or all those in both relations – regardless of whether they have matching tuples in the other relation. • The left outer join operation keeps every tuple in the first or left relation R in R S; if no matching tuple is found in S, then the attributes of S in the join result are “padded” with null values. • A similar operation, right outer join, keeps every tuple in the second or right relation S in the result of R S. • A third operation, full outer join, denoted by keeps all tuples in both the left and the right relations when no matching tuples are found, padding them with null values as needed.
  • 33. Left Outer Join E.g. List all employees and the department they manage, if they manage a department.
  • 34. Examples of Queries in Relational Algebra • Work through Query 1 (p. 171) and Query 2 (p. 172) of E&N • Show all intermediate relations as well as the final result