A unary operation that returns its argument relation, with certain attributes left out.
Notation:
ΠA1,A2,A3…Ak(r)
where A1,A2,…,Ak are attribute names and r is a relation name.
The result is defined as the relation of k columns obtained by erasing the columns that are not listed
Duplicate rows removed from result, since relations are sets
Composition of Relational Operations
The result of a relational-algebra operation is relation and therefore of relational-algebra operations can be composed together into a relational-algebra expression.
Consider the query -- Find the names of all instructors in the Physics department.
Πname(σdept_name="Physics"(instructor))
Instead of giving the name of a relation as the argument of the projection operation, we give an expression that evaluates to a relation.
Cartesian-Product Operation
The Cartesian-product operation (denoted by X) allows us to combine information from any two relations.
Example: the Cartesian product of the relations instructor and teaches is written as:
instructor X teaches
We construct a tuple of the result out of each possible pair of tuples: one from the instructor relation and one from the teaches relation (see next slide)
Since the instructor ID appears in both relations we distinguish between these attribute by attaching to the attribute the name of the relation from which the attribute originally came.
instructor.ID
teaches.ID
The instructor X teaches table
Figure 2.5 — The Cartesian Product Table instructor X teaches (Source: Slide 17)
instructor.ID
name
dept_name
salary
teaches.ID
course_id
sec_id
semester
year
10101
Srinivasan
Comp. Sci.
65000
10101
CS-101
1
Fall
2017
10101
Srinivasan
Comp. Sci.
65000
10101
CS-315
1
Spring
2018
10101
Srinivasan
Comp. Sci.
65000
10101
CS-347
1
Fall
2017
10101
Srinivasan
Comp. Sci.
65000
12121
FIN-201
1
Spring
2018
10101
Srinivasan
Comp. Sci.
65000
15151
MU-199
1
Spring
2018
10101
Srinivasan
Comp. Sci.
65000
22222
PHY-101
1
Fall
2017
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
12121
Wu
Finance
90000
10101
CS-101
1
Fall
2017
12121
Wu
Finance
90000
10101
CS-315
1
Spring
2018
12121
Wu
Finance
90000
10101
CS-347
1
Fall
2017
12121
Wu
Finance
90000
12121
FIN-201
1
Spring
2018
12121
Wu
Finance
90000
15151
MU-199
1
Spring
2018
12121
Wu
Finance
90000
22222
PHY-101
1
Fall
2017
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
15151
Mozart
Music
40000
10101
CS-101
1
Fall
2017
15151
Mozart
Music
40000
10101
CS-315
1
Spring
2018
15151
Mozart
Music
40000
10101
CS-347
1
Fall
2017
15151
Mozart
Music
40000
12121
FIN-201
1
Spring
2018
15151
Mozart
Music
40000
15151
MU-199
1
Spring
2018
15151
Mozart
Music
40000
22222
PHY-101
1
Fall
2017
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
22222
Einstein
Physics
95000
10101
CS-101
1
Fall
2017
22222
Einstein
Physics
95000
10101
CS-315
1
Spring
2018
22222
Einstein
Physics
95000
10101
CS-347
1
Fall
2017
22222
Einstein
Physics
95000
12121
FIN-201
1
Spring
2018
22222
Einstein
Physics
95000
15151
MU-199
1
Spring
2018
22222
Einstein
Physics
95000
22222
PHY-101
1
Fall
2017
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
Join Operation (Cont.)
The table corresponding to:
σinstructor.id=teaches.id(instructor×teaches))
Can equivalently be written as
instructor⋈instructor.id=teaches.idteaches.
Union Operation
The union operation allows us to combine two relations
Notation: r∪s
For r∪s to be valid.
r,s must have the samearity (same number of attributes)
The attribute domains must be compatible (example: 2nd column of r deals with the same type of values as does the 2nd column of s)
Example: to find all courses taught in the Fall 2017 semester, or in the Spring 2018 semester, or in both
Πcourse_id(σsemester="Fall"∧year=2017(section))∪Πcourse_id(σsemester="Spring"∧year=2018(section))
section table
Figure 2.7 — The section Relation (Source: Slide 21)
course_id
sec_id
semester
year
building
room_number
time_slot_id
BIO-101
1
Summer
2017
Painter
514
B
BIO-301
1
Summer
2018
Painter
514
A
CS-101
1
Fall
2017
Packard
101
H
CS-101
1
Spring
2018
Packard
101
F
CS-190
1
Spring
2017
Taylor
3128
E
CS-190
2
Spring
2017
Taylor
3128
A
CS-315
1
Spring
2018
Watson
120
D
CS-319
1
Spring
2018
Watson
100
B
CS-319
2
Spring
2018
Taylor
3128
C
CS-347
1
Fall
2017
Taylor
3128
A
EE-181
1
Spring
2017
Taylor
3128
C
FIN-201
1
Spring
2018
Packard
101
B
HIS-351
1
Spring
2018
Painter
514
C
MU-199
1
Spring
2018
Packard
101
D
PHY-101
1
Fall
2017
Watson
100
A
Union Operation (Cont.)
Result of:
Πcourse_id(σsemester="Fall"∧year=2017(section))∪Πcourse_id(σsemester="Spring"∧year=2018(section))
The set-intersection operation allows us to find tuples that are in both the input relations.
Notation: r∩s
Assume:
r,s have the same arity
attributes of r and s are compatible
Example: Find the set of all courses taught in both the Fall 2017 and the Spring 2018 semesters.
Πcourse_id(σsemester="Fall"∧year=2017(section))∩Πcourse_id(σsemester="Spring"∧year=2018(section))
Result
course_id
CS-101
Set Difference Operation
The set-difference operation allows us to find tuples that are in one relation but are not in another.
Notation: r−s
Set differences must be taken between compatible relations.
r and s must have the same arity
Attribute domains of r and s must be compatible
Example: to find all courses taught in the Fall 2017 semester, but not in the Spring 2018 semester
Πcourse_id(σsemester="Fall"∧year=2017(section))−Πcourse_id(σsemester="Spring"∧year=2018(section))
course_id
CS-347
PHY-101
The Assignment Operation
It is convenient at times to write a relational-algebra expression by assigning parts of it to temporary relation variables.
The assignment operation is denoted by ← and works like assignment in a programming language.
Example: Find all instructor in the “Physics” and Music department.
Physics←σdept_name="Physics"(instructor)Music←σdept_name="Music"(instructor)Physics∪Music
With the assignment operation, a query can be written as a sequential program consisting of a series of assignments followed by an expression whose value is displayed as the result of the query.
The Rename Operation
The results of relational-algebra expressions do not have a name that we can use to refer to them. The rename operator, ρ, is provided for that purpose
The expression:
ρx(E)
returns the result of expression E under the name x
Another form of the rename operation:
ρx(A1,A2,…An)(E)
Aggregate Functions
These functions operate on the multiset (set with duplicates) of values of a column of a relation, and return a value
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate Functions Examples
Find the average salary of instructors
γavg(salary)(instructor)
Find the total number of courses in 2018
γcount(course_id)(σyear=2018(section))
Aggregate Functions – Group By
Find the average salary of instructors in each department
dept_nameγavg(salary)(instructor)
Figure 2.9 — Aggregate Function with Group By Operation (Source: Slide 29)
instructor
ID
name
dept_name
salary
76766
Crick
Biology
72000
45565
Katz
Comp. Sci.
75000
10101
Srinivasan
Comp. Sci.
65000
83821
Brandt
Comp. Sci.
92000
98345
Kim
Elec. Eng.
80000
12121
Wu
Finance
90000
76543
Singh
Finance
80000
32343
El Said
History
60000
58583
Califieri
History
62000
15151
Mozart
Music
40000
33456
Gold
Physics
87000
22222
Einstein
Physics
95000
dept_name / avg_salary
dept_name
avg_salary
Biology
72000
Comp. Sci.
77333
Elec. Eng.
80000
Finance
85000
History
61000
Music
40000
Physics
91000
Equivalent Queries
There is more than one way to write a query in relational algebra.
Example: Find information about courses taught by instructors in the Physics department with salary greater than 90,000