Summary: In this tutorial, you will learn how to use MySQL INNER JOIN clause to select data from multiple tables based on join conditions.
Introducing MySQL INNER JOIN clause
The MySQL INNER JOIN clause is an optional part of SQL SELECT statement. The MySQL INNER JOIN clause appears immediately after the FROM clause. Before using MySQL INNER JOIN clause, you have to specify the following criteria:
- First, you need to specify the tables you want to join with the main table. The main table appear in the FROM clause. The table you want to join with appear after keyword INNER JOIN. Theoretically, you can join a table with unlimited number of tables. However, for better performance you should limit the number of tables to join based on join conditions and volume of data in those tables.
- Second, you need to specify the join condition or join predicate. The join condition appears after the keyword ON of MySQL INNER JOIN clause. The join condition is the rule for matching rows between the main table and other tables being joined with.
The syntax of the MySQL INNER JOIN is as follows:
1 |
SELECT column_list |
2 |
FROM t1 |
3 |
INNER JOIN t2 ON join_condition1 |
4 |
INNER JOIN t3 ON join_condition2 |
5 |
... |
6 |
WHERE where_conditions; |
For example, if you join two tables A and B, the MySQL INNER JOIN clause compares each record of the table A with each record of table B to find all pair of records that satisfy the join-condition. When the join-condition are satisfied, column values for each matched pair of record of table A and table B are combined into a returned record. Note that the records on both tables have to match based on the join-condition. If no record on both table A and B matches, the query will return an empty result.
Avoid column ambiguous error in MySQL INNER JOIN
If you join multiple tables that has column with similar name, you have to use table qualifier to refer to column to avoid column ambiguous error. Suppose if table tbl_A and tbl_B has the same column M. In the SELECT statement with MySQL INNER JOIN clause, you have to refer to column M by using the table qualifier as tbl_A.M or tbl_B.M (table_name.column_name).
Another effective way to avoid column ambiguous is by using table alias. For example, you can give A as the table alias of the table tbl_A and refer to the column M as A.M so you don’t have to type again and again the long table name in your SQL statement.
Example of MySQL INNER JOIN clause
Let’s take a look at two tables: products and orderDetails in our sample database.

The products table is the master data table that stores all products. Whenever a product is sold, it is stored in the orderDetails table with other information. The link between products table and orderDetails table is productCode.
Now, if you want to know what product was sold in which order, you can use the MySQL INNER JOIN clause as follows:
1 |
SELECT A.productCode, A.productName, B.orderNumber |
2 |
FROM products A |
3 |
INNER JOIN orderDetails B on A.productCode = B.productCode; |

There are more returned rows that are not listed on this screenshot
The MySQL INNER JOIN clause compares each row of table products and orderDetails table to find a pair of rows that has the same productCode. If a pair of rows that have the same, the product code, product name and order number are combined into a returned row.
In this tutorial, you have learned how to use MySQL INNER JOIN to select data from multiple tables. You have also learned how to use table qualifier to avoid column ambiguous error in MySQL INNER JOIN clause.
Related posts:
