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.

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
218,708 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.