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

Having trouble with this Question?

In a car database there is a Sale table with columns, SaleID, CarID, CustomerID, LocationID, SalesRepID, SaleAmount and SaleDate and a Car table with columns, CarID, ModelID, VIN, ModelYear and StickerPrice.

Use a subquery along with IN to list all sales of cars with Sticker Price greater than $30000. Include all columns.

3 Answers

Joel Bardsley
Joel Bardsley
31,249 Points

Hi Sanora,

I'm not sure what you're having trouble with in particular, so I'll go through the challenge step-by-step. For anyone else reading this, this refers to challenge 2 of 4 here: https://teamtreehouse.com/library/querying-relational-databases/subqueries/subqueries

Firstly the challenge states to list all sales, including all columns:

SELECT * FROM Sale;

In the WHERE clause you need to include the IN operator. As instructed in the previous videos, the IN operator only accepts one column from the Car table we're performing the subquery from, so it needs to be a column that relates to both the Sale and the Car tables. As both tables have the CarID column, let's make use of that:

SELECT * FROM Sale
WHERE CarID IN (
  SELECT CarID FROM Car
);

Finally, you just need to filter the subquery by a StickerPrice greater than $30000:

SELECT * FROM Sale
WHERE CarID IN (
  SELECT CarID FROM Car
  WHERE StickerPrice > 30000
);

Hope that helps.

Sean M
Sean M
7,344 Points

SELECT * FROM Sale WHERE CarID IN (SELECT CarID FROM Car WHERE StickerPrice > 30000);

  1. Select all from sale
  2. specifying the car id from the car table
  3. specifying the car id to have a sticker price greater than 30,000

Thanks soo much... I was originally having trouble then realized it was because of a typo. I was typing in Sales instead of sale.