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

Help with Challenege 1 of 4

SELECT Model.ModelID, ModelName, StickerPrice FROM Model INNER JOIN Car ON Model.ModelID = Car.ModelID WHERE Car.CarID IN(SELECT Car.CarID FROM Car WHERE StickerPrice > 30000);

What Challenge are you referring to? Is it the same one referenced here? https://teamtreehouse.com/community/bummer-as-answer-when-gives-correct-result-in-sql-playground

With databases, without knowing what your querying and why makes it impossible to help.

4 Answers

Joel Bardsley
Joel Bardsley
31,249 Points

For others accessing this question, it relates to the Code Challenge here: https://teamtreehouse.com/library/querying-relational-databases/subqueries/subqueries


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

Use a subquery along with IN to list all the Model Names with a Sticker Price greater than $30000


Firstly, you just need to list the Model Names:

SELECT ModelName FROM Model

As it's a subquery as part of an IN statement, you don't need to join the two tables. You can connect the ModelID columns for both the Model and Car tables as follows:

SELECT ModelName FROM Model
WHERE ModelID IN (
  SELECT ModelID FROM Car
)

The WHERE clause contains the ModelID from the Model table and the subquery contains the ModelID from the Car table. Lastly you just need to filter the subquery by the StickerPrice:

SELECT ModelName FROM Model
WHERE ModelID IN (
  SELECT ModelID FROM Car
  WHERE StickerPrice > 30000
);

THANK YOU!!! It was soo simple but it felt so complicated. Youre a Savior brother!

It's a play of words. I knew that the "Sticker price" would confuse people! Thanks for the answer.

Joel McCracken Jr
Joel McCracken Jr
3,142 Points

Thank you Other Joel =+)

I was over thinking this as well!!! And had not yet understood that they were ONLY wanting to see the ModelNames and not to also see the StickerPrice

Sean M
Sean M
7,344 Points

SELECT ModelName FROM Model WHERE ModelID IN (SELECT ModelID FROM Car WHERE StickerPrice > 30000);

  1. Selecting the model name from the model table.
  2. specifying the model id from the car tables
  3. specifying even further the model id that has a sticker price greater than 30,000

Thank you very much for the correct answer!I am grateful! And Iยดve learnt something new as well. Many greetings from Bernt