180 likes | 190 Views
Learn about division, natural join, and outer join operations in relational algebra with examples and queries.
E N D
Session 3 Welcome: To session 3-the fifth learning sequence “ Relational algebra “ Recap : In the previous learning sequences, we discussed the eight operators of relational algebra. Present learning: We shall explore the following topic: - RA: Division and Joins
Relational Algebra • Two Advanced RA operators: • Division • Natural-Join Operation • The operators take two or more relations as inputs and give a new relation as a result.
Division Operation Suited to queries that include the phrase “for all”. Let r and s be relations on schemas R and S respectively where R = (A1, …, Am, B1, …, Bn) S = (B1, …, Bn) The result of r s is a relation on schema R – S = (A1, …, Am) r s = { t | t R-S(r) u s ( tu r ) } r s
Division Operation – Example A B B Relations r, s: 1 2 3 1 1 1 3 4 6 1 2 1 2 s r s: A r
Another Division Example Relations r, s: A B C D E D E a a a a a a a a a a b a b a b b 1 1 1 1 3 1 1 1 a b 1 1 s r A B C r s: a a
Natural-Join Operation Let r and s be relations on schemas R and S respectively. Then, r s is a relation on schema R S obtained as follows: Consider each pair of tuples tr from r and ts from s. If tr and ts have the same value on each of the attributes in RS, add a tuple t to the result, where t has the same value as tr on r t has the same value as ts on s • Notation: r s
Natural-Join Operation Example: R = (A, B, C, D) S = (E, B, D) Result schema = (A, B, C, D, E) rs is defined as:r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s)) • Notation: r s
Natural Join Operation – Example Relations r, s: r s B D E A B C D 1 3 1 2 3 a a a b b 1 2 4 1 2 a a b a b r s A B C D E 1 1 1 1 2 a a a a b
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 of the join. Uses null values: null signifies that the value is unknown or does not exist All comparisons involving null are (roughly speaking) false by definition. Will study precise meaning of comparisons with nulls later
Outer Join – Example Relation loan branch-name loan-number amount Downtown Redwood Perryridge L-170 L-230 L-260 3000 4000 1700 customer-name loan-number Jones Smith Hayes L-170 L-230 L-155 • Relation borrower
Outer Join – Example Inner JoinloanBorrower loan-number branch-name amount customer-name L-170 L-230 Downtown Redwood 3000 4000 Jones Smith • Left Outer Join loan Borrower loan-number branch-name amount customer-name L-170 L-230 L-260 Downtown Redwood Perryridge 3000 4000 1700 Jones Smith null
Outer Join – Example Right Outer Join loanborrower loan-number branch-name amount customer-name L-170 L-230 L-155 Downtown Redwood null 3000 4000 null Jones Smith Hayes loan-number branch-name amount customer-name L-170 L-230 L-260 L-155 Downtown Redwood Perryridge null 3000 4000 1700 null Jones Smith null Hayes • Full Outer Join Loanborrower
Example Queries (1) Find all customers who have an account from at least the “Downtown” and the Uptown” branches. Query 1 CN(BN=“Downtown”(depositoraccount)) CN(BN=“Uptown”(depositoraccount)) where CN denotes customer-name and BN denotes branch-name.
Example Queries (2) Find all customers who have an account from at least the “Downtown” and the Uptown” branches. Query 2 customer-name, branch-name(depositoraccount) temp(branch-name) ({(“Downtown”), (“Uptown”)})
Find all customers who have an account at all branches located in Brooklyn city. Example Queries (3) customer-name, branch-name(depositoraccount) branch-name (branch-city = “Brooklyn” (branch))
Relational Algebra Summary: In this learning sequence, we discussed another basic operators of the topic relational algebra.