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
177,727 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
177,727 Points

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