Sub topic 6
Joins: theta, equi, natural and outer
This chapter assumes you know selection (σ), projection (π), the set operations, Cartesian product (×) and rename (ρ). A join combines two relations by pairing tuples that satisfy a condition. Every join below is a shorthand built from those operators, except outer joins, which need an extra step.
Theta join
A theta join pairs each tuple of R with each tuple of S and keeps the pairs that satisfy a condition θ:
R ⋈_θ S = σ_θ (R × S)θ can use any comparison: =, ≠, <, ≤, >, ≥. The result has all attributes of both relations, so attribute names must be distinct. Rename first if they are not.
For example, Student ⋈_(Student.age > Senior.age) Senior pairs students with older seniors.
Size bounds: the result has between 0 and |R|·|S| tuples. The maximum occurs when θ is true for every pair.
Equi-join
An equi-join is a theta join where θ is only a conjunction of equalities, such as R.B = S.B. Both columns stay in the result, so R.B and S.B appear side by side with identical values.
Natural join
The natural join R ⋈ S has no explicit condition. It does three things:
- It equates every attribute name that R and S share.
- It keeps only one copy of each shared attribute.
- It returns the remaining attributes of both relations.
Formally, if the shared attributes are B₁…Bₖ:
R ⋈ S = π_(attributes of R ∪ attributes of S) ( σ_(R.B1 = S.B1 ∧ … ∧ R.Bk = S.Bk) (R × S) )Two edge cases appear often in GATE questions:
- If R and S share no attributes, the condition is empty and R ⋈ S equals R × S.
- If R and S have identical schemas, R ⋈ S equals R ∩ S.
A tuple with a NULL in a shared attribute never matches anything, because NULL = NULL is not true. Such tuples disappear from an inner join.
Worked example
Use these instances:
| Student sid | sname |
|---|---|
| 1 | Asha |
| 2 | Ben |
| 3 | Cy |
| Enrolled sid | cid |
|---|---|
| 1 | 10 |
| 1 | 11 |
| 3 | 10 |
| 4 | 12 |
Student ⋈ Enrolled matches on sid. Asha matches twice and Cy once. Ben has no enrollment, and the tuple (4, 12) has no student. The result has three tuples: (1, Asha, 10), (1, Asha, 11) and (3, Cy, 10).
Outer joins
An inner join drops tuples that find no partner. An outer join keeps them and pads the missing side with NULL.
- Left outer join (⟕) keeps every tuple of the left relation.
- Right outer join (⟖) keeps every tuple of the right relation.
- Full outer join (⟗) keeps both.
On the example:
| Join | Extra tuples beyond the 3 inner results | Total |
|---|---|---|
| Student ⟕ Enrolled | (2, Ben, NULL) | 4 |
| Student ⟖ Enrolled | (4, NULL, 12) | 4 |
| Student ⟗ Enrolled | both of the above | 5 |
A full outer join is the union of the left and right outer joins. The inner join is a subset of all three.
The same joins in SQL
This script creates the example tables and runs the inner and outer joins. It runs in PostgreSQL or SQLite, except that SQLite supports RIGHT and FULL joins only from version 3.39.
CREATE TABLE Student (sid INT, sname VARCHAR(20));
CREATE TABLE Enrolled (sid INT, cid INT);
INSERT INTO Student VALUES (1, 'Asha'), (2, 'Ben'), (3, 'Cy');
INSERT INTO Enrolled VALUES (1, 10), (1, 11), (3, 10), (4, 12);
SELECT * FROM Student NATURAL JOIN Enrolled; -- 3 rows
SELECT * FROM Student s LEFT OUTER JOIN Enrolled e
ON s.sid = e.sid; -- 4 rows
SELECT * FROM Student s FULL OUTER JOIN Enrolled e
ON s.sid = e.sid; -- 5 rowsCounting join results
GATE often asks for the minimum and maximum size of a join. Let R(A, B) have m tuples and S(B, C) have n tuples.
| Situation | Natural join R ⋈ S size | Column | Column |
|---|---|---|---|
| No restriction | 0 to m·n | ||
| No shared attribute | exactly m·n | ||
| B is a key of S | at most m | ||
| B is a key of S, and B in R is a non-null foreign key to S | exactly m | ||
| Same schema | R ∩ S | , at most min(m, n) |
The maximum m·n occurs when every tuple of R and S has the same B value. The minimum 0 occurs when the B values are disjoint.
Common errors
- Treating a natural join as a join on columns of the same position or type. It uses attribute names only.
- Forgetting that a natural join silently becomes a Cartesian product when no names are shared.
- Expecting NULL keys to match each other in a join.
- Putting a filter on the right table in
WHEREafter a left outer join. This removes the padded rows and turns the query back into an inner join. Put the condition inONinstead.
Exercise
R(A, B) has 20 tuples and S(B, C) has 5 tuples. B is the primary key of S, and B in R is a non-null foreign key referencing S. What are the sizes of R ⋈ S, the left outer join R ⟕ S, and the full outer join R ⟗ S?
Show answer
Every tuple of R has a non-null B that matches exactly one tuple of S, so R ⋈ S has exactly 20 tuples. No tuple of R is unmatched, so R ⟕ S also has 20. The full outer join adds the S tuples that no R tuple references. That number is between 0 and 5, so R ⟗ S has between 20 and 25 tuples.
Exercise
R(A, B) has 6 tuples and S(C, D) has 4 tuples. What is the size of R ⋈ S, and what is the size if S is renamed to S(B, D)?
Show answer
R and S share no attribute names, so R ⋈ S is the Cartesian product with 6·4 = 24 tuples. After the rename the shared attribute is B, so the size is between 0 and 24, depending on the B values.