There are three simple tables. Actor
, Film
, Film_actor
.
Film_Actor
table:
actor_id | film_id
--------------+---------------------------
1 | 3
2 | 4
Actor
table:
actor_id | first_name | last_name
------------+-----------------------------+----------
1 | Daniel | Craig
2 | Eva | Green
Film
table:
film_id | title
-------------+---------------------------------------
3 | Casino Royale
4 | Colombo
I would like to find two actors who cast together the most and list the titles
of those movies. Moreover, actor_id
of the first_actor
should be lower than actor_id
of the second_actor.
So the desired result should look like this:
first_actor | second_actor | title
----------------+---------------+---------------
Daniel Craig | Eva Green | Casino Royale
I've written the following code:
SELECT
ac1.actor_id first_actor_id
, ac1.first_name + ' ' + ac1.last_name first_actor
, ac2.actor_id second_actor_id
, ac2.first_name + ' ' + ac2.last_name second_actor
,count(*) FilmCount
FROM
Actor as ac1
INNER JOIN Film_Actor as fa1 ON fa1.actor_id = ac1.actor_id
INNER JOIN Film_Actor as fa2 ON fa1.film_id = fa2.film_id
INNER JOIN Actor as ac2 ON fa2.actor_id = ac2.actor_id
and fa1.actor_id < ac2.actor_id
INNER JOIN Film as fi ON fa1.film_id = fi.film_id
GROUP BY
ac1.actor_id
, ac1.first_name
, ac1.last_name
, ac2.actor_id
, ac2.first_name
, ac2.last_name
In my view, the above code is a little bit clumsy as the code makes checking of this condition actor_id
of the first_actor
should be lower than actor_id
of the second_actor*` just for second actor.
Is it possible to improve comparing actor_id
of the first_actor
with actor_id
of the second_actor`?