Databases

david hollaway
david hollaway
14,475 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?

2 Answers

david hollaway
david hollaway
14,475 Points

Thanks. This worked:

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

Steven Parker
Steven Parker
105,320 Points

david hollaway — Glad I could help. But normally, you would choose the answer that helped resolve the issue as "best answer". Choosing your own response might be confusing to someone reading the question laster.

Steven Parker
Steven Parker
105,320 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.