MySQL: Inner Join

Profile picture for user arilio666

Now if we wanna join another table into a current working table with a common column as reference INNER JOIN is the thing for you. To put it simply when we want to combine columns from table A into the column from table B that is where we use this. The INNER word is optional we don't have to type that to represent inner join. Now there are many other joins but in this article, we will be focusing more on the inner join concept.

Syntax

SELECT * FROM TableName1 JOIN TableName2 ON TableName1.commoncolumn = TableName2.commoncolumn;

So the main concept here is to join a table we have to look for the basis of the merge like a common column like id column to latch on to the other table so that reason we are using ON.

So enough syntax discussion and more real-time goodies.

Let us take a live table example where we will be doing the following things.

For this, we will be using the customer's table and orders table and be joining the column based on customer_id.

Here is the Order and Customers table.

Order Table And Customers Table Join:

SELECT order_id, o.customer_id, first_name, last_name from orders o JOIN customers c ON o.customer_id = c.customer_id;

  • So here we have joined two tables based on customer_id from both tables and said that it should be equal with ON clause and also named alias to both the tables and called them.
  • The reason we are using o.customer_id in the select place is that we have two table's customer_id and MySQL doesn't know which one to pick so we have to give the respective customer_id column while selecting.
Tags