mysqlsqlrankdense-rank

Rank actors with movies released in India based on their average ratings. Which actor is at the top of the list?


it looks something like this-- Note: The actor should have acted in at least five Indian movies. -- (Hint: You should use the weighted average based on votes. If the ratings clash, then the total number of votes should act as the tie breaker

SELECT n.name as actor_name 
     , r.total_votes
     , COUNT(r.movie_id) as movie_count
     , r.avg_rating as actor_avg_rating
     , RANK() OVER( PARTITION BY
        rm.category = 'actor'
        ORDER BY 
        r.avg_rating DESC
        ) actor_rank
  FROM names as n
  JOIN role_mapping as rm
    ON n.id = rm.movie_id
  JOIN movie as m
    ON m.id = rm.movie_id
  JOIN ratings as r
    ON r.movie_id = m.id
 where m.country regexp '^INDIA$' 
   and m.languages regexp '^HINDI$'
 group 
    by actor_name
 having count(rm.movie_id) >= 5; 

The output gives no error but no result too.

ERD Diagram


Solution

  • This would work:

    SELECT a.name as actor_name, c.total_votes, COUNT(c.movie_id) as movie_count,c.avg_rating as actor_avg_rating,
    RANK() OVER( PARTITION BY
                d.category = 'actor'
                ORDER BY 
                c.avg_rating DESC
                ) actor_rank
    FROM names a, movie b, ratings c, role_mapping d    
    where b.country = 'INDIA'
           and b.id = c.movie_id
           and b.id= d.movie_id
           and a.id = d.name_id
        
    group by actor_name
    having count(d.movie_id) >= 5
    order by actor_avg_rating desc
    ; 
    

    You had tried joining nameid with movie id which is the mistake