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

PHP

Database Foundations stage 7 problem

I'm stuck on the second step of the code challenge.

The question is to display the average movie review scores higher then 2 and group them by movie ID. When I do this query:

SELECT movie_id, AVG(score) as average FROM reviews GROUP BY movie_id HAVING average >= 2

The results are all nicely grouped by the movie ID. However the average scores are all lower then 0.

| movie_id | average |

| 1 | 0.28372E1 |

| 2 | 0.30732E1 |

| 3 | 0.38333E1 |

etc...

Am I missing something here?

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Sander;

This challenge has a long history of causing people some confusion. Here is a sample of the history.

Now, here is what the task is asking us to do:

Like before, group reviews by "movie_id", get the average "score" as "average" and filter out any averages over 2.

So first, your SQL filter statement is including the movie_id in the results, which the task is not requesting. Not sure if that is a make or break for this challenge necessarily, but it could through an error.

Secondly, the SQL statement which is filtering the average has the >= sign which is providing the incorrect results. We want to filter out movies which have an average score over 2, so they would have an average of under 2.

So, putting those two issues together, leaves us with a SQL statement like:

# Course: Database Foundations
# Module: SQL Calculating, Aggregating and Other Functions
# Challenge: Grouping, Joining, and Cleaning Up
# Task: 2 of 3:

SELECT AVG(score) AS average FROM reviews GROUP BY movie_id HAVING average <= 2;

I hope that makes some sense and helps.

Happy coding,

Ken

Thanks for your help Ken,

I was filtering out the wrong movie reviews with average >= 2 instead of average <= 2.