04 Rel-Algebra2
04 Rel-Algebra2
R( A B ) S( B C )
1 2 3 4
5 6 7 8
1 2
σA+B<5 (R) = A B
1 2
1 2
Example: Bag Projection
R( A, B ) S( B, C )
1 2 3 4
5 6 7 8
1 2
R( A, B ) S( B, C )
1 2 3 4
5 6 7 8
1 2
• Each copy of the
R×S= A R.B S.B C tuple (1,2) of R is
1 2 3 4 being paired with
1 2 7 8 each tuple of S.
5 6 3 4
5 6 7 8 • So, the duplicates do
1 2 3 4 not have an effect on
1 2 7 8 the way we compute
the product.
Bag Union
• Union, intersection, and difference need new
definitions for bags.
• An element appears in the union of two bags the
sum of the number of times it appears in each
bag.
• Example:
{1,2,1} ∪ {1,1,2,3,1}
= {1,1,1,1,1,2,2,3}
Bag Intersection
• An element appears in the intersection of two
bags the minimum of the number of times it
appears in either.
• Example:
{1,2,1} ∩ {1,2,3}
= {1,2}.
Bag Difference
δ(R) = A B
1 2
3 4
Sorting
• R1 := τ L (R2).
– L is a list of some of the attributes of R2.
R= A B
1 2
3 4
πA+BC,AA1,AA2 (R) = C A1 A2
3 1 1
7 3 3
Aggregation Operators
R= A B
1 3
3 4
3 2
SUM(A) = 7
COUNT(A) = 3
MAX(B) = 4
MIN(B) = 2
AVG(B) = 3
Grouping Operator
• R1 := γL (R2).
• L is a list of elements that are either:
1. Individual (grouping ) attributes.
2. AGG(A), where AGG is one of the
aggregation operators and A is an attribute.
Applying γL(R)
• Group R according to all the grouping attributes on
list L.
– That is, form one group for each distinct list of values for
those attributes in R.
R= A B C
1 2 3 Then, average C within
4 5 6 groups:
1 2 5
A B AVG(C)
γ A,B,AVG(C) (R) = ?? 1 2 4
First, group R: 4 5 6
A B C
1 2 3
1 2 5
4 5 6
Example: Grouping/Aggregation
• StarsIn(title, year, starName)
• We want, for each star who has appeared in at least three
movies the earliest year in which he or she appeared.
– First we group, using starName as a grouping attribute.
– Then, we have to compute the MIN(year) for each group.
– However, we need also compute COUNT(title) aggregate for
each group, in order to filter out those stars with less than three
movies.
• σctTitle>3[γstarName,MIN(year)minYear,COUNT(title)ctTitle(StarsIn)]
Outerjoin
• Suppose we join R S.
• A tuple of R that has no tuple of S with which
it joins is said to be dangling.
– Similarly for a tuple of S.
• Outerjoin preserves dangling tuples by
padding them with a special NULL symbol in
the result.
Example: Outerjoin
R= A B S= B C
1 2 2 3
4 5 6 7
• γA,SUM(B)(R)
• R S