Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Jonatan Spahn
Jonatan Spahn
6,362 Points

Multiple Inner Joins

I'm trying to get the following information from a database: Order #, CustomerName, ProductName, and Quantity.

I've been able to get make inner joins to get the Order # and CustomerName or ProductName and Quantity but I'm having trouble getting them all to show up. Here is my code

SELECT OrderID, CustomerName, ProductName, Quantity FROM OrderDetails
INNER JOIN Orders ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID;

Here is the link to the database I'm using.

https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all

Thank you!

You need to either alias your table names (and their subsequent fields) or be explicit in the field names in the select statement since more than one field has orderID in it:

SELECT Orders.OrderID, CustomerName, ProductName, Quantity 
FROM OrderDetails
INNER JOIN Orders ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID 
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID;
Jonatan Spahn
Jonatan Spahn
6,362 Points

Thank you to both of you for the help!

1 Answer

Steven Parker
Steven Parker
231,248 Points

You were really close!

Since more than one joined table contains a field named OrderID you have to qualify which one you are displaying in the SELECT clause:

SELECT Orders.OrderID, CustomerName, ProductName, Quantity
FROM OrderDetails
INNER JOIN Orders ON OrderDetails.OrderID = Orders.OrderID
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID;