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

Guilherme Ferreira
Guilherme Ferreira
1,769 Points

where 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
Steven Parker
231,127 Points

This 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
Guilherme Ferreira
1,769 Points

The 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
Guilherme Ferreira
1,769 Points

I 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
Steven Parker
231,127 Points

Looks right to me. So I copied and pasted that query directly into the challenge ... and it passed. Try again?

Guilherme Ferreira
Guilherme Ferreira
1,769 Points

Now it passed! Seens like a bug, there was a whitespace before the SELECT and that return an error