24. Films with more than 10 actors

medium

Instruction
  • Write a query to return the titles of the films with >= 10 actors.

Table 1: film

       col_name       |  col_type
----------------------+--------------------------
 film_id              | integer
 title                | text
 description          | text
 release_year         | integer
 language_id          | smallint
 original_language_id | smallint
 rental_duration      | smallint
 rental_rate          | numeric
 length               | smallint
 replacement_cost     | numeric
 rating               | text

Table 2: film_actor

Films and their casts

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

Sample results

         title
------------------------
 ACADEMY DINOSAUR
 ARABIA DOGMA

Expected results

Solution postgres

WITH film_casts_cnt AS (
	SELECT 
	    film_id,
	    COUNT(*) AS actors_cnt
	FROM film_actor
	GROUP BY film_id
	HAVING COUNT(*)>=10
)

SELECT title
FROM film
WHERE film_id IN (
    SELECT film_id
    FROM film_casts_cnt
)
    

Explanation

This query is selecting the titles of all films that have 10 or more actors in their cast.

The first part of the query creates a temporary table called "film_casts_cnt" which contains the count of actors in each film. It does this by grouping the "film_actor" table by film_id and counting the number of rows in each group. It then filters this table to only include films with 10 or more actors.

The second part of the query selects the titles of all films where the film_id is in the "film_casts_cnt" table. This effectively joins the two tables on the film_id column and returns the title for each matching row.



More IN, BETWEEN, LIKE, CASE WHEN questions

ID Title Level FTPR
178 Members who worked at both Microsoft and Google linkedin medium
34%
177 Purchases by platform report amazon medium
10%
176 Employees' annual bonus amazon easy
12%
160 Sellers with no sales by day ebay hard
11%
156 Cancellation rate by unbanned users lyft hard
15%
155 Driver with the highest cancellation rate lyft easy
24%
151 Salary report dropbox easy
18%
138 Happy restaurants doordash easy
14%
136 Extremely late orders doordash easy
11%
131 Churned accounts affirm hard
10%
102 Histogram by visit session duration mobile hard
17%
35 Film length report easy
27%
34 Stocked up movies easy
27%
33 Returning customers medium
8%
32 Unpopular movies hard
17%
31 Movies that have not been returned easy
26%
30 Inactive customers in May easy
22%
29 Second highest spend customer medium
14%
28 Film with the second largest cast medium
26%
27 Film with the largest cast easy
27%
26 Second shortest film easy
30%
25 Shortest film easy
46%
22 Average cost per rental transaction easy
42%
15 Fast movie watchers vs slow watchers hard
6%
14 Good days and bad days hard
4%
13 Actors' first name medium
14%
12 Actors' last name ending in 'EN' or 'RY' easy
27%
11 Actors' last name easy
21%