Understanding Joins
Build a visual mental model for matching rows from two SQLite tables with JOIN and ON.
A join combines rows from two tables into one result. You use it when one table has part of the answer and another table has the rest.
For example, orders knows the amount. customers knows the name. A join lets you ask: "show each order with the customer's name."
The basic shape
A join appears in the FROM part of a query. The ON clause says how rows match.
Read the ON line as: "match an order to a customer when the order's customer_id equals the customer's id."
The row-matching picture
A useful beginner mental model is cartesian then filter:
- Imagine every order paired with every customer.
- Test each pair with the
ONcondition. - Keep only the pairs where the condition is true.
You do not write a loop. SQLite chooses an efficient plan. But conceptually, ON is the test that decides which pairings become result rows.
That result shows every possible pairing. A normal join keeps only the rows where the ids line up.
What is the job of the ON clause in a join?
It chooses the final column names only.
It states the condition for deciding which rows from the tables match.
It permanently connects the two tables.
It sorts the final result.
Table aliases make joins readable
Aliases are short names you give tables inside one query. They make joined queries easier to read.
orders o means "use o as a short name for orders in this query." Prefixes also solve ambiguity: both tables may have a column named id, so o.id and c.id say exactly which one you mean.
Joins do not change your stored tables
A join creates a result table for the query. The original tables stay separate.
What if a row has no match?
The kind of join determines what happens. A regular JOIN is an inner join: it keeps only matches. Later, a LEFT JOIN will keep unmatched rows from one side too.
A join query returns a combined result. What happens to the original tables?
They are merged permanently.
They stay separate; the join result exists as the query's answer.
The right table is deleted.
The primary keys are removed.
Check your understanding
What does a join do?
It stacks two tables vertically even when their columns are unrelated.
It combines rows from tables by matching them with a condition.
It turns every text value into a number.
It creates a primary key automatically.
In FROM orders o JOIN customers c ON o.customer_id = c.id, what are o and c?
Permanent new table names.
Short aliases for the two tables inside this query.
Column values copied from the database.
Required SQLite keywords.
Which condition correctly matches orders to their customers?
orders.id = customers.id
orders.customer_id = customers.id
orders.amount = customers.id
orders.customer_id = customers.name
Why is "cartesian then filter" a helpful mental model?
It means you should always write CROSS JOIN in real queries.
It shows that SQLite considers possible row pairings and keeps the ones where the ON condition is true.
It proves joins can only use two-row tables.
It replaces the need for foreign keys.