Joins Introduction
Understand why joins exist and how MySQL combines rows from two or more related tables into a single result.
Introduction
So far, every query in this course has worked with a single table. But real databases almost always split data across multiple related tables — one for students, one for courses, one for enrollments — to avoid repeating information and to keep data organized. A join is how you bring that related data back together in a single query.
This lesson is a conceptual overview. You will set up two related tables that will be reused across the next several lessons, each of which covers one specific join type in depth.
- Why data is split across multiple related tables in the first place.
- How a shared key, like a foreign key, links two tables together.
- How to picture matching rows between two tables.
- A preview of the join types covered in upcoming lessons.
Why Joins Exist
Imagine storing every student's enrolled course name directly in the students table. If a course name changes, or a student enrolls in multiple courses, this quickly becomes messy and repetitive. Instead, relational databases split related information into separate tables and connect them using a shared key — typically a foreign key that refers to another table's primary key.
A join is the SQL operation that reverses this split at query time: it combines rows from two or more tables into one result, matched together based on that shared key, so you can ask questions that span multiple tables at once.
Think of two spreadsheets — one listing students, and another listing course enrollments with only a student ID column. A join is like using VLOOKUP to pull each student's name into the enrollments sheet, matched by that shared ID.
Setting Up Two Related Tables
Let's create a "students" table and an "enrollments" table that references it. These two tables will be used again in the next several lessons as we cover each join type in detail.
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE enrollments (
id INT PRIMARY KEY,
student_id INT,
course_name VARCHAR(50),
FOREIGN KEY (student_id) REFERENCES students(id)
);
INSERT INTO students (id, name) VALUES
(1, 'Alex'),
(2, 'Priya'),
(3, 'Sam');
INSERT INTO enrollments (id, student_id, course_name) VALUES
(1, 1, 'Mathematics'),
(2, 1, 'Physics'),
(3, 2, 'Chemistry');Notice that Sam (id 3) has no matching rows in enrollments, and that student_id in the enrollments table is the shared key that links each enrollment back to a specific student.
How Matching Works
When you join students to enrollments, MySQL looks at every row in students and every row in enrollments, and pairs them together wherever students.id equals enrollments.student_id. Rows that share the same key value are combined side by side into one wider result row.
| students.id | students.name | enrollments.student_id | enrollments.course_name | |
|---|---|---|---|---|
| 1 | Alex | 1 | Mathematics | |
| 1 | Alex | 1 | Physics | |
| 2 | Priya | 2 | Chemistry | |
| 3 | Sam | (no match) | (no match) |
Notice that Alex appears twice in the matched result — once for each course they are enrolled in — because a join produces one output row for every matching pair. Sam has no enrollment rows to match, which is exactly the kind of case different join types (covered next) handle differently.
A First Join Example
Here is a simple join combining both tables. Don't worry about the exact JOIN syntax yet — the very next lesson covers INNER JOIN in full detail. For now, just notice how the two tables are combined into one result using the shared student_id key.
SELECT students.name, enrollments.course_name
FROM students
JOIN enrollments ON students.id = enrollments.student_id;+-------+-------------+
| name | course_name |
+-------+-------------+
| Alex | Mathematics |
| Alex | Physics |
| Priya | Chemistry |
+-------+-------------+Notice that Sam does not appear in this result at all, since Sam has no matching row in enrollments. This behavior is specific to the join type used here, and is exactly what the upcoming lessons will explain in depth.
Types of Joins You Will Learn Next
MySQL supports several distinct kinds of joins, each with different rules for which unmatched rows are kept or dropped. The next several lessons cover each one using the same students and enrollments tables set up in this lesson.
| Join Type | What It Does |
|---|---|
| INNER JOIN | Keeps only rows that have a match in both tables. |
| LEFT JOIN | Keeps all rows from the left table, with NULLs where there is no match on the right. |
| RIGHT JOIN | Keeps all rows from the right table, with NULLs where there is no match on the left. |
| CROSS JOIN | Combines every row from one table with every row from the other, with no matching condition. |
| SELF JOIN | Joins a table to itself, useful for comparing rows within the same table. |
Common Mistakes
- Assuming all join types behave the same way for unmatched rows — they do not.
- Joining on the wrong pair of columns, which produces incorrect or meaningless matches.
- Forgetting that a shared key does not need the same column name in both tables, just the same underlying value.
- Not checking for unmatched rows (like Sam in this lesson) before assuming a join returned "everything."
Best Practices
- Always identify the shared key between two tables before writing a join.
- Sketch out, even mentally, which rows should and should not match before running the query.
- Use clear table aliases once queries involve multiple tables (covered further in later lessons).
- Learn each join type's behavior with unmatched rows — it is the single biggest source of join confusion.
Frequently Asked Questions
Do the two tables need a formal FOREIGN KEY constraint to be joined?
No. A join only requires that the values logically match — a foreign key constraint is good practice for data integrity, but MySQL does not require it to perform a join.
Can more than two tables be joined at once?
Yes, a single query can join three, four, or more tables together, though this course builds up to that gradually starting with two tables.
What happens if I just list two tables without any join condition?
That produces a CROSS JOIN by default, pairing every row of one table with every row of the other, which is rarely what you want unless done intentionally.
Which join type should I learn first?
INNER JOIN, covered in the very next lesson, is the most commonly used join type and the best starting point.
Key Takeaways
- Joins combine rows from two or more related tables into a single result.
- A shared key, often a foreign key, is what links rows between tables.
- Rows are matched wherever the key values are equal.
- Different join types handle unmatched rows differently.
- The next several lessons will each cover one join type using the same students and enrollments tables.
Summary
Joins exist because relational databases spread related data across multiple tables, and a join is how you bring that data back together based on a shared key.
In this lesson, you learned why joins exist, set up a students and enrollments table pair you will reuse throughout the next several lessons, and previewed the specific join types ahead. Next, you will dive into the INNER JOIN in full detail.
- You understand why joins exist and what problem they solve.
- You can picture how rows are matched between two related tables.
- You are ready to learn the INNER JOIN in depth.