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 trialJan Svensson
10,005 PointsJoining Tables With SubQueries
The Following Question is :
"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."
I seem to be getting the exact result that I want with my code but it doesn't pass. Can anybody see what is wrong?
3 Answers
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHey Jan, which code challenge is this? Without knowing that I can only guess as to what the SQL would be:
SELECT *
FROM "Sale"
WHERE "CustomerID" IN (SELECT "CustomerID" FROM "Customer" WHERE "Gender" = 'F')
Ken Alger
Treehouse TeacherJan;
Can you post the query you are using?
Thanks,
Ken
Jan Svensson
10,005 PointsCertainly!
SELECT Sale.* FROM Sale INNER JOIN (SELECT * FROM Customer WHERE Gender = 'F') AS Customer ON Sale.CustomerID = Customer.CustomerID
Brian Vancil
3,301 PointsI tried the following query, which it also didn't like:
SELECT s.* FROM Sale AS s INNER JOIN (SELECT CustomerID FROM Customer WHERE Gender = 'F') AS c ON s.CustomerID = c.CustomerID;
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHey Brian, did you try your query with all lower case letters for table and column names? I wonder if you have to use quotations when mixing capitalized letters in table and column names.
Jan Svensson
10,005 PointsJan Svensson
10,005 PointsHey Chris, It is the very last one in "Querying Relational Databases".
My query was something like you posted. I've commented mine in the comment above.
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsChris Jones
Java Web Development Techdegree Graduate 23,933 PointsThanks, Jan. I think you're query works, but I think the code challenge just wants you to use the IN keyword.
Kudos for coming up with your own solution, though, and thinking outside the box:). Nothing wrong with that. I think the IN keyword is just more conventional.
Jan Svensson
10,005 PointsJan Svensson
10,005 PointsThe IN keyword was used in the assignment right before this one, but now they want to do it with a 'derived table'. But I agree, the IN keyword solution seems much more conventional in this case :)
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsChris Jones
Java Web Development Techdegree Graduate 23,933 PointsOh, sorry about that.
This query passed:
I'm not sure why it wouldn't take your query. I just watched the "Using a Subquery to create a temporary table (part 1) to see exactly what they did and copied that. Maybe the code challenge wanted a very specific syntax? I wonder if you naming the derived table "Customer" caused confusion because there is already a "Customer" table that you're basing the derived table on.
Let me know if you have any more questions!
Jan Svensson
10,005 PointsJan Svensson
10,005 PointsThanks a lot Chris! Yes, it seems like it could have been the naming that caused the problem.
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsChris Jones
Java Web Development Techdegree Graduate 23,933 PointsNot a problem! Glad I could help!