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

David Corrales
David Corrales
4,698 Points

Oh my gosh this stuff is getting tough! Subqueries are challenging anyone know how to solve this?

Challenge Task 1 of 4 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

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

Bummer: There's something wrong with your SQL statement. Please review your code and try again.

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

4 Answers

SELECT column FROM T1 WHERE ID IN (SELECT ID FROM T2 WHERE condition);
David Corrales
David Corrales
4,698 Points

It worked PERFECTLY! Who are you?! You should be famous! Coding genius!

Some hints:

  • No need for the join since you will be using a subquery instead.

  • I would use a key in the subquery - the common field between the two tables.

  • You only need to select the ModelName in the main query

David Corrales
David Corrales
4,698 Points

Hmm, I'm still having a bit of a hard time, would you be able to provide an example of the correct way to do it perhaps?

David Corrales
David Corrales
4,698 Points

Wow you're literally so fast in the world! Thanks so much! I'll try it!

Oh my god. I had the worst trouble with this and I was making it so complicated >.< Thank you!