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 trial

Databases Reporting with SQL Aggregate and Numeric Functions Practice Session

Jonatan Spahn
Jonatan Spahn
6,362 Points

User Average Rating, a little more advanced using subqueries

So I'm in the SQL Playground for the Aggregate and Numeric Functions (practice sessions) in the Reporting with SQL section.

What I would like to do is a join which states all movies that have been rated that are not SCI Fi. I was able to state the movies that have been rated here is the code:

SELECT title As Movie_Title, ROUND(AVG(rating),1) As Average_Rating FROM movies INNER JOIN reviews ON movies.ID = reviews.movie_id GROUP BY movie_id;

So I would like to narrow this and return movies that are not Sci Fi, I was thinking a subqury would do the trick but I can't figure it out. Here is what I have tried so far...

SELECT title As Movie_Title, ROUND(AVG(rating),1) As Average_Rating FROM movies IN (SELECT title FROM movies WHERE genre != "Sci Fi") INNER JOIN reviews ON movies.ID = reviews.movie_id GROUP BY movie_id;

SELECT title As Movie_Title, ROUND(AVG(rating),1) As Average_Rating FROM movies WHERE genre != "Sci Fi" INNER JOIN reviews ON movies.ID = reviews.movie_id GROUP BY movie_id;

1 Answer

Steven Parker
Steven Parker
231,272 Points

You don't need a sub-query.

Your WHERE clause will do the trick, but you have to place it correctly. It should come after the JOIN but before the GROUP BY.