LESSON 26 / 62 · INNER JOININNER JOIN
Description and syntax of INNER JOIN
In the previous lesson, we looked at the general structure of a multi-table query:
MySQL 8.4
SELECT table_fields
FROM table_1
[INNER | {LEFT | RIGHT | FULL} [OUTER]] JOIN table_2
ON join_condition
[...]- [ ]optional part
- { a | b }one of the options
- ...can be repeated
The columns from any of the joined tables that go into the result.
Inner join: only rows that found a match get into the result. This is the default join type, so you can omit INNER.
Outer join: left, right or full. Besides the matched pairs, the result also gets the unmatched rows of the left, the right or both tables.
Only emphasizes that the join is outer. It goes after LEFT, RIGHT or FULL and does not change the result.
The table being joined. To join more tables, add a separate JOIN ... ON ... for each of them.
The rule by which rows of the two tables find each other, for example Payments.family_member = FamilyMembers.member_id.
- table_1
- The first table that the other tables are joined to.
- INNER
- KeywordOptional
- LEFT | RIGHT | FULL
- KeywordOptional
- OUTER
- KeywordOptional
When it comes to a multi-table query with an inner join, the general structure looks like this:
MySQL 8.4
SELECT table_fields
FROM table_1
[INNER] JOIN table_2
ON join_condition
[...]- [ ]optional part
- ...can be repeated
The columns from any of the joined tables that go into the result.
Inner join: only rows that found a match get into the result. This is the default join type, so you can omit INNER.
The table being joined. To join more tables, add a separate JOIN ... ON ... for each of them.
The rule by which rows of the two tables find each other, for example Payments.family_member = FamilyMembers.member_id.
- table_1
- The first table that the other tables are joined to.
- INNER
- KeywordOptional
For example, a query might look like this:
member_idINTgood_idINTgood_type_idINTpayment_idINT
SELECT family_member, member_name FROM Payments
INNER JOIN FamilyMembers
ON Payments.family_member = FamilyMembers.member_id| family_member | member_name |
|---|---|
| 1 | Headley Quincey |
| 2 | Flavia Quincey |
| 3 | Andie Quincey |
| 4 | Lela Quincey |
| 4 | Lela Quincey |
| 5 | Annie Quincey |
| 2 | Flavia Quincey |
| 2 | Flavia Quincey |
| 5 | Annie Quincey |
| 3 | Andie Quincey |
| 2 | Flavia Quincey |
| 1 | Headley Quincey |
| 3 | Andie Quincey |
| 3 | Andie Quincey |
Concept of inner join
An inner join is a type of join that returns only the rows that have matching values in both tables based on the join condition. It creates a new table that includes fields from both the first and second tables.
Visually, this looks like:

Since our condition specifies that Payments.good_id is equal to Goods.good_id, only the records where both tables have the same good_id value will be included in the resulting query.
Using WHERE to join tables
You can also use the WHERE operator to join tables internally. For example, the above query, written using INNER JOIN, will look like this:
SELECT family_member, member_name FROM Payments, FamilyMembers
WHERE Payments.family_member = FamilyMembers.member_id| family_member | member_name |
|---|---|
| 1 | Headley Quincey |
| 2 | Flavia Quincey |
| 3 | Andie Quincey |
| 4 | Lela Quincey |
| 4 | Lela Quincey |
| 5 | Annie Quincey |
| 2 | Flavia Quincey |
| 2 | Flavia Quincey |
| 5 | Annie Quincey |
| 3 | Andie Quincey |
| 2 | Flavia Quincey |
| 1 | Headley Quincey |
| 3 | Andie Quincey |
| 3 | Andie Quincey |
Python Academy
Practice
Solve it in the editor. The query runs against a sample database in your browser.
Write a query and run it to see the results.
Write a query and run it to see the results.
Write a query and run it to see the results.
This table is linked to Reservations (the table where you can get the room ID) by the reservation_id and Reservations.id fields.
Write a query and run it to see the results.