1
SELECT AVG(rating) FROM ratings WHERE ratings.movie_id = (SELECT id FROM movies where movies.year = 2012);

It doesnt seem to print anything.

| |
2

You did the hardest part, now it needs just one little improvement. If you write "ratings.movie_id = " program takes just one match. You should use "ratings.movie_id IN ", and then program takes all matches. Hope it helps. With code below output is 6.27545924967655

SELECT AVG(rating) FROM ratings WHERE movie_id IN (SELECT id FROM movies where 
movies.year = 2012)
| improve this answer | |
  • Thanks you! But even if it considers 1 match, shouldnt it print out the average. And SELECT title FROM movies WHERE movies.year=2008; Why doesnt this consider only 1 match? – Varun Beluguli Mar 19 at 11:56
  • IN - This helps when a subquery returns more than one result.. If you use = to subquery it cannot handle more than 1 result. Pay attention to the fact that if you write single query like "SELECT title FROM movies WHERE movies.year=2008 " you cannot use IN operator, if you did so you would have syntax error. The best way to get a grasp of it is just to play with it. – Bijacz Mar 19 at 12:53
  • Thanks a lot! Will explore! – Varun Beluguli Mar 19 at 15:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .