Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!