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 trialAdiv Abramson
6,919 PointsMy query returned results but I got a warning that something's wrong with my query
Can someone explain what I did wrong here with my query?
Sergey Podgornyy
20,660 PointsIs it a task from some quiz?
4 Answers
Steven Parker
231,261 PointsYou are correct that the GROUP BY clause must have all of the fields in the SELECT clause except for aggregate functions.
But... there's no requirement that the SELECT clause have all of the fields in the GROUP BY clause (though the results may seem a bit silly without them).
Adiv Abramson
6,919 PointsYes. It's from the Database Foundations series. Thank you.
Adiv Abramson
6,919 PointsHere is the problem statement:
Group all reviews by "movie_id" and get the average "score" and alias it as "average".
Sergey Podgornyy
20,660 PointsTry this code:
SELECT AVG(score) AS average FROM reviews GROUP BY movie_id;
Brandon McClelland
4,645 PointsIt sounds like you don't need to include the select movie_id piece. Doing so would cause the id to show up in your results, but the task is only asking to show the average.
Adiv Abramson
6,919 PointsThank you for your response. I will try that. But I thought that the GROUP BY clause had to have all of the fields in the SELECT clause except for aggregate functions. So I thought, if I want to group by movie_id, I must include it in the SELECT clause and in the GROUP BY clause. I seem to recall Microsoft Access displaying alerts to that effect on numerous occasions when I composed queries in that application.
Adiv Abramson
6,919 PointsAdiv Abramson
6,919 PointsI checked the box to add my code to this posting but that didn't work.
My query is
SELECT movie_id, AVG(score) AS average FROM reviews GROUP BY movie_id;