Skip to content
Part IA Michaelmas Term

Natural Join

Definition

The natural join (denoted \bowtie) is the RA operator that combines two relations by equating columns with the same name and eliminating duplicate columns from the output.

Schema notation

When we write the schema of RR as R(A,B)R(A, B), we mean that the schema is R(AB)R(A \cup B) where AA and BB are disjoint sets of attributes. The notation splits the schema into parts for clarity: AA are the attributes unique to RR, BB are the shared attributes.

Formal definition

Given R(A,B)R(A, B) and S(B,C)S(B, C), where BB is the set of attributes shared by both relations, the natural join RSR \bowtie S is a relation over attributes ABCA \cup B \cup C defined as:

RS={tuR,  vS,  u.[B]=v.[B]t=u.[A]u.[B]v.[C]}R \bowtie S = \{t \mid \exists u \in R,\; v \in S,\; u.[B] = v.[B] \land t = u.[A] \cup u.[B] \cup v.[C]\}

That is: for every pair of tuples (u,v)(u, v) from RR and SS where the shared attributes match (u.[B]=v.[B]u.[B] = v.[B]), the result tuple tt combines uu‘s unique attributes (u.[A]u.[A]), the shared attributes (u.[B]u.[B], which equals v.[B]v.[B]), and vv‘s unique attributes (v.[C]v.[C]).

The shared attributes BB appear once in the result, not twice.

RA expression for natural join

The natural join can be expressed in terms of primitive RA operators:

RS=πA,B,C(σB=B(R×ρBB(S)))R \bowtie S = \pi_{A, B, C}\left(\sigma_{B = B'}\left(R \times \rho_{\vec{B} \mapsto \vec{B'}}(S)\right)\right)

The steps:

  1. Rename the shared columns in SS to distinguish them from those in RR: ρBB(S)\rho_{\vec{B} \mapsto \vec{B'}}(S).
  2. Take the Cartesian product: R×ρBB(S)R \times \rho_{\vec{B} \mapsto \vec{B'}}(S).
  3. Select rows where the shared columns are equal: σB=B()\sigma_{B = B'}(\cdots).
  4. Project to remove the duplicate columns: πA,B,C()\pi_{A, B, C}(\cdots).

The notation B=BB = B' abbreviates the conjunction of equalities on each attribute in BB: u.B1=v.B1u.Bn=v.Bnu.B_1 = v.B'_1 \land \cdots \land u.B_n = v.B'_n.

SQL translation

SQL provides NATURAL JOIN:

SELECT *
FROM R
NATURAL JOIN S;

This automatically equates all columns with the same name. More commonly, explicit joins are used:

SELECT *
FROM R
INNER JOIN S ON R.B = S.B;

Worked example

Students(name, sid, cid) — student name, ID, and the college ID they belong to.

namesidcid
Alicea1C1
Bobb1C2
Carolc1C1

Colleges(cid, cname) — college ID and college name.

cidcname
C1Churchill
C2King’s
C3Trinity

Students \bowtie Colleges:

namesidcidcname
Alicea1C1Churchill
Bobb1C2King’s
Carolc1C1Churchill

The join matches on the shared cid column. Alice and Carol are both matched to Churchill. Bob is matched to King’s. College C3 (Trinity) has no students, so it does not appear in the result. If a student had a cid with no matching college, that student would also be excluded.

Join variations in SQL

Beyond the natural join, SQL supports several join types. These go beyond core RA but are essential in practice:

Join typeSQL syntaxBehaviour
Inner joinINNER JOIN / JOINRows with matching values in both tables
Left outer joinLEFT JOINAll rows from left table; NULLs for unmatched right rows
Right outer joinRIGHT JOINAll rows from right table; NULLs for unmatched left rows
Full outer joinFULL JOINAll rows from both; NULLs where no match
Cross joinCROSS JOINCartesian product (no join condition)

When NULL values are present in the data, the behaviour of joins becomes more nuanced — see the textbook (Lemahieu 7.3.1.5) for details.

Join conditions beyond equality

The natural join only handles equality on shared columns. SQL generalises this with arbitrary join predicates:

SELECT *
FROM Employees e
JOIN Departments d ON e.dept_id = d.dept_id AND e.salary > d.budget;

In RA, a non-equality join must be expressed via product and selection:

σdept_id=dept_id    salary>budget(R×ρ{}(S))\sigma_{\text{dept\_id} = \text{dept\_id}' \;\land\; \text{salary} > \text{budget}}(R \times \rho_{\{\ldots\}}(S))

Summary

  • RSR \bowtie S equates shared columns and eliminates duplicates.
  • RA definition: πA,B,C(σB=B(R×ρBB(S)))\pi_{A,B,C}(\sigma_{B=B'}(R \times \rho_{\vec{B} \mapsto \vec{B'}}(S))).
  • SQL: NATURAL JOIN or INNER JOIN ... ON.
  • Outer joins (LEFT, RIGHT, FULL) preserve unmatched rows with NULLs.
  • Non-equality join predicates require product + selection in RA.