Python Tutorials · Python MySQL

MySQL Join

Learn all about MySQL Join in this comprehensive tutorial.

5 min read advanced
  • You can combine rows from two or more tables, based on a related column between them, by using a JOIN statement.
  • In the example above, Hannah, and Michael were excluded from the result, that is because INNER JOIN only shows the records where there is a match.
  • If you want to return all products, and the users who have them as their favorite, even if no user have them as their favorite, use the RIGHT JOIN statement:

Join Two or More Tables

You can combine rows from two or more tables, based on a related column between them, by using a JOIN statement.

Consider you have a "users" table and a "products" table:

users

python

products

python

These two tables can be combined by using users' fav field and products' id field.

python
Note: Note: You can use JOIN instead of INNER JOIN. They will both give you the same result.

LEFT JOIN

In the example above, Hannah, and Michael were excluded from the result, that is because INNER JOIN only shows the records where there is a match.

If you want to show all users, even if they do not have a favorite product, use the LEFT JOIN statement:

python

RIGHT JOIN

If you want to return all products, and the users who have them as their favorite, even if no user have them as their favorite, use the RIGHT JOIN statement:

python
Note: Note: Hannah and Michael, who have no favorite product, are not included in the result.

Module quiz

2 questions
1

Which of the following is true about MySQL Join?

2

What is the most common pitfall when working with MySQL Join?

Answer all questions to submit.