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 trialJan Lundeen
5,887 PointsOn SQL Playground, Stage 3, Practice, how do I calculate the average movie rating and round it to 1 decimal place?
Hi,
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?
Thanks,
Jan
3 Answers
Steven Parker
231,275 PointsDid 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.
Charlie Harcourt
8,046 PointsHi, 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;
Jan Lundeen
5,887 PointsYou're right. That would work. Thanks!
Jan
Jan Lundeen
5,887 PointsJan Lundeen
5,887 PointsHi Steven,
I tried to put it in one column, but I got a syntax error. When I tried to combine the columns, I used AVG first and ROUND second [e.g. SELECT AVG(ROUND(rating, 1)) AS "average_rating" FROM reviews;]. It looks like I mixed up the order of the commands (should be ROUND, AVG instead of AVG, ROUND) and probably didn't get the parentheses placed correctly.
Originally, when I wrote this question, I listed "average_rating" = 3.8 on one line and ROUND(“average_rating”, 1) = 0 on the second line. It looks like the results were listed on one line instead. I agree. It will be hard to confirm with that data set.
Thanks for your help!
Jan
Steven Parker
231,275 PointsSteven Parker
231,275 PointsWell, you could round the average to 0 places and it should return 4.
Remember when nesting functions, the innermost is performed first, and the outermost is performed last. Just the opposite of peeling an onion.