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

James Head
James Head
18,872 Points

Querying Relational Databases SubQueries Question Help

Im stuck on question 3/4 in the Querying Relational Databases SubQueries course.

The 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 along with IN to list all sales to female customers. (Gender = 'F') Select all columns."

My answer is:

"SELECT * FROM Sale WHERE SaleID IN (SELECT CustomerID FROM Customer WHERE Gender = 'F');"

However I'm getting the following error:

"Your query didn't return all columns from the Sale table for who's CustomerIDs belong to people who identify as female!"

Please could someone help!

Thank you

2 Answers

Umesh Ravji
Umesh Ravji
42,386 Points
SELECT * FROM Sale WHERE **SaleID** IN (SELECT CustomerID FROM Customer WHERE Gender = 'F');

You are obtaining a collection of CustomerID's from the subquery, but then you are selecting customers from the Sale table based on SaleID when you should be selecting them based on CustomerID

James Head
James Head
18,872 Points

Great, thank you Umesh.

Sean M
Sean M
7,344 Points

SELECT * FROM Sale WHERE CustomerID IN (SELECT CustomerID FROM Customer WHERE Gender = 'F');

  1. Select all from sale table
  2. Specifying customer id from the customer table
  3. specifying the gender be female.

note: a single letter/char is represented in single quotation: 'F'

Kevin Gates
Kevin Gates
15,053 Points

This, but with Markdown

SELECT * FROM Sale WHERE CustomerID IN
(SELECT CustomerID FROM Customer WHERE Gender = 'F');