MySQL INNER JOIN

INNER JOIN in MySQL returns only the matching rows in both the tables (i.e. returns only those rows for which the join condition satisfies).

To demonstrate INNER JOIN with extensive list of examples and visual representation let us create Customers and Orders tables as shown in the following image by executing the following script

mysql-joins-demo-tables

EXAMPLE 1: As per the data in our demo tables, Customers with CustomerId 1 and 2 in Customers table have the orders in the Orders table. Where as the customer with CustomerId 3 doesn’t have any order in the Orders table. So the INNER JOIN on the CustomerId column between Customers and Orders table will return the Customer and Order details of the Customers with CustomerId 1 and 2 only.

RESULT:
mysql-inner-join-example

EXAMPLE 2: Below Inner Join query demonstrates how to get name of all the Customer who have at-least one order in the Orders table.

RESULT:
mysql-inner-join-example-2-result

Leave a Reply

Your email address will not be published. Required fields are marked *