Relational Algebra Instructor: Mohamed Eltabakh [email protected] 1

Embed Size (px)

Citation preview

  • Slide 1

Relational Algebra Instructor: Mohamed Eltabakh [email protected] 1 Slide 2 More Relational Operators 2 Slide 3 Joins We mentioned Cartesian Product multiplies two relations 3 R S R X S What if I want to join R and S based on a certain condition? Natural and Theta Joins Slide 4 Natural Join: R S (Join on the common attributes) Consider relations R with attributes A R, and S with attributes A S. Let A = A R A S = {A1, A2, , An} The common attributes In English Natural join R S is a Cartesian Product R X S with equality predicates on the common attributes (Set A) 4 =S.C S A"> Theta Join: R C S Theta Join is cross product, with condition C It is defined as : R C S = ( C (R X S)) AB 12 32 R DC 23 45 45 S R R.A>=S.C S ABDC 3223 Theta join can express both Cartesian Product & Natural Join 7 Recommendation: Always use Theta join (more explicit and more clear) Slide 8 Example Queries Find customer names having account balance below 100 or above 10,000 customer_name (depositor account_number ( balance 10,000 (account))) 8 Slide 9 Assignment Operator: The assignment operation () provides a convenient way to express complex queries on multiple line Write query as a sequence of line consisting of: Series of assignments Result expression containing the final answer Assignment must always be made to a temporary relation variable May use a variable multiple times in subsequent expressions Example: R1 ( ((A=B) ^ (D>5)) (R S)) W R2 R1 (R.A = T.C) T Result R1 U R2 9 Slide 10 Example Queries 10 For branches that gave loans > 100,000 or hold accounts with balances >50,000, report the branch name along whether it is reported because of a loan or an account R1 branch_name, Loan As Type ( amount >100,000 (loan)) R2 branch_name, Account As Type ( balance > 50,000 (account))) Result R1 U R2 Slide 11 Example Queries Find customers having account balance below 100 and loans above 10,000 R1 customer_name (depositor account_number ( balance 10,000 (loan))) Result R1 R2 11 Slide 12 More Relational Operators 12 Slide 13 Outer Join An extension of the join operation that avoids loss of information Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result Uses null values to fill in empty attributes with no matching Types of outer join between R and S Left outer ( R o S) : preserve all tuples from the left relation R Right outer (R o S): preserve all tuples from the right relation S Full outer (R S): preserve all tuples from both relations o 13 Slide 14 Left Outer Join ( R o S): Example RS 14 R S (R o S) Slide 15 Right Outer Join ( R o S): Example RS R S (R o S) 15 Slide 16 Full Outer Join ( R S): Example RS R S (R S) o o 16 Slide 17 Duplicate Elimination: (R) Delete all duplicate records Convert a bag to a set R AB 12 34 12 12 (R) AB 12 34 17 Slide 18 Grouping & Aggregation operator: Aggregation function takes a collection of values and returns a single value as a result avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Grouing & Aggregate operation in relational algebra g1,g2, gm, F1(A1), F2(A2), Fn(An) (R) R is a relation or any relational-algebra expression g1, g2, gm is a list of attributes on which to group (can be empty) Each Fi is an aggregate function applied on attribute Ai within each group 18 Slide 19 Grouping & Aggregation Operator: Example sum(c) (R) R S branch_name,sum(balance) (S) 19 Slide 20 Example Queries Find customer names having loans with sum > 20,000 customer_name ( sum > 20,000 ( customer_name, sum sum(amount) (loan borrower))) 20 Slide 21 Example Queries Find the branch name with the largest number of accounts R1 branch_name, countAccounts count(account_number) (account) R2 Max max(countAccounts) (R1) Result branch_name (R1 countAccounts = Max R2) 21 Slide 22 Example Queries Find account numbers and balances for customers having loans > 10,000 account_number, balance ( (depositor account) ( customer_name (borrower ( amount >10,000 (loan)))) ) 22 Slide 23 Reversed Queries (what does it do)? Find customers who did not take loans customer_name (customer) - customer_name (borrower) 23 Slide 24 Reversed Queries (what does it do)? Find customer name with the largest loan from a branch ABC R1 ( MaxLoan max(amount) ( branch_name= ABC (loan))) Result customer_name (borrower (R1 MaxLoan=amount^branch_name= ABC loan)) 24 Slide 25 Example Queries Find customer name with the largest loan from a branch in NY city 25 Slide 26 Summary of Relational-Algebra Operators Set operators Union, Intersection, Difference Selection & Projection & Extended Projection Joins Natural, Theta, Outer join Rename & Assignment Duplicate elimination Grouping & Aggregation 26