Databases Querying Relational Databases Joining Table Data with SQL JOIN Queries

David Corrales
David Corrales
4,646 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

Adam Pengh
Adam Pengh
28,898 Points
SELECT
  M.ModelName,
  C.VIN,
  C.StickerPrice
FROM Car C
INNER JOIN Model M ON M.ModelId = C.ModelId
KRIS NIKOLAISEN
KRIS NIKOLAISEN
53,693 Points

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;