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

Nir Tal
Nir Tal
25,921 Points

sql sum - not sure why my answer is incorrect

the question: 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 for the movie "Starman" in the reviews table. Alias it as starman_total_ratings.

my answer: select SUM(rating) as starman_total_ratings from reviews group by movie_id having movie_id = 6

the result is starman_total_rating 10

the answer is correct but the response is that i'm missing a where clause

what am i doing wrong?

please help!

3 Answers

Patrik Horváth
Patrik Horváth
11,110 Points

there is no reason use having just read your task again and you will see :)

PS use uppercase next time its so hard read your code in no Standards :)

SELECT SUM(rating) AS starman_total_ratings FROM reviews WHERE movie_id = 6;

Hi there,

It's pretty much what the message said - it's looking for a where clause. You're using 'having' instead, which is generally used when you need to filter after select, but since you don't here, it's looking for a 'where' before your group by instead. In fact, since you're already going to use the 'where' to filter down to one movie, you don't need to group the results. I think it's probably looking for (separated for readability):

select 
   SUM(rating) as starman_total_ratings 
from reviews 
where movie_id = 6

Basically, your 'having' clause works for filtering and returns the data requested, but the challenge is looking specifically for 'where'. Generally speaking, 'where' can be more efficient (if you don't need to filter after select) because 'where' filters the data before select - you generally just want to use 'having' if you have a particular need to filter the data after your initial select is completed. This is generally in cases where you need to filter after you group the initial results. It won't matter here, but I thought I'd mention it because it can make a difference on a larger data set with a lot of rows.

In this example, with 'where' it will only return rows with an ID of 6, then it will add the ratings up. With having, it will return everything, group by the id, then filter down to only the sum for the specified id. In this case, 'where' gives you the same result without unnecessarily totaling up numbers that aren't needed.

I hope this was helpful!