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 trialGuilherme Ferreira
1,769 Pointswhere with aggregate functions
I'm receiving the message that the where clause is missing, but the task said to find the sum of a value, which tells me that I need to use the having keyword, right?
4 Answers
Steven Parker
231,269 PointsThis is an example of what I was talking about previously. The WHERE
clause will be used to determine which rows participate in the aggregate (just those for that particular movie). You don't need to include "movie_id" in the SELECT
clause, and you also won't need a GROUP BY
or a HAVING
.
Guilherme Ferreira
1,769 PointsThe link is: https://teamtreehouse.com/library/reporting-with-sql/aggregate-and-numeric-functions/summing-values
Task: 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.
That's the query I tried: SELECT SUM(rating) AS starman_total_ratings, movie_id FROM reviews GROUP BY movie_id HAVING movie_id = 6;
Guilherme Ferreira
1,769 PointsI tried: SELECT SUM(rating) AS starman_total_ratings FROM reviews WHERE movie_id = 6; but I'm still got an error that says "You should try writing a SELECT query".
Steven Parker
231,269 PointsLooks right to me. So I copied and pasted that query directly into the challenge ... and it passed. Try again?
Guilherme Ferreira
1,769 PointsNow it passed! Seens like a bug, there was a whitespace before the SELECT and that return an error