RienSQL
LESSON 26 / 62 · INNER JOIN

INNER 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_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
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:

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

Python Academy

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1INNER 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).
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 2Multi table INNER JOIN
Append the query from the previous assignment by adding another inner join to the Student table. Combine the Student_in_class.student and Student.id fields and print the student's name (first_name field) instead of the student ID.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 3Multi table INNER JOIN with filtration
Print the names of products that a family member with the "son" status bought. To get a selection, you need to join the Payments table with the FamilyMembers table for the family_member and member_id fields, and the Goods table for the good and good_id fields.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 4INNER JOIN with grouping
Print the identifier (the room_id field) and the average rating of the room (the rating field, for display use the avg_score alias), based on reviews from the Reviews table.
This table is linked to Reservations (the table where you can get the room ID) by the reservation_id and Reservations.id fields.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.