19. Most productive actor

medium

Instruction

  • Write a query to return the first name and the last name of the actor who appeared in the most films.

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

 first_name | last_name
------------+-----------
 MICHAEL       | JACKSON

Expected results

Solution postgres

WITH top_actor AS (
	SELECT 
	  actor_id, 
	  COUNT(*) AS film_cnt
	FROM film_actor
	GROUP BY actor_id
	ORDER BY film_cnt DESC
	LIMIT 1
)
SELECT first_name, last_name
FROM actor A
WHERE A.actor_id 
IN (
  SELECT actor_id 
  FROM top_actor
);
    

Explanation

This query is designed to find the first and last name of the actor who has acted in the most number of films.

It starts with a common table expression (CTE) named "top_actor," which uses the "film_actor" table to count the number of films each actor has acted in. It groups the records by actor_id and orders them in descending order of the count. The LIMIT 1 clause ensures that only the actor who has acted in the most number of films is selected.

The main query then selects the first and last name of the actor from the "actor" table, where the actor_id is present in the subquery of the "top_actor" CTE. This ensures that only the actor with the highest number of films is returned in the result set.



More COUNT, SUM, AVERAGE, MIN, MAX, GROUP BY, HAVING questions

ID Title Level FTPR
216 Highest spender gaming hard -
215 Monthly active users gaming hard -
214 D30 retention rate gaming hard -
209 Card issuer customers count visa easy -
206 Search results recall by testing group search engine medium -
205 Search results recall search engine medium -
204 AB testing sanity check search engine easy -
202 Monthly active paid subscriptions subscription easy -
198 Average score per subject snap easy -
197 Students who didn't finish all subjects snap easy -
196 User segments report snap hard -
193 Difference of replies by testing groups twitter medium
100%
192 Number of replies to the original tweet by group twitter medium
100%
190 Twitter campaign spend report twitter hard
50%
187 Comment distribution at Google Forum google easy
50%
186 Average comments per question google easy
50%
185 Top 5 non-English contents by watch time netflix medium
53%
184 Top 5 movies by watch time netflix medium
7%
183 Top 5 movies by number of watchers netflix easy
33%
181 Users who watched less than one hour of Netflix netflix easy
23%
174 Closed accounts social easy
18%
173 User Popularity Percentage social medium
5%
171 Binge watches tiktok easy
19%
170 Top 3 countries by average watch time tiktok easy
10%
169 Same day streams tiktok easy
29%
167 Top country in video streaming tiktok easy
24%
164 Orders distribution by brand walmart easy
22%
163 Average number of orders per brand walmart easy
36%
161 Biggest sales day ebay easy
19%
159 Transaction volume from suspended accounts ebay easy
51%
154 Rideshare completion rate lyft easy
22%
153 Project ROI dropbox easy
18%
149 fans by video category social easy
30%
146 Purchase by platform afterpay easy
13%
143 Customer purchase report afterpay easy
15%
134 Expensive departments robinhood medium
32%
133 Small departments robinhood easy
44%
132 Top 3 trending countries affirm medium
8%
130 Newly created accounts affirm easy
34%
128 Completion rate uber easy
23%
125 Active restaurants with fewer than 5 trips per city uber easy
14%
124 Active restaurants by city uber easy
36%
112 Top 3 customers amazon easy
30%
107 Customer count by platform mobile medium
19%
106 Mobile vs. web mobile medium
9%
101 Average number of visits per user mobile easy
12%
98 Most popular user social easy
29%
97 Social influencer social easy
10%
96 Overall acceptance rate social easy
13%
95 Candidate products for subscription amazon hard
11%
93 number of sales for each product this month amazon easy
19%
89 Top 5 artists in the US spotify medium
11%
86 Top song in the US spotify easy
19%
85 Top 4 queries based on click through rate on new year's day search engine hard
6%
84 Click through rate on new year's day search engine medium
10%
82 The top search query on new year's day search engine easy
12%
81 How many people searched on new year's day search engine easy
24%
23 Average spend per customer in Feb 2020 easy
16%
21 Customer who rented the most medium
20%
20 Customer who spent the most medium
15%
18 Top film category easy
25%
10 Min and max spend easy
13%
9 Number of high spend customers by month medium
4%
8 Average customer spend by month easy
22%
7 Unique customers count by month easy
19%
6 Daily revenue in June, 2020 medium
11%
5 Monthly revenue easy
24%