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

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

Can anyone point out where I'm going wrong on my MySQL command because I cannot see it anywhere

I'm hoping someone can point out where it is I'm going wrong with this MySQL command because I can't see it myself. The goal is the following:

"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."

and here is my MySQL command:

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

If anyone can point out where the problem is I'd be very grateful

Thanks guys

6 Answers

Alan Johnson
Alan Johnson
7,625 Points

I see one more issue in your answer. Note that the directions for this step of the code challenge ask you to put the value of a particular column before the average.

It's displaying the title before the average but it's still saying there's something wrong. Here's 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"

Alan Johnson
Alan Johnson
7,625 Points

Check your conditional in the group by. You want movies with averages that aren't greater than 2.

Do I need to add a WHERE clause? I've tried changing the '>' operator to '<' but nothing shows up. I'm really sorry if I'm beginning to annoy you

Alan Johnson
Alan Johnson
7,625 Points

Not annoying me at all. Now that you changed the operator to <, what's the full query that you're using for the answer?

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

Alan Johnson
Alan Johnson
7,625 Points

So looking at the answer key (thank goodness I have access to that), the only difference I see is the order of the joins. The answer I have says movies LEFT OUTER JOIN reviews rather than vice versa.

I just had a chat with Andrew Chalkley about this, and we're going to dig in a little to see if we can make this better very soon, just because I too hit some challenges in completing this.

Thanks so much for that Alan! Much appreciated :) I hope you can make it better soon

Alan Johnson
Alan Johnson
7,625 Points

Thank you, Joe! Were you able to complete the code challenge?

Yes I was thank god haha! :D

Right Answer( the key was in the RIGHT instead of LEFT):

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

Alan Johnson
Alan Johnson
7,625 Points

Ooh. I see one more issue in your query. Look at what you're joining to reviews:

FROM reviews LEFT OUTER JOIN reviews ON movies.id = reviews.movie_id

Ahh yess. I've changed LEFT OUTER JOIN reviews to movies now. I'm getting there, it's showing the actual table, but it says "Bummer! There's something wrong with your query"

Hi again, it's not showing the title column before the average column. This is what I'm trying to fix now

George Abood
George Abood
3,160 Points

I'm still a little baffled, here's what I thought:

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;

I'm sure I'm missing something.

Alan Johnson
Alan Johnson
7,625 Points

It looks like you're missing a comma inside of IFNULL:

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

Nope, that doesn't work. I got this error "63386dbc-34c9-4ddf-8794-8cd71eb3cdbf.rb:38:in eval': undefined methodto_a' for "SQL Error: Not unique table/alias: 'reviews'":String (NoMethodError) from 63386dbc-34c9-4ddf-8794-8cd71eb3cdbf.rb:38:in eval' from 63386dbc-34c9-4ddf-8794-8cd71eb3cdbf.rb:38:in"

Big spoilers, but it sounds like this was a tough one that maybe has some bugs, so here goes!

What worked for me was this (without the line breaks I included to make it look nice in the forum):

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

The thing that kept tripping me up was that I kept putting a space like this, which messed up the IFNULL:

 IFNULL (AVG(score)

Anyway, it put the challenge in code challenge! Maybe this would be a good one to make as a multistep one, like in some of the HTML/CSS challenges?

Michael Day
Michael Day
10,101 Points

Poor code challenge and hope they fix this soon its been 5 months now.

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

Hey Michael,

Can you be more specific? What queries are you entering in to make it fail?

Regards
Andrew