Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Jan Lundeen5,731 Points
On SQL Playground, Stage 3, Practice, how do I calculate the average movie rating and round it to 1 decimal place?
In the Reporting in SQL class, I'm running into some issues with a question in the SQL Playground, Stage 3, Practice section. Here's the question:
Calculate the average rating for each movie and round it to one decimal place.
Here's my query:
SELECT AVG(rating) AS "average_rating", ROUND("average_rating", 1) FROM reviews;
Here's the results:
average_ rating = 3.8 ROUND(“average_rating”, 1) = 0
I think the average_rating is correct, but the rounded result is not. How do I calculate the average movie rating and round it to 1 decimal place?
Steven Parker218,707 Points
Did you want only one column?
In that case you'd want to enclose the average calculation inside the rounding function instead of listing them as two columns. So it might look like this:
SELECT ROUND(AVG(rating), 1) AS average_rating FROM reviews;
Although it will be a bit hard to confirm with that data set since you apparently get the same result even without rounding.
Hi, the task is asking to calculate EACH rating for EACH movie and round it to one decimal place. To get them all to display don't forget to use a GROUP BY!!
SELECT movie_id,ROUND(AVG(rating),1) AS movie_rating FROM reviews GROUP BY movie_id;