Databases

david hollaway
david hollaway
13,106 Points

SUM function() help

https://teamtreehouse.com/library/reporting-with-sql/aggregate-and-numeric-functions/summing-values Here's 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.

Type in your command below. Bummer! Your query didn't perform the correct sum calculation.

SELECT SUM(rating) AS starman_total_ratings, rating FROM reviews WHERE movie_id = "6" GROUP BY rating;

What am I doing wrong?

3 Answers

david hollaway
david hollaway
13,106 Points

Thanks. This worked:

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

Steven Parker
Steven Parker
94,464 Points

I see 3 potential issues:

  • the movie_id is a numeric field, but you're comparing it with a string
  • the instructions only ask for the sum, the rating column itself should probably not be returned
  • you probably don't need "GROUP BY"

If that's not everything, provide a link to the course page to enable a more complete analysis.

Steven Parker
Steven Parker
94,464 Points

david hollaway — Glad to help. You can mark the question solved by choosing a "best answer".
And happy coding!