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

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

I need help with this question, I have tried several variations of this code and either get the error that I'm not getting all of the model ids (which it doesn't ask for so assuming that's an issue with my join) or a blanket error that the code is off.

Select Modelname from Model Inner join Car ON car.modelid=model.modelid 
Where stickerprice IN (Select stickerprice from car where stickerprice>30000);

4 Answers

Steven Parker
Steven Parker
231,271 Points

You nearly had it on your 3rd try. The trick is to make the subquery select the column that both tables have in common. This is also the same column that the first WHERE clause is comparing to.

Select Modelname from Model Where ModelID IN (Select ModelID from car where stickerprice>30000);
--                                                   ^^^^^^^

hey Mark can you explain a bit more please? i'm not understanding why we have to do a subquery vs just giving the condition of 'where stickerprice>30000' furthermore I don't understand "So change that to select modelID", what are you suggesting to change? Thanks

I am now getting this error: SQL Error: no such column: StickerPrice

I have tried the below three codes: Select Modelname from Model Where StickerPrice IN (Select modelid from car where stickerprice>30000); Select Modelname from Model Where modelid IN (Select stickerprice from car where stickerprice>30000);

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 38,297 Points

Hi Zaal. Couple of things here. First you don't need an inner join. The question asks to use a sub query so that leaves us with:

Select Modelname from Model
Where stickerprice IN (Select stickerprice from car where stickerprice>30000);

so in the subquery you are selecting sticker price? is that what we want? We want models with sticker price over 30000. So change that to select modelID and compare with modelID outside the subquery and it should work

Good luck and keep at it!