RianAcademy
Search
⌘
K
Premium

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
table_fields

The columns from any of the joined tables that go into the result.

table_1
The first table that the other tables are joined to.
INNER

Inner join: only rows that found a match get into the result. This is the default join type, so you can omit INNER.

KeywordOptional
LEFT | RIGHT | FULL

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.

KeywordOptional
OUTER

Only emphasizes that the join is outer. It goes after LEFT, RIGHT or FULL and does not change the result.

KeywordOptional
table_2

The table being joined. To join more tables, add a separate JOIN ... ON ... for each of them.

join_condition

The rule by which rows of the two tables find each other, for example Payments.family_member = FamilyMembers.member_id.

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
table_fields

The columns from any of the joined tables that go into the result.

table_1
The first table that the other tables are joined to.
INNER

Inner join: only rows that found a match get into the result. This is the default join type, so you can omit INNER.

KeywordOptional
table_2

The table being joined. To join more tables, add a separate JOIN ... ON ... for each of them.

join_condition

The rule by which rows of the two tables find each other, for example Payments.family_member = FamilyMembers.member_id.

For example, a query might look like this:

FamilyMembers
member_id
INT
status
VARCHAR
member_name
VARCHAR
birthday
DATETIME
Goods
good_id
INT
good_name
VARCHAR
type
INT
GoodTypes
good_type_id
INT
good_type_name
VARCHAR
Payments
payment_id
INT
family_member
INT
good
INT
amount
INT
unit_price
INT
date
DATETIME
MySQL 8.4
SELECT family_member, member_name FROM Payments
INNER JOIN FamilyMembers
    ON Payments.family_member = FamilyMembers.member_id
family_membermember_name
1Headley Quincey
2Flavia Quincey
3Andie Quincey
4Lela Quincey
4Lela Quincey
5Annie Quincey
2Flavia Quincey
2Flavia Quincey
5Annie Quincey
3Andie Quincey
2Flavia Quincey
1Headley Quincey
3Andie Quincey
3Andie 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:

Concept of Inner Join

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:

MySQL 8.4
SELECT family_member, member_name FROM Payments, FamilyMembers
    WHERE Payments.family_member = FamilyMembers.member_id
family_membermember_name
1Headley Quincey
2Flavia Quincey
3Andie Quincey
4Lela Quincey
4Lela Quincey
5Annie Quincey
2Flavia Quincey
2Flavia Quincey
5Annie Quincey
3Andie Quincey
2Flavia Quincey
1Headley Quincey
3Andie Quincey
3Andie Quincey
Tasks for self-testing
0 / 4
1. INNER JOIN

Join the Class and Student_in_class tables using an inner join on the Class.id and Student_in_class.class fields. Print the class name (Class.name field) and student ID (Student_in_class.student field).
Class
id
INT
name
VARCHAR
Schedule
id
INT
date
DATE
class
INT
number_pair
INT
teacher
INT
subject
INT
classroom
INT
Student
id
INT
first_name
VARCHAR
middle_name
VARCHAR
last_name
VARCHAR
birthday
DATE
address
VARCHAR
Student_in_class
id
INT
class
INT
student
INT
Subject
id
INT
name
VARCHAR
Teacher
id
INT
first_name
VARCHAR
middle_name
VARCHAR
last_name
VARCHAR
Timepair
id
INT
start_pair
TIME
end_pair
TIME
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Result
Class
Schedule
Student
Student_in_class
Subject
Teacher
Timepair

Run a query to see the result, or open a table above