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.

Development Tools Database Foundations SQL Calculating, Aggregating and Other Functions Grouping, Joining and Cleaning Up

Samuel Altarac
Samuel Altarac
4,961 Points

help with grouping in MySQL

I can't get past the 3rd challenge question which asks:

Like before, select the average "score" as "average", setting to 0 if null, by grouping the "movie_id" from the "reviews" table. Also, do an outer join on the "movies" table with its "id" column and display the movie "title" before the "average". Finally, filter out any "average" score over 2.

I used the following code:

select title, ifnull(avg(score).0) as average from movies left outer join reviews on movies.id = reviews.movie_id group by movie_id having average < 2;

What am I doing wrong?

4 Answers

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

Kazimierz Matan
Kazimierz Matan
13,257 Points

I see a dot (.) in "ifnull(avg(score).0)", there should be a comma (,).

Generally, SQL in better readable if you write SQL keywords, functions and other language constructs as all uppercase. You can then easily distinguish them from data (table and column names etc.).

Did this end up working? I still am having issue as well with the following

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

Yuan Tang
Yuan Tang
14,168 Points

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