4.4.6 · D1Databases

Foundations — Joins — INNER, LEFT, RIGHT, FULL OUTER, CROSS, SELF

2,865 words13 min readBack to topic

This page assumes nothing. Before you can read the parent note Joins, you need to know exactly what a table, a row, a key, a NULL, a "match", and a handful of symbols mean. We introduce no symbol before defining it — every piece of notation gets a plain-words meaning and a picture in the section where it first appears, in the order the ideas depend on each other.


1. A table = a grid of facts

Figure — Joins — INNER, LEFT, RIGHT, FULL OUTER, CROSS, SELF

Look at the figure. The whole blue-bordered box is the Employees table. The horizontal strip highlighted in orange is one row — it is Asha, and everything true about Asha. The vertical strip in green is one column — the name property across all employees. Where they cross (the darker square) is one cell, holding the single value Asha.


2. Normalization — why the data is split in the first place

Figure — Joins — INNER, LEFT, RIGHT, FULL OUTER, CROSS, SELF

The figure shows the two worlds side by side. On the left (red) is the un-normalized table: "Sales" is copied onto every Sales employee — three copies of the same fact. On the right (green) is the normalized pair of tables: the word "Sales" appears once, and each employee stores only a tiny reference to it.


3. Keys — the values that let rows point at each other

Figure — Joins — INNER, LEFT, RIGHT, FULL OUTER, CROSS, SELF

In the figure, the arrow is the whole point. Employees.dept_id = 10 is a foreign key: it is not the department's name, it is a pointer to the department whose primary key dept_id = 10. The name "Sales" lives once, in Departments — exactly the normalization idea from section 2, now made concrete by the id.


4. NULL — the value that means "no value"

The single most surprising rule, which controls almost every join subtlety:


5. Two tables, three kinds of rows

Before any symbols, internalise this picture — it is the whole topic.

Two overlapping circles (a Venn diagram). The left circle is all Employees rows; the right is all Departments rows. They overlap where a foreign key finds its primary key.

  • Overlap (purple): rows that match — Asha ↔ Sales. These are the only rows an INNER JOIN keeps.
  • Left-only (blue): left rows with no partner — Bilal (dept 20 missing), Cara (dept NULL).
  • Right-only (orange): right rows with no partner — Legal (no employee).

Every join in the parent note is just a choice of which coloured regions to keep. Memorise the three regions and the six joins become trivial.


6. The symbols the derivation uses

The parent note's derivation writes joins in the language of Relational Algebra. Here is every symbol, earned one at a time, and then all six joins written in that language.

6a. — the Cartesian product (this IS the CROSS JOIN)

The grid in the figure is the product. Down the side: the 3 employees. Across the top: the 2 departments. Each of the inner cells is one pair — one possible combined row. This is the raw material a join filters. Notice: nothing has matched yet — every combination exists, even nonsensical ones like "Asha paired with Legal".

6b. — the join condition (predicate)

Why a symbol instead of the words? Because the shape of the join is the same whatever the condition is — equality, less-than, or a compound AND. Writing lets us describe all of them at once.

6c. — selection (filtering)

Now the INNER join reads in plain English:

"Take every employee–department pair (), then keep only the pairs where the ids match ()." That surviving set — the purple overlap in figure s03 — is the INNER JOIN. Nothing mysterious: product, then filter.

6d. Set literals and union — padding the lonely rows

With these, every outer join is INNER plus the lonely rows padded with :

The figure shades each join's kept regions on the same Venn picture so you can see that RIGHT is the exact mirror of LEFT (swap blue for orange), and FULL is simply both outer regions added to the INNER core.

6e. — cardinality (counting rows)

Now the parent's counting check is fully readable. With INNER , so , and so :


7. Aliases, dotted names, and the SELF JOIN


Prerequisite map

Table = grid of rows

Keys primary and foreign

Normalization stores each fact once

JOIN reconnects split data

NULL means absent

Matching never true for NULL

Cartesian product m times n

CROSS = the product itself

INNER = filter the product

Predicate theta

Selection sigma

Outer joins pad with NULL via union

Set literal NULL

Union combines sets

Aliases and dotted names

SELF = table crossed with its copy

Six joins INNER LEFT RIGHT FULL CROSS SELF

Related deeper topics once you're comfortable: Indexes (how the engine avoids materialising the full product), GROUP BY and Aggregation (what you often do after joining), and Foreign Keys (which guarantee matches exist).


Equipment checklist

Cover the right side; can you answer each before reading on?

What is a row versus a column?
A row is one thing (all facts about one entity); a column is one property shared across all things.
What does normalization do, and why does it force joins to exist?
It stores each fact in one place; the resulting split means related data lives in separate tables, so a join is needed to reconnect it.
What is a foreign key?
A column that stores a primary-key value belonging to another table — a pointer to a row elsewhere.
Is NULL the same as 0 or ""?
No — NULL means absent/unknown; it is a distinct marker, not a value.
What does NULL = NULL evaluate to?
UNKNOWN — not true. You must use IS NULL to test for it.
Why does an INNER JOIN drop a row whose key is NULL?
The match condition uses =, and NULL = anything is never true, so the row finds no partner.
What does (Cartesian product) produce and how many rows?
Every pairing of a row from with a row from ; rows.
Which join is exactly with no filter?
CROSS JOIN.
In , what do and mean?
= selection (keep rows passing the test); = the matching predicate/condition.
Write INNER JOIN in relational-algebra words.
— form all pairs, then keep only those satisfying the condition.
What do the braces in mean?
A set literal — the one-row set containing a single all-NULL stand-in row, used to pad the missing side.
Write RIGHT and FULL in terms of INNER, , .
; .
What does do in the outer-join formulas?
Union — combines the matched pairs with the padded lonely rows into one result set.
What does mean?
The number of rows (cardinality) in .
How is a SELF JOIN written in relational algebra?
— the same table crossed with a relabelled copy of itself, then filtered.
Why are table aliases essential in a self join?
They give the single table two distinct names so a.col and b.col refer to different copies unambiguously.