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

Jan Lundeen
Jan Lundeen
5,886 Points

Subqueries - Challenge 4 of 4 - still getting error

Hi,

I was working the questions for subqueries, Challenge 4 of 4. Here's the question:

In a car database there is a Sale table with columns, SaleID, CarID, CustomerID, LocationID, SalesRepID, SaleAmount and SaleDate and a Customer table with columns, CustomerID, FirstName, LastName, Gender and SSN. Use a subquery as a derived table to show all sales to female ('F') customers. Select all columns from the Sale table only

Here's my query: SELECT * FROM Sale AS s INNER JOIN(SELECT CustomerID FROM Customer WHERE Gender = ‘F’) AS c ON s.CustomerID = c.CustomerID;

I keep on getting an error (Bummer! There's something wrong with your SQL statement. Please review your code and try again). It wasn't very specific.

I looked at the other questions posted on this question in the community. It looks like my query should be correct. However, I'm probably overlooking something. Can you help me out with this?

Thanks,

Jan

Hi, Jan maybe try a left join instead of and inner join, as the instructions mention "... Sale table only". Hope this helps, D.

Jan Lundeen
Jan Lundeen
5,886 Points

Thanks! I tried LEFT JOIN and LEFT OUTER JOIN, but it didn't work. I got the following error message:

You're missing the INNER JOIN keywords.

Any other ideas?

Jan

5 Answers

jahid ali
jahid ali
2,311 Points

Hello Jan Lundeen,

Give this a go:

      SELECT * FROM sale AS s
           INNER JOIN (SELECT customerid FROM customer WHERE gender = 'F') AS c
           ON s.customerid = c.customerid;
Jan Lundeen
Jan Lundeen
5,886 Points

Thanks Jahid! That worked. The only difference between your query and mine was that customerid is lowercase. Weird stuff, but it worked.

Jan

Merci mille fois :)

Shouldnt there be a space between join & (select, below: "INNER JOIN(SELECT CustomerID

Other than that, I dunno and will have to leave it to the experts as I'm still a sql beginner : )

Jan Lundeen
Jan Lundeen
5,886 Points

Thanks for trying, but I'm not sure that's what's causing it.

Giovanni Esposito
Giovanni Esposito
7,830 Points

Hello Jan Lundeen,

Try using the aliase on SELECT * to indicate from wich table you require the columns, for example: SELECT s.* FROM Sale AS s INNER JOIN(SELECT CustomerID FROM Customer WHERE Gender = ‘F’) AS c ON s.CustomerID = c.CustomerID;

Jan Lundeen
Jan Lundeen
5,886 Points

Hi Giovanni,

Thanks for trying, but that doesn't work either.

Jan

SELECT * FROM sale AS s INNER JOIN (SELECT customerid FROM customer WHERE gender = 'F') AS c ON s.customerid = c.customerid;