Question 51

Movie only actor

Instruction
  • Write a query to return the first name and last name of actors who only appeared in movies.
  • Actor appeared in tv should not be included .
  • The order of your results doesn't matter.

Table 1: actor_movie

Actors who appeared in a movie.

  col_name  | col_type
------------+-------------------
 actor_id   | integer
 first_name | character varying
 last_name  | character varying

Table 2: actor_tv

Actors who appeared in a TV show.

  col_name  | col_type
------------+-------------------
 actor_id   | integer
 first_name | character varying
 last_name  | character varying

Sample results

 first_name  |  last_name
-------------+-------------
 ED          | CHASE
 ZERO        | CAGE
 CUBA        | OLIVIER

Solution

postgres
SELECT M.first_name, M.last_name
FROM actor_movie M
LEFT JOIN actor_tv T
ON M.actor_id = T.actor_id
WHERE T.actor_id IS NULL;

Explanation

This PostgreSQL query selects the first and last names of actors who have only appeared in movies but not in TV shows. It does this by joining two tables, actor_movie and actor_tv, on their actor_id columns. The LEFT JOIN keyword is used to include all rows from the actor_movie table and only matching rows from the actor_tv table. The WHERE clause filters out any rows where the actor_id in the actor_tv table is null, indicating that the actor has not appeared in any TV shows.

Copied

Last Submission

postgres

No submission yet for this engine. Run and submit your query to save it here.

Copied
Expected results

Submit a query to compare against expected output.