Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Complex Operators

Similar presentations


Presentation on theme: "More Complex Operators"— Presentation transcript:

1 More Complex Operators
Relational Algebra #2 More Complex Operators cs3431

2 More Algebra Operators
Set Operators: Union, Intersection, Difference Cross (Cartesian) Product Select, Project Join: Natural Join, Theta Join, (Left/Right) Outer Join Renaming, Duplicate Elimination Aggregation: MIN, MAX, COUNT, SUM, AVG Grouping cs3431

3 Natural Join: R ⋈ S Consider relations Let A = AR ∩ AS, and
R with attributes AR, and S with attributes AS. Let A = AR ∩ AS, and A = {A1, A2, …, An} R ⋈ S can be defined as : πAR – A, A, AS - A (σR.A1 = S.A1 AND R.A2 =S.A2 AND … R.An=S.An (R X S)) cs3431

4 Natural Join example R1 R2 R1 ⋈ R2 A B 1 2 B C 2 3 4 5 A B C 1 2 3
cs3431

5 Theta Join: R ⋈C S Theta Join is Cross product, with condition C.
It is defined as : R ⋈C S = (σC (R X S)) R1 ⋈ R1.B<R2.BR2 R1 R2 A R1.B R2.B C 1 2 4 5 3 A B 1 2 3 B C 2 3 4 5 cs3431

6 Outer Join: R ⋈o S Similar to natural join (plus with extra tuples that preserve all content of R and S) R1 ⋈o R2 R1 R2 A B C D 1 2 3 10 11 4 5 6 null 7 8 9 12 A B C 1 2 3 4 5 6 7 8 9 B C D 2 3 10 11 6 7 12 But if a tuple in R has no “matching” tuple in S, then tuple appears in output with NULL for attributes in S, and vice versa. cs3431

7 Left Outer Join: R ⋈oLS Similar to natural join (not symmetric !)
But if a tuple in R1 has no “matching” tuple in R2, then tuple appears in output , with NULL for attributes in R2 R1 ⋈oL R2 R1 R2 A B C D 1 2 3 10 11 4 5 6 null 7 8 9 A B C 1 2 3 4 5 6 7 8 9 B C D 2 3 10 11 6 7 12 cs3431

8 Right Outer Join: R ⋈oRS
Similar to natural join (not symmetric !) But if a tuple in R2 has no “matching” tuple in R1, then tuple appears in output , with NULL for attributes in R1 R1 ⋈oR R2 R1 R2 A B C D 1 2 3 10 11 null 6 7 12 A B C 1 2 3 4 5 6 7 8 9 B C D 2 3 10 11 6 7 12 cs3431

9 Renaming ρS (R) changes relation name from R to S
ρS(A1, A2, …, An) (R) renames also attributes of R to A1, A2, …, An. ρS(X, C, D) (R2) ρS (R2) R2 S S B C D 2 3 10 11 6 7 12 X C D 2 3 10 11 6 7 12 B C D 2 3 10 11 6 7 12 cs3431

10 Duplicate Elimination:  (R)
Convert a bag to a set.  (R) R A B 1 2 3 4 A B 1 2 3 4 cs3431

11 Extended Projection: πL (R)
Here L can be : An attribute (just like simple projection) An expression x → y, where x and y are names of attributes This renames attribute x to y. An expression E → z, where E is any expression with attributes, constants, and arithmetic operators This generates an attribute called z whose values are given by E. π C, B→A, C+D→X (R) cs3431

12 Extended Projection: πL (R)
πB→A, C+D→X, C, D (R) R A X C D 2 13 3 10 14 11 6 19 7 12 B C D 2 3 10 11 6 7 12 cs3431

13 Aggregation operators
MIN, MAX, COUNT, SUM, AVG AGGB (R) considers only non-null values of R. SUMB (R) MINB (R) COUNTB (R) R MINB (R) 2 SUMB (R) 9 COUNTB (R) 3 A B 1 2 3 4 null AVGB (R) COUNT* (R) MAXB (R) AVGB (R) 3 COUNT* (R) 4 MAXB (R) 4 cs3431

14 Making Aggregation Useful
AVG gpa ( Students) returns one average for all students. What if I would like the average gpa for freshman, sophomore, junior and senior students, separately? What if I would like the average gpa for students from different origins (country / city / etc)? cs3431

15 Grouping Operator: GL, AL (R)
GL, AL (R) groups all attributes in GL, and performs aggregation specified in AL.  starName, MIN (year)→year, COUNT(title) →num (StarsIn) StarsIn title year starName SW1 77 HF Matrix 99 KR 6D&7N 93 SW2 79 Speed 94 starName year num HF 77 3 KR 94 2 cs3431

16 Summary : Algebra Operators
Set Operators: Union, Intersection, Difference Cartesian Product Select, Project Join: Natural Join, Theta Join, (Left/Right) Outer Join Renaming, Duplicate Elimination Aggregation: MIN, MAX, COUNT, SUM, AVG Grouping cs3431


Download ppt "More Complex Operators"

Similar presentations


Ads by Google