1

I need to create a view that contains the id and names of actors that are in a drama film that released in 2010.

I wrote this:

CREATE VIEW DRAMA_ACTORS 
AS 
    SELECT 
        ACTOR_ID, FIRST_NAME, LAST_NAME
    FROM 
        ACTOR, FILM_ACTOR, FILM, FILM_CATEGORY, CATEGORY
    WHERE 
        ACOTR.ACTOR_ID = FILM_ACTOR.ACTOR_ID
        AND FILM_ACTOR.FILM_ID = FILM.FILM_ID
        AND FILM.FILM_ID = FILM_CATEGORY.FILM_ID
        AND FILM_CATEGORY.CATEGORY_ID = CATEGORY.CATEGORY_ID
        AND CATEGORY.NAME = 'Drama'
        AND FILM.RELEASE_YEAR = '2010';

The schema is like this:

enter image description here

Thanks for any help

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
cdl2333
  • 11
  • 2
  • 2
    [Bad habits to kick : using old-style JOINs](https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins) - that old-style *comma-separated list of tables* style was replaced with the *proper* ANSI `JOIN` syntax in the ANSI-**92** SQL Standard (**more than 20 years** ago) and its use is discouraged – marc_s Oct 21 '15 at 04:43
  • Which DBMS are you using? Postgres? Oracle? –  Oct 21 '15 at 06:52
  • Do SELECT DISTINCT to avoid duplicates (if an actor has been in several movies that year.) Do you store years as text?!? Don't do that, use integer! – jarlh Oct 21 '15 at 06:57

2 Answers2

1

You might want to use JOIN like this:

SELECT a.ACTOR_ID, a.FIRST_NAME, a.LAST_NAME
FROM FILM AS f
JOIN FILM_ACTOR AS fa ON fa.FILM_ID = f.FILM_ID
JOIN ACOTR AS a ON a.ACTOR_ID = fa.ACTOR_ID
JOIN FILM_CATEGORY AS fc ON fc.FILM_ID = f.FILM_ID
JOIN CATEGORY AS c ON c.CATEGORY_ID = fc.CATEGORY_ID
WHERE c.NAME = 'Drama' AND f.RELEASE_YEAR = '2010'

If you got duplicate results, use DISTINCT or GROUP BY in your query.

jhmt
  • 1,401
  • 1
  • 11
  • 15
0

It's better (In my opinion) to avoid creating duplicates in the first place, rather than to eliminate them afterwards.

If we want to identify actors who've been in a drama in 2010, it shouldn't matter if they've been in multiple films - we just want to assert that at least one such film exists:

SELECT 
    ACTOR_ID, FIRST_NAME, LAST_NAME
FROM 
    ACTOR a
WHERE
    EXISTS (SELECT *
       FROM FILM_ACTOR fa
               INNER JOIN
            FILM f
               ON fa.FILM_ID = f.FILM_ID
               INNER JOIN
            FILM_CATEGORY fc
               ON
                  fc.FILE_ID = f.FILM_ID
               INNER JOIN
            CATEGORY c
               ON
                  fc.CATEGORY_ID = c.CATEGORY_ID
    WHERE
        a.ACTOR_ID = fa.ACTOR_ID
    AND c.NAME = 'Drama'
    AND f.RELEASE_YEAR = '2010'
);
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448