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
Jonathan OsteenCourses 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?
Steven Parker229,018 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: "
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.
Jef Davis29,162 Points
I just completed this quiz question with the following:
SELECT * FROM Sale
(SELECT CustomerID FROM Customer WHERE Gender = 'F')
ON Sale.CustomerID = Female_Customer.CustomerID;
Seems like the only thing you and I have done differently is where I simply did "SELECT * FROM"