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

Hans Eisenman
Hans Eisenman
1,315 Points

Having a little trouble with this derived table query. Error I'm getting is just "Bummer: try again!" :-\

This is my first derived table query and I'm not sure what I'm doing wrong. Would appreciate any help.

Hans Eisenman
Hans Eisenman
1,315 Points

Hmm, not seeing my code or the quiz question here in this request so I'll just post it here in case you can't see it either:

Assignment: 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.

My code:

select *
from
    (select *
    from sale s
    inner join customer c ON s.customerid = c.CustomerID
    ) as G
where gender = 'f'

1 Answer

Steven Parker
Steven Parker
231,269 Points

You don't need to join the entire Customer table here. The instructions say they want only the "Sale" table columns, so the only thing you need from the Customer table is the CustomerID to join on:

SELECT * FROM Sale AS s
INNER JOIN (SELECT CustomerID FROM Customer WHERE Gender = 'F') AS c
  ON s.CustomerID = c.CustomerID;
Hans Eisenman
Hans Eisenman
1,315 Points

Ok, makes sense. Thank you!

Steven Parker
Steven Parker
231,269 Points

Hans Eisenman — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!