RA Join Operations

COMP3311 20T3 ♢ RA Join Operations ♢ [0/13]
❖ RA Operations to Combine Relations


Relational algebra has several ways to combine info from relations

Join conditions involve related attributes from the two relations

Frequently, primary-key joined with foreign-key

COMP3311 20T3 ♢ RA Join Operations ♢ [1/13]
❖ Product

Product (Cartesian product) combines information from two relations pairwise on tuples.

r × s   =   { (t1 : t2)  |  t1 ∈ r ∧ t2 ∈ s },     where r(R), s(S)

Each tuple in the result contains all attributes from r and s, possibly with some fields renamed to avoid ambiguity.

If t1 = (A1...An) and t2 = (B1...Bn) then (t1:t2) = (A1...An,B1...Bn)

Result size is large:   |r × s| = |r|.|s|     Schema: R∪S

Algorithmic view:

result = {}
for each tuple t1 in relation r
    for each tuple t2 in relation s
        result = result ∪ {(t1:t2)} }

COMP3311 20T3 ♢ RA Join Operations ♢ [2/13]
❖ Product (cont)

Example of product:


[Diagram:Pics/relalg/product.png]

COMP3311 20T3 ♢ RA Join Operations ♢ [3/13]
❖ Natural Join

Natural join is a specialised product:

Consider relation schemas R(ABC..JKLM), S(KLMN..XYZ).

The natural join of relations r(R) and s(S) is defined as:

r ⨝ s   =   r Join s   =   { (t1[ABC..J] : t2[K..XYZ])  |  t1 ∈ r ∧ t2 ∈ s ∧ match }

where    match   =   t1[K] = t2[K] ∧ t1[L] = t2[L] ∧ t1[M] = t2[M]

Algorithmic view:

result = {}
for each tuple t1 in relation r
   for each tuple t2 in relation s
      if (matches(t1,t2))
         result = result ∪ {combine(t1,t2)}

COMP3311 20T3 ♢ RA Join Operations ♢ [4/13]
❖ Natural Join (cont)

Example of natural join:

[Diagram:Pics/relalg/nat-join.png]

COMP3311 20T3 ♢ RA Join Operations ♢ [5/13]
❖ Theta Join

The theta join is a specialised product containing only pairs that match on a supplied condition C.

r ⨝C s   =   { (t1 : t2)  |  t1 ∈ r ∧ t2 ∈ s ∧ C(t1 : t2) },
where r(R),s(S)

Examples:   (r1 Join[B>E] r2) ... (r1 Join[E<D∧C=G] r2)

All attribute names are required to be distinct (cf natural join)

Can be defined in terms of other RA operations:

r ⨝C s   =   r Join[C] s   =   Sel[C] ( r × s )

Note that   r ⨝true s   =   r × s.

COMP3311 20T3 ♢ RA Join Operations ♢ [6/13]
❖ Theta Join (cont)

Example theta join:

[Diagram:Pics/relalg/join2.png]

(Theta join is the most frequently-used join in SQL queries)

COMP3311 20T3 ♢ RA Join Operations ♢ [7/13]
❖ Theta Join (cont)

Querying with relational algebra (join) ...


Reminder: projection removes duplicates

COMP3311 20T3 ♢ RA Join Operations ♢ [8/13]
❖ Outer Join

R ⨝C S does not include in its result

R ⟕C S (left outer join) includes For tuples with no match, assign NULL to "unmatched" attributes


Variants are right outer join and full outer join

COMP3311 20T3 ♢ RA Join Operations ♢ [9/13]
❖ Outer Join (cont)

Example left outer join:

[Diagram:Pics/relalg/outer.png]

COMP3311 20T3 ♢ RA Join Operations ♢ [10/13]
❖ Division

Consider two relation schemas R and S where S ⊂ R.

The division operation is defined on instances r(R), s(S) as:

r / s   =   r Div s   =   { t | t ∈ r[R-S] ∧ satisfy }

where   satisfy   =   ∀ ts ∈ S ( ∃ tr ∈ R ( tr[S] = ts ∧ tr[R-S] = t ) )

Operationally:

COMP3311 20T3 ♢ RA Join Operations ♢ [11/13]
❖ Division (cont)

Example of division:


[Diagram:Pics/relalg/division2.png]

COMP3311 20T3 ♢ RA Join Operations ♢ [12/13]
❖ Division (cont)

Querying with relational algebra (division) ...

Division handles queries that include the notion "for all".

E.g. Which beers are sold in all bars?

We can answer this as follows:

COMP3311 20T3 ♢ RA Join Operations ♢ [13/13]


Produced: 11 Nov 2020