Chapter 5 Part 1
Chapter 5 Part 1
Systems
▪ Binary Operations
▪ Union
▪ Set difference
▪ Cartesian Product
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:
DNO = 4 (EMPLOYEE)
SALARY > 30,000 (EMPLOYEE)
– denoted by <selection condition>(R) where the symbol (sigma) is used to
denote the select operator, and the selection condition is a Boolean
expression specified on the attributes of relation R
21
SELECT Operation Properties
The SELECT operation <selection condition>(R) produces a relation S that has the
same schema as R
23
Select Examples
Person Id Name Address Hobby
1123 John 123 Main stamps
1123 John 123 Main coins
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps
NOT(Hobby=‘hiking’) (Person)
Hobby‘hiking’ (Person) 24
Unary Relational Operations (cont.)
• PROJECT Operation: selects certain columns from the table and
discards the others.
Example:
LNAME, FNAME,SALARY (EMPLOYEE)
The general form of the project operation is: <attribute list>(R) where is
the symbol used to represent the project operation and <attribute list> is the
desired list of attributes.
27
PROJECT Operation Properties
The number of tuples in the result of <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.
<list1> ( <list2> (R) ) = <list1> (R) as long as <list2> contains the attributes
in <list1>
28
The number of tuples in the result of π<list>(R) is always less than or equal to the number of tuples
in R.
Example: Consider a relation Student with the attributes:
SINCE DUPLICATE TUPLES ARE REMOVED, THE NUMBER OF TUPLES IN THE RESULT ≤ THE NUMBER OF TUPLES IN THE ORIGINAL RELATION.
If the attribute list includes a key of R, then the number of tuples is equal to the number of tuples in
R.
If we project on a key (a set of attributes that uniquely identify tuples), the number of tuples
remains the same.
Example: If we do π(ID, Name)(Student):
SINCE ID IS A UNIQUE KEY, NO TUPLES ARE REMOVED, SO THE NUMBER OF TUPLES REMAINS 3 (SAME AS THE ORIGINAL TABLE).
π<list1>(π<list2>(R))=π<list1>(R) as long as <list2><list2><list2> contains all attributes in <list1><list1><list1>.
If we first apply π<list2>(R) and then apply π<list1>, it gives the same result as directly applying π<list1>(R)—as
long as <list2> includes all attributes in <list1>
SELECT and PROJECT Operations
36
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 DNO=5 (EMPLOYEE)
RESULT1 SSN(DEP5_EMPS)
RESULT2(SSN) 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”.
37
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.
– dom(Ai) = dom(Bi) for i=1, 2, ..., n.
38
Example
Tables:
Person (SSN, Name, Address, Hobby)
Professor (Id, Name, Office, Phone)
are not union compatible.
But
Name (Person) and Name (Professor)
are union compatible so
Name (Person) - Name (Professor)
makes sense. 39
UNION Example
STUDENT INSTRUCTOR:
40
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”.
41
Set Difference Example
S1 S2
SID SName Age SID SName Age
473 Popeye 22 202 Rusty 21
192 Jose 22 403 Marcia 20
715 Alicia 28 914 Hal 24
914 Hal 24 192 Jose 22
881 Stimpy 19
42
Relational Algebra Operations From
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)
43
Cartesian (Cross) Product
• If R and S are two relations, R S is the set of all concatenated tuples <x,y>, where x is a
tuple in R and y is a tuple in S
– R and S need not be union compatible
• R S 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
R S x3 x4 y3 y4
44
R S
Cartesian Product Example
50
Theta JOIN