Google

Monday, October 8, 2007

What is Inner Join?

Assuming we have two tables, viz. Customers and Orders:



The relationship between them is one-to-many as indicated by the red arrow. The Customer Primary Key is inserted into the Order Table as a CustomerID field. This implies that each Order can have only one Customer. On the other hand, each Customer can have more than one order. Hence, one-to-many.

In order to display a table which shows which customer ordered what item, we need to join the two tables in a meaningful way. A possible solution is the following SQL Query:

SELECT Customers.CustomerID, Customers.CompanyName, Orders.OrderID, Orders.OrderDate
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID

This will yield:




Notice this syntax:

FROM Customers INNER JOIN Orders

It reads as "Join the Customers Table to the Orders Table".

The next condition is:

ON Customers.CustomerID = Orders.CustomerID

It reads as "based on CustomerID". And we know that Customer ID from Customer Table is inserted into Order's Table as a field under the CustomerID field. This is what makes the Join possible.