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 Joining Table Data with SQL JOIN Queries

Tracy Cano
Tracy Cano
6,645 Points

Can someone help me understand how to answer this question about outer joins?

Challenge Task 5 of 5 In a car database there is a Sale table with columns, SaleID, CarID, CustomerID, LocationID, SalesRepID, SaleAmount and SaleDate. The database also has a SalesRep table with columns, SalesRepID, FirstName, LastName, SSN, PhoneNumber, StreetAddress, City, State and ZipCode.

Show all SaleDate, SaleAmount, and SalesRep First and Last name from Sale and SalesRep. Make sure that all Sales appear in results even if there is no SalesRep associated to the sale.

1 Answer

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Data Analysis Techdegree Graduate 49,443 Points

Hey there Tracy Cano ! :wave:

For this one, we need to utilize an OUTER JOIN to make sure we're showing all Sales, even if there is not an associated SalesRep for the row.

We'll want to specify what we want to select from both tables. I decided to put the Sale table on the "left" as it makes more sense in my head. Then LEFT OUTER JOIN the other table where the IDs match up.

I hope this makes sense! :smiley:

SELECT SaleDate, SaleAmount, FirstName, LastName
FROM Sale
LEFT OUTER JOIN SalesRep ON Sale.SalesRepID = SalesRep.SalesRepID