Skip to content
Part IA Michaelmas Term

Relational Algebra: Product and Renaming

Product (×\times)

The RA product is based on the Cartesian product but adapted for database relations. Given R(A1,,Am)R(A_1, \ldots, A_m) and S(B1,,Bn)S(B_1, \ldots, B_n), the product R×SR \times S returns a relation over the combined attributes A1,,Am,B1,,BnA_1, \ldots, A_m, B_1, \ldots, B_n.

Mathematically, the Cartesian product of two relations (as sets of tuples) would return pairs of tuples:

{(t,u)tR,uS}\{(t, u) \mid t \in R, u \in S\}

The RA product instead returns tuples formed by concatenating the fields of each pair:

{tutR,uS}\{t \cdot u \mid t \in R, u \in S\}

where \cdot denotes tuple concatenation.

Example. Suppose:

RR:

AB
1x
2y

SS:

CD
p10
q20

R×SR \times S:

ABCD
1xp10
1xq20
2yp10
2yq20

If RR has mm rows and SS has nn rows, the product has m×nm \times n rows.

Column name disjointness

A critical constraint: the schemas of RR and SS must have disjoint column names. If both relations have a column called id, the product would have two columns both called id, which is not a valid relation.

When column names overlap, renaming is required first. For example, if R(A,B)R(A, B) and S(B,C)S(B, C) share column BB:

ρ{BB}(S)\rho_{\{B \mapsto B'\}}(S)

produces S(B,C)S'(B', C), and then:

R×SR \times S'

produces a valid relation over (A,B,B,C)(A, B, B', C).

SQL translations

There are two equivalent SQL formulations of the product:

Explicit syntax (preferred).

SELECT A, B, C, D
FROM R CROSS JOIN S;

Implicit syntax (comma-separated tables).

SELECT A, B, C, D
FROM R, S;

Both produce the same result. The CROSS JOIN form makes the intent more explicit.

The product is rarely used alone

The product itself is of limited practical use. Its value comes from combining it with selection to form a join. Selecting the product rows where matching columns are equal gives the natural join or equi-join:

σB=B(R×ρ{BB}(S))\sigma_{B = B'}(R \times \rho_{\{B \mapsto B'\}}(S))

This pattern is so common that SQL provides a dedicated JOIN ... ON syntax, and RA has the derived \bowtie (natural join) operator (see following note).

Combining product with renaming

When writing RA expressions involving product, the standard pattern is:

  1. Rename overlapping columns in one or both sub-queries with ρ\rho.
  2. Apply ×\times to the renamed sub-queries.
  3. Apply σ\sigma to select matching rows.
  4. Apply π\pi to remove duplicate columns.

Example. Find all pairs of students and lecturers in the same college:

πname,lname(σStudents.college=Lecturers.college(Students×ρ{namelname,  collegelcollege}(Lecturers)))\pi_{\text{name}, \text{lname}}(\sigma_{\text{Students.college} = \text{Lecturers.college}}(\text{Students} \times \rho_{\{\text{name} \mapsto \text{lname},\; \text{college} \mapsto \text{lcollege}\}}(\text{Lecturers})))

But this is verbose. The natural join \bowtie (next note) simplifies it.

Summary

  • R×SR \times S concatenates tuples; the result has R×S|R| \times |S| rows.
  • Column names of RR and SS must be disjoint; use ρ\rho when they overlap.
  • SQL: CROSS JOIN or comma-separated tables in FROM.
  • The product is typically combined with selection to form a join.