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 Reporting with SQL Aggregate and Numeric Functions Calculating Averages

I don't understand why i can't user GROUP BY and HAVING in this example.

We're in a movie database. There's a reviews table with the columns of id, movie_id, username, review and rating. The movie "Starman" has the id of 6. Movie ids are found in the movie_id column in the reviews table. Write a query that totals up all ratings in the reviews table. Alias it as starman_total_ratings.

My solution to this was:

SELECT SUM(rating) AS starman_total_ratings FROM reviews GROUP BY movie_id HAVING movie_id = 6;

1 Answer

Steven Parker
Steven Parker
229,785 Points

:point_right: Using GROUP BY is more complicated than needed for this task.

Plus, some databases do not allow you to use GROUP BY unless the grouping term is also part of the SELECT clause.

But for returning a single result as in this case, a simple WHERE can replace both the GROUP BY and the HAVING, which is what the challenge is looking for.