select a.*,COUNT(Distinct c.actor_id) as countOfAllOtherActorsInAllHisFilms
from actor a
left join film_actor b ON a.actor_id = b.actor_id
left join film_actor c ON ( b.film_id = c.film_id AND c.actor_id != a.actor_id)
WHERE 1
GROUP BY a.actor_id
ORDER BY countOfAllOtherActorsInAllHisFilms DESC
in case you’d like to filter out set of films:
select a.*,COUNT(Distinct c.actor_id) as countOfAllOtherActorsInAllHisFilms
from actor a
left join film_actor b ON a.actor_id = b.actor_id
left join film_actor c ON ( b.film_id = c.film_id AND c.actor_id != a.actor_id)
left join film d ON b.film_id=d.film_id
WHERE d.rating='G'
GROUP BY a.actor_id
ORDER BY countOfAllOtherActorsInAllHisFilms DESC
edit: I added keyword distinct
to counting function. Without it, an actor will be counted two times if they appear in two films together.
2
solved Which actors have worked with the greatest numbers of other actors in the set of films observed in the data?