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 trialdrstrangequark
8,273 PointsI thought you couldn't use a WHERE clause when using the SUM keyword?
The challenge is:
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 query:
SELECT SUM(review) AS "starman_total_ratings" FROM reviews GROUP BY movie_id HAVING movie_id = 6;
I get the error: You're missing the WHERE clause. However I thought that you COULDN'T use a WHERE clause with a SUM keyword and that was the whole point of using the HAVING keyword. Plus, when I try to replace the HAVING keyword with WHERE, it also returns an error.
4 Answers
markmneimneh
14,132 PointsHello
Try this. If you want to use Where, the Where clause needs to come before group by.
SELECT SUM(review) AS "starman_total_ratings"
FROM
reviews
WHERE movie_id = 6
GROUP BY movie_id;
Hope this answers this question
Paul Leenheer
Courses Plus Student 11,720 PointsHi guys,
The correct code is the following:
SELECT SUM(rating) AS "starman_total_ratings" FROM reviews WHERE movie_id = 6 GROUP BY movie_id;
It was one little mistake. Only the review must be replaced by rating.
Greetings,
Dylan
drstrangequark
8,273 PointsNevermind. I'm an idiot. I was getting the sum of Review when I should have been getting the sum of Rating
markmneimneh
14,132 PointsHello
I do same type of mistakes all the time. Glad you got it resolved.
drstrangequark
8,273 PointsThanks!
drstrangequark
8,273 Pointsdrstrangequark
8,273 PointsThanks for the response. When I tried your query I got back "Your query didn't perform the correct sum calculation."