Welcome to the Treehouse Community
Looking to learn something new?
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 Parker215,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: "
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 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"