SQL Why We Join Tables
What you will learn
- Why data is split over several tables
- What primary and foreign keys do (a quick recap)
- What happens when tables are combined with no condition
- The idea of a join condition
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.
1. One fact in one place
A database does not keep everything in one huge table. A name or a city is stored once, in the table about that thing, and other tables point to it with a key (Database Fundamentals, chapters 3 and 5 explain why). The table that is pointed to is the parent. The table that holds the pointer is the child.
In the Shop, customers is the parent and orders is the child: each order holds the customer_id of the customer who placed it.
In the Bank, customers is the parent and accounts is the child: each account holds the customer_id of its owner.
In the School, courses is the parent and sections is the child: each section holds the course_id of the course it teaches.
Look at a few rows of each, and at the column that links them:
SELECT customer_id, first_name, last_name FROM customers ORDER BY customer_id LIMIT 3;
customer_id first_name last_name
----------- ---------- ---------
1 Vik Ahmed
2 Wanda Ahmed
3 Dana Costa
(3 rows)
SELECT order_id, customer_id, order_date, status FROM orders ORDER BY order_id LIMIT 3;
order_id customer_id order_date status
-------- ----------- ---------- ---------
1 279 2025-04-23 delivered
2 414 2024-01-21 delivered
3 105 2025-05-03 delivered
(3 rows)
SELECT customer_id, full_name, city FROM customers ORDER BY customer_id LIMIT 3;
customer_id full_name city
----------- ----------- -------
1 Chen Park Lima
2 Rafi Uddin Manila
3 Grace Kamau Toronto
(3 rows)
SELECT account_id, customer_id, account_type, balance_cents FROM accounts ORDER BY account_id LIMIT 3;
account_id customer_id account_type balance_cents
---------- ----------- ------------ -------------
1 77 checking 192400
2 128 savings 52078
3 279 savings 62780
(3 rows)
SELECT course_id, code, title FROM courses ORDER BY course_id LIMIT 3;
course_id code title
--------- ------- ----------------------
1 MATH101 Foundations of Algebra
2 MATH201 Geometry
3 MATH301 Statistics
(3 rows)
SELECT section_id, course_id, term, room FROM sections ORDER BY section_id LIMIT 3;
section_id course_id term room
---------- --------- ----------- ----
1 1 2025-Autumn R04
2 2 2025-Spring R07
3 2 2025-Autumn R11
(3 rows)
The child holds only the key, not the parent's details. To see an order together with the customer's name, or a section with its course title, the two tables must be joined: put side by side on a matching key.
2. The primary key and the foreign key
- A primary key identifies each row of its own table: the id column of the parent table.
- A foreign key is a column in the child that holds the parent's key: the matching column of the child table (it has the same name here).
pragma_foreign_key_list (lesson 1.1) shows the links of a table:
SELECT "from" AS child_column, "table" AS points_to_table, "to" AS parent_column FROM pragma_foreign_key_list('orders');
child_column points_to_table parent_column
------------ --------------- -------------
employee_id employees employee_id
customer_id customers customer_id
(2 rows)
SELECT "from" AS child_column, "table" AS points_to_table, "to" AS parent_column FROM pragma_foreign_key_list('accounts');
child_column points_to_table parent_column
------------ --------------- -------------
branch_id branches branch_id
customer_id customers customer_id
(2 rows)
SELECT "from" AS child_column, "table" AS points_to_table, "to" AS parent_column FROM pragma_foreign_key_list('sections');
child_column points_to_table parent_column
------------ --------------- -------------
teacher_id teachers teacher_id
course_id courses course_id
(2 rows)
3. Combining with no condition
What happens if you combine two tables without saying how their rows belong together? SQL pairs every row of the first with every row of the second. This is a cross join, written with a comma or CROSS JOIN:
SELECT (SELECT COUNT(*) FROM customers) AS parent_rows,
(SELECT COUNT(*) FROM orders) AS child_rows,
(SELECT COUNT(*) FROM customers CROSS JOIN orders) AS every_pairing;
parent_rows child_rows every_pairing
----------- ---------- -------------
420 2600 1092000
(1 row)
SELECT (SELECT COUNT(*) FROM customers) AS parent_rows,
(SELECT COUNT(*) FROM accounts) AS child_rows,
(SELECT COUNT(*) FROM customers CROSS JOIN accounts) AS every_pairing;
parent_rows child_rows every_pairing
----------- ---------- -------------
300 450 135000
(1 row)
SELECT (SELECT COUNT(*) FROM courses) AS parent_rows,
(SELECT COUNT(*) FROM sections) AS child_rows,
(SELECT COUNT(*) FROM courses CROSS JOIN sections) AS every_pairing;
parent_rows child_rows every_pairing
----------- ---------- -------------
19 41 779
(1 row)
The last number is the first two multiplied. Almost none of those pairings is meaningful: a customer paired with every order, not just their own. A cross join is rarely what you want. Seeing the number explode is the best warning against forgetting the join condition.
A small cross join shows the idea: every row of the first table with every row of the second.
SELECT a.n AS first, b.letter AS second
FROM (SELECT 1 AS n UNION ALL SELECT 2) AS a
CROSS JOIN (SELECT 'x' AS letter UNION ALL SELECT 'y') AS b
ORDER BY first, second;
first second
----- ------
1 x
1 y
2 x
2 y
(4 rows)
SELECT a.n AS first, b.letter AS second
FROM (SELECT 1 AS n UNION ALL SELECT 2) AS a
CROSS JOIN (SELECT 'x' AS letter UNION ALL SELECT 'y') AS b
ORDER BY first, second;
first second
----- ------
1 x
1 y
2 x
2 y
(4 rows)
SELECT a.n AS first, b.letter AS second
FROM (SELECT 1 AS n UNION ALL SELECT 2) AS a
CROSS JOIN (SELECT 'x' AS letter UNION ALL SELECT 'y') AS b
ORDER BY first, second;
first second
----- ------
1 x
1 y
2 x
2 y
(4 rows)
4. The join condition
A real join adds a condition that says which rows belong together: the child's key equals the parent's key. Only the pairs that satisfy it are kept. Lesson 4.2 writes this with JOIN ... ON. As a preview, the same idea with a plain WHERE:
SELECT p.first_name, c.order_date
FROM customers AS p, orders AS c
WHERE c.customer_id = p.customer_id
ORDER BY c.order_id LIMIT 5;
first_name order_date
---------- ----------
Dana 2025-04-23
Ugo 2024-01-21
Jin 2025-05-03
Sara 2026-08-18
Bruno 2024-05-26
(5 rows)
SELECT p.full_name, c.account_type
FROM customers AS p, accounts AS c
WHERE c.customer_id = p.customer_id
ORDER BY c.account_id LIMIT 5;
full_name account_type
------------- ------------
Leo Kumar checking
Hana Kim savings
Vera Banerjee savings
Olga Garcia checking
Amir Singh checking
(5 rows)
SELECT p.code, c.term
FROM courses AS p, sections AS c
WHERE c.course_id = p.course_id
ORDER BY c.section_id LIMIT 5;
code term
------- -----------
MATH101 2025-Autumn
MATH201 2025-Spring
MATH201 2025-Autumn
MATH201 2026-Spring
MATH301 2025-Spring
(5 rows)
Each row of the child is now paired with its own parent only. The explicit JOIN ... ON form of the next lessons is clearer and is the one to use.
Worked example: reading a join in words
"Show each order with the name of the customer who placed it."
| Part of the sentence | In SQL |
|---|---|
| "each order" | the child table, orders |
| "the customer who placed it" | the parent table, customers |
| "who placed it" | the condition: orders.customer_id = customers.customer_id |
Two tables, one condition that matches the child's foreign key to the parent's primary key. The tables are made up.
Common mistakes
- No join condition, which multiplies the rows (a cross join).
- Joining on the wrong columns, for example two unrelated ids that happen to be numbers.
- Forgetting which table is the parent and which the child.
- Expecting the child to hold the parent's details. It holds only the key.
Key takeaways
- Data is split over tables; a child holds the key of its parent.
- A primary key identifies a row; a foreign key points to another table's primary key.
- Combining tables with no condition pairs every row with every row (rows multiply).
- A join condition keeps only the pairs where the keys match.
Practice
1. Which table is the child in "customers and their orders"?
Show answer
orders: it holds the customer_id that points to its customer.
2. A table has 50 rows and another has 20. How many rows does their cross join have?
Show answer
50 * 20 = 1000.
3. What does a foreign key hold?
Show answer
The primary key value of a row in another table.
4. Why is a missing join condition dangerous?
Show answer
Every row is paired with every row, so the result is huge and mostly meaningless.
5. What does the join condition compare?
Show answer
The child's foreign key with the parent's primary key.
6. How can you list the links of a table in SQLite?
Show answer
SELECT * FROM pragma_foreign_key_list('table_name');