6. JOINS
Activity 6.01: Implementing JOINS
Solution:
- Look at the tables involved in obtaining and identifying the common columns. If you look closely at the following diagram, you will notice that the data that's required is scattered across not just two tables but three, that is,
Orders
,OrderItems
, andProducts
.In cases like these, we'll have to perform multiple joins, with the first join being between the
Orders
andOrderItems
table to get price details, and the second join being between theOrderItems
andProducts
table to get the product information. - In a new query window, implement this logic into the query:
SELECT Orders.OrderNumber, OrderItems.UnitPrice, OrderItems.Quantity, Products.ProductName FROM Orders JOIN OrderItems ON Orders.OrderID = OrderItems.OrderID JOIN Products ON OrderItems.ProductID = Products.ProductID
In this query, we joined the
Orders
andOrderItems
tables using theOrderID
and joined theProducts
andOrderItems
tables using...