Instruction
- Write a query to return the total number of actors using
UNION
. - Actor who appeared in both tv and movie has the same value of
actor_id
in both actor_tv and actor_movie tables.
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
count ------- 123
Expected results
Solution postgres
SELECT COUNT(*) FROM (
SELECT
T.actor_id
FROM actor_tv T
UNION
SELECT
M.actor_id
FROM actor_movie M
) X;
Explanation
This query is counting the number of unique actor IDs that appear in both the "actor_tv" table and the "actor_movie" table. It first combines the actor IDs from both tables using the "UNION" clause, which removes any duplicate actor IDs. The resulting table is then counted using the "COUNT(*)" function. The query is useful for determining the total number of actors who have appeared in both TV shows and movies.
Copied
Your results