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 Querying Relational Databases Joining Table Data with SQL JOIN Queries

David Corrales
David Corrales
4,698 Points

I asked an earlier question regarding the same challenge, but if possible, can you help me with this one?

In a car database there is a Make table with columns, MakeID and MakeName, a Model table with columns, ModelID, MakeID and ModelName and a Car table with columns, CarID, ModelID, VIN, ModelYear and StickerPrice.

For all cars in the database, show Make Name, Model Name, VIN and Sticker Price from the Model and Car tables in one result set.

Type in your command below, then press Ctrl-Enter.

Bummer: Your query didn't select the MakeName, ModelName, VIN and StickerPrice!

SELECT mk.MakeName, md.ModelName, ca.VIN, ca.StickerPrice FROM Make AS mk INNER JOIN Model AS md ON md.MakeID = md.ModelID INNER JOIN Car as ca ON ca.ModelID = ca.ModelID;

2 Answers

You are using the same tables in your joins with md.MakeID = md.ModelID AND ca.ModelID = ca.ModelID. A corrected version is as follows:

SELECT mk.MakeName, md.ModelName, ca.VIN, ca.StickerPrice
FROM Make mk
INNER JOIN Model md ON mk.MakeID = md.MakeID
INNER JOIN Car ca ON ca.ModelID = md.ModelID; 
Adam Pengh
Adam Pengh
29,881 Points
SELECT
  M.ModelName,
  C.VIN,
  C.StickerPrice
FROM Car C
INNER JOIN Model M ON M.ModelId = C.ModelId