Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Databases

Jonathan Osteen
PLUS
Jonathan Osteen
Courses Plus Student 4,439 Points

Why is my answer rejected? "ambiguous column name"

The prompt for SQL states:

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 tried the following:

SELECT SaleID, CarID, CustomerID, LocationID, SalesRepID, SaleAmount, SaleDate FROM Sale INNER JOIN(SELECT CustomerID FROM Customer WHERE Gender = "F") AS female_customers ON Sale.CustomerID = female_customers.CustomerID;

I get back the response: SQL Error: ambiguous column name: CustomerID That column exists in both tables, so I don't understand what is going on. Any ideas?

Thank you

2 Answers

Steven Parker
Steven Parker
215,984 Points

You said it yourself: "That column exists in both tables".

This issue arises from when you JOIN two (or more) tables with the same column name. So when you use that column name in your SELECT clause, you need to indicate which table you are displaying the value from. So you might say: "Sale.CustomerID".

One of the advantages of using subqueries in the WHERE clause instead of a JOIN, is that there will not be an opportunity for a name conflict.

Jonathan Osteen
Jonathan Osteen
Courses Plus Student 4,439 Points

Thank you Steven.

However, when I update the query to the following:

SELECT SaleID, CarID, Sale.CustomerID, LocationID, SalesRepID, SaleAmount, SaleDate FROM Sale INNER JOIN(SELECT CustomerID FROM Customer WHERE Gender = "F") AS female_customers ON Sale.CustomerID = female_customers.CustomerID;

Then it still tells me that I'm incorrect, explaining Your query didn't return all columns from the Sale table for who's CustomerIDs belong to people who identify as female!

I also don't understand what you are suggesting to do instead of using a JOIN. The course specifically covers handling subqueries in two different ways. One is to use the IN keyword, which the other three questions in the quiz propose, and other is to use a JOIN as a derived or temporary table. When I try to use the "IN", I get the right response, but the quiz marks it wrong and states specifically to not use the IN keyword.

For reference, you can see the Subqueries section of the cheatsheet provided in the teacher notes of this section of the course, posted here.

Thanks!

Steven Parker
Steven Parker
215,984 Points

You didn't provide a link to the course page that I could use to test with, but when I see the phrase "Select all columns from the Sale table only." my first inclination would be to write: "SELECT Sale.* FROM ...".

Jonathan Osteen
Jonathan Osteen
Courses Plus Student 4,439 Points

Indeed, rather than specify the columns, I need to just select *, but you can find the quiz here in any case.

Jef Davis
Jef Davis
29,162 Points

I just completed this quiz question with the following:

SELECT * FROM Sale
INNER JOIN
(SELECT CustomerID FROM Customer WHERE Gender = 'F') 
AS Female_Customer
ON Sale.CustomerID = Female_Customer.CustomerID; 

Seems like the only thing you and I have done differently is where I simply did "SELECT * FROM"

Jonathan Osteen
Jonathan Osteen
Courses Plus Student 4,439 Points

Yup, thanks Jefferson Davis, that does seem to be it. Of course it's something simple like that :-) Thanks!