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

What is wrong with my SQL query?

I'm on Database Foundations Challenge Task 3 of 3. Here is my code:

SELECT title, IFNULL(AVG(score),0) AS average FROM reviews LEFT OUTER JOIN movies ON movies.id = reviews.movie_id GROUP BY movie_id HAVING average < 2;

Works fine on my machine. I get a "Bummer!" in the browser though.

1 Answer

Hi Jeffrey,

With a LEFT OUTER JOIN you have to reverse the order of the reviews and movies table.

movies LEFT OUTER JOIN reviews

The table that you place on the left will have all of its rows included but the table on the right will not necessarily have all of its rows included. Only the ones matching the ON clause.

You have to consider the possibility that the reviews table does not necessarily contain reviews for all movies but we want to make sure that all movies are returned in the results.

Thanks Jason! Yeah, I totally get it now. My mistake. Thanks for clearing that up!