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 Subqueries Subqueries

Taylor Han
Taylor Han
2,406 Points

Subquery with SQL (Task 1)

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

What I tried:

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

Really stuck on these, I think I just need to see what the correct syntax looks like. Thank you!

2 Answers

Steven Parker
Steven Parker
230,178 Points

You've got the right idea, but:

  • the query only asks you to select the ModelName (not everything)
  • the Car table doesn't have a ModelName field

The common field both tables have is ModelID, try that for the WHERE and subquery.

Taylor Han
Taylor Han
2,406 Points

I made those fixes, thanks!

Not sure why this was so difficult. I was trying to inner join them and have both the model name and sticker prices listed.

A working solution is

select ModelName from Model WHERE ModelID IN (Select ModelID from Car where StickerPrice > 30000)