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 Subqueries Using a Subquery to Create a Temporary Table (Part 2)

Sharina Jones
PLUS
Sharina Jones
Courses Plus Student 18,771 Points

Query producing incorrect results

I've entered the following query, but the results I get don't match the video. I can't figure out where I've gone wrong. Any help would be greatly appreciated.

--combined as subqueries
SELECT sr.LastName, Loc1.StLouisAmount, Loc2.ColumbiaAmount FROM SalesRep AS sr
  LEFT OUTER JOIN(
    SELECT SalesRepID, SUM(SaleAmount) AS StLouisAmount
    FROM Sale AS s WHERE s.LocationID = 1
    GROUP BY SalesRepID
  ) AS Loc1 ON sr.SalesRepID = Loc1.SalesRepID
  LEFT OUTER JOIN(
    SELECT SalesRepID, SUM(SaleAmount) AS ColumbiaAmount
    FROM Sale AS s WHERE s.LocationID = 2
    GROUP BY SalesRepID
  ) AS Loc2 ON sr.SalesRepID = Loc1.SalesRepID;

Thanks.

2 Answers

Emmanuel C
Emmanuel C
10,636 Points

Hey Sharina,

The last line

 AS Loc2 ON sr.SalesRepID = Loc1.SalesRepID;

I believe should be

 AS Loc2 ON sr.SalesRepID = Loc2.SalesRepID;

Different temp table.

Jorgen Rasmussen
Jorgen Rasmussen
3,367 Points

Don't Feel Silly Sharina I just did the same thing and this question was very helpful