57. Total number of actors (with UNION)

easy

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.



More UNION, UNION ALL questions

ID Title Level FTPR
129 Top 3 and bottom 3 courier uber medium
10%
104 MAU: Monthly active users mobile easy
19%
94 Top 3 products vs. bottom 3 products amazon medium
6%
87 Top song in the US and UK spotify medium
10%
47 Actors and customers whose first names end in 'D'. easy
45%
46 Actors and customers whose last name starts with 'A' easy
32%