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

Erin DeSpain
Erin DeSpain
6,763 Points

Impossible Results? "The subquery didn't return all CustomerIDs for people who identify as female (F)."

Question Content:

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 Sale AS s
INNER JOIN (
    SELECT Gender FROM Customer WHERE Gender = 'F') AS c
ON c.CustomerID = s.CustomerID

Response:

Bummer: The subquery didn't return all CustomerIDs for people who identify as female (F).


Question:

Where did things go wrong? NOTE: I've also tried entering the Sale Fields individually to prevent the normal duplication of fields one gets with a join. Consequence: same exact result.

1 Answer

Since you are joining CustomerID you will want to select CustomerID instead of Gender in your subquery.

SELECT * FROM Sale AS s
INNER JOIN (
    SELECT CustomerID FROM Customer WHERE Gender = 'F') AS c
ON c.CustomerID = s.CustomerID
Erin DeSpain
Erin DeSpain
6,763 Points

Thank you! That was perfect!