41. Productive actors vs less-productive actors

hard

***********

  • ****************************************************************************
  • *****************************************

**********

  • ************************>= 30*******
  • *****************************<30*******

Table 1: actor

  col_name   | col_type
-------------+--------------------------
 actor_id    | integer
 first_name  | text
 last_name   | text

Table 2: film_actor

Films and their casts

  col_name   | col_type
-------------+--------------------------
 actor_id    | smallint
 film_id     | smallint

Sample results

 actor_category  | count
-----------------+-------
 less productive |   123
 productive      |   456

Solution postgres

SELECT actor_category,
    COUNT(*)
FROM (        
	SELECT 
	    A.actor_id,
	    CASE WHEN  COUNT(DISTINCT FA.film_id) >= 30 THEN 'productive' ELSE 'less productive' END AS actor_category	     
	FROM actor A
	LEFT JOIN film_actor FA
	ON FA.actor_id = A.actor_id
	GROUP BY A.actor_id
) X
GROUP BY actor_category;
    

Explanation

This query is used to group actors into two categories based on their productivity. The first category is "productive," which includes actors who have acted in 30 or more films. The second category is "less productive," which includes actors who have acted in less than 30 films.

The query selects the actor_category and the count of actors in each category.

To achieve this, the query first selects the actor_id and uses a CASE statement to categorize them as either "productive" or "less productive." It counts the number of distinct film_ids each actor has acted in, and if the count is greater than or equal to 30, the actor is categorized as "productive."

The subquery is then grouped by actor_id, and the outer query groups the actor_category and counts the number of actors in each category.

Overall, this query provides insights into the productivity of actors and can help in making casting decisions or identifying successful actors in the film industry.

Expected results



More OUTER JOIN questions

ID Title Level FTPR
194 Daily active users report for new market snap easy
20%
191 Number of replies by group twitter medium
18%
172 Comments distribution facebook hard
13%
168 Average number of streams in the US tiktok medium
13%
140 Daily sales of restaurant 100011 doordash hard
8%
118 Daily bookings in the US airbnb hard
5%
100 Advertiser ROI facebook medium
8%
56 Total number of actors easy
30%
55 Busy days and slow days medium
10%
54 Customer groups by movie rental spend medium
20%
53 Movie groups by rental income hard
11%
52 Movies cast by movie only actors easy
11%
51 Movie only actor easy
37%
45 Movie inventory optimization hard
9%
44 In-demand vs not-in-demand movies medium
6%
43 Customers who rented vs. those who did not medium
8%
42 Films that are in stock vs not in stock medium
12%