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 Summing Values

bummer: you're missing the where cause

with the SUM function and GROUP BY keyword there shouldn't be a WHERE cause but a HAVING cause, right?

3 Answers

Gabriel Plackey
Gabriel Plackey
11,064 Points

I'm not sure where you would be using GROUP BY in this challenge, as you don't need to group anything. Regardless you can use SUM and GROUP BY with the WHERE clause. Like

SELECT SUM(numbers), id, name FROM table WHERE condition GROUP BY name

It can cause errors depending on what is in the table and how it is structured but it is possible. AS for this challenge, the correct answer is

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

You can actually have both in the same SQL statement. WHERE is used to filter records before any groupings take place. HAVING is used to filter values after they have been grouped. Only columns or expression in the group can be included in the HAVING clause’s conditions.

In this case movie_id isn't being grouped. It is being filtered to a specific movie_id.

ah great, makes sense! Many thanks :)