Learner Responses (7)
Learner Responses (7)
/* Now that you have imported the data sets, let’s explore some of the
tables.
To begin with, it is beneficial to know the shape of the tables and whether
any column has null values.
Further in this segment, you will take a look at 'movies' and 'genre'
tables.*/
-- Segment 1:
-- Q1. Find the total number of rows in each table of the schema?
-- Type your code below:
select count(*) as movie_row_count
from movie;
-- similarly evaluate for remaining columns
-- OR
SELECT
COUNT( CASE
WHEN title IS NULL THEN id
END) as title_nulls,
COUNT(CASE
WHEN year IS NULL THEN id
END) as year_nulls,
.....
FROM movie;
-- Rocky
SELECT SUM(CASE WHEN title IS NULL THEN 1 ELSE 0 END) AS
title_null_count,
SUM(CASE WHEN year IS NULL THEN 1 ELSE 0 END) AS
year_null_count,
SUM(CASE WHEN date_published IS NULL THEN 1 ELSE 0
END) AS date_published_null_count,
SUM(CASE WHEN duration IS NULL THEN 1 ELSE 0 END) AS
duration_null_count,
SUM(CASE WHEN country IS NULL THEN 1 ELSE 0 END) AS
country_null_count,
SUM(CASE WHEN worlwide_gross_income IS NULL THEN 1 ELSE 0
END) AS worldwide_gross_income_null_count,
SUM(CASE WHEN languages IS NULL THEN 1 ELSE 0 END) AS
languages_null_count,
SUM(CASE WHEN production_company IS NULL THEN 1 ELSE 0
END) AS production_company_null_count
FROM movie;
-- Now as you can see four columns of the movie table has null values.
Let's look at the at the movies released
-- each year.
-- Q3. Find the total number of movies released each year? How does the
trend look month wise? (Output expected)
+---------------+-------------------+
| Year | number_of_movies|
+-------------------+----------------
| 2017 | 2134 |
| 2018 | . |
| 2019 | . |
+---------------+-------------------+
-- Part one
select year,
count(*) as number_of_movies
from movie
group by year;
-- Q5. Find the unique list of the genres present in the data set?
-- Type your code below:
SELECT DISTINCT genre
FROM genre
ORDER BY genre;
/* So, RSVP Movies plans to make a movie of one of these genres.
Now, wouldn’t you want to know which genre had the highest number of
movies produced in the last year?
Combining both the movie and genres table can give more interesting
insights. */
/* So, based on the insight that you just drew, RSVP Movies should focus
on the ‘Drama’ genre.
But wait, it is too early to decide. A movie can belong to two or more
genres.
So, let’s find out the count of movies that belong to only one genre.*/
/* Output format:
+---------------+-------------------+
| genre | avg_duration |
+-------------------+----------------
| thriller | 105 |
| . | . |
| . | . |
+---------------+-------------------+ */
-- Type your code below:
-- Q9.What is the rank of the ‘thriller’ genre of movies among all the genres
in terms of number of movies produced?
-- (Hint: Use the Rank function)
/* Output format:
+---------------+-------------------+---------------------+
| genre | movie_count | genre_rank
|
+---------------+-------------------+---------------------+
|drama | 2312 | 2
|
+---------------+-------------------+---------------------+*/
-- Type your code below:
WITH summary AS
(
SELECT
genre,
COUNT(movie_id) AS movie_count,
RANK () OVER (ORDER BY COUNT(movie_id) DESC) AS
genre_rank
FROM
genre
GROUP BY genre
)
SELECT
*
FROM
summary
WHERE
lower(genre) = 'thriller';
-- Segment 2:
-- Q10. Find the minimum and maximum values in each column of the
ratings table except the movie_id column?
/* Output format:
+---------------+-------------------+---------------------+----------------------+-----------------
+-----------------+
| min_avg_rating|max_avg_rating | min_total_votes |
max_total_votes |min_median_rating|min_median_rating|
+---------------+-------------------+---------------------+----------------------+-----------------
+-----------------+
| 0 | 5 | 177
| 2000 | 0 | 8
|
+---------------+-------------------+---------------------+----------------------+-----------------
+-----------------+*/
-- Type your code below:
select min(avg_rating) as min_avg_rating,
max(avg_rating) as max_avg_rating,
...
from rating;
/* So, the minimum and maximum values in each column of the ratings
table are in the expected range.
This implies there are no outliers in the table.
Now, let’s find out the top 10 movies based on average rating.*/
/* Do you find you favourite movie FAN in the top 10 movies with an
average rating of 9.6? If not, please check your code again!!
So, now that you know the top 10 movies, do you think character actors
and filler actors can be from these movies?
Summarising the ratings table based on the movie counts by median rating
can give an excellent insight.*/
-- Q12. Summarise the ratings table based on the movie counts by median
ratings.
/* Output format:
+---------------+-------------------+
| median_rating | movie_count |
+-------------------+----------------
| 1 | 105 |
| . | . |
| . | . |
+---------------+-------------------+ */
-- Type your code below:
-- Order by is good to have
select median_rating, count(movie_id) as movie_count
from ratings
group by median_rating
order by median_rating;
/* Movies with a median rating of 7 is highest in number.
Now, let's find out the production house with which RSVP Movies can
partner for its next project.*/
-- Q13. Which production house has produced the most number of hit
movies (average rating > 8)??
/* Output format:
+------------------+-------------------+---------------------+
|production_company|movie_count | prod_company_rank|
+------------------+-------------------+---------------------+
| The Archers | 1 | 1 |
+------------------+-------------------+---------------------+*/
-- Type your code below:
WITH top_prod AS
(
SELECT
m.production_company,
COUNT(m.id) AS movie_count,
ROW_NUMBER() OVER (ORDER BY COUNT(m.id) DESC) AS
prod_company_rank
FROM
movie AS m
LEFT JOIN
ratings AS r
ON m.id = r.movie_id
WHERE avg_rating>8 AND m.production_company IS NOT NULL
GROUP BY m.production_company
)
SELECT
*
FROM
top_prod
WHERE
prod_company_rank = 1;
-- Q14. How many movies released in each genre during March 2017 in the
USA had more than 1,000 votes?
/* Output format:
+---------------+-------------------+
| genre | movie_count |
+-------------------+----------------
| thriller | 105 |
| . | . |
| . | . |
+---------------+-------------------+ */
-- Type your code below:
SELECT
genre,
COUNT(g.movie_id) AS movie_count
FROM
genre AS g
INNER JOIN
movie AS m
ON g.movie_id = m.id
INNER JOIN
ratings AS r
ON m.id = r.movie_id
WHERE
year = 2017
AND MONTH(date_published) = 3
AND LOWER(country) LIKE '%usa%'
AND total_votes > 1000
GROUP BY genre
ORDER BY movie_count DESC;
-- Answer is Yes
/* Now that you have analysed the movies, genres and ratings tables, let us
now analyse another table, the names table.
Let’s begin by searching for null values in the tables.*/
-- Segment 3:
-- OR
-- Solution 2
SELECT
COUNT(CASE
WHEN name IS NULL THEN id
END) AS name_nulls,
COUNT(CASE
WHEN height IS NULL THEN id
END) AS height_nulls,
COUNT(CASE
WHEN date_of_birth IS NULL THEN id
END) AS date_of_birth_nulls,
COUNT(CASE
WHEN known_for_movies IS NULL THEN id
END) AS known_for_movies_nulls
FROM
names;
-- Q19. Who are the top three directors in the top three genres whose
movies have an average rating > 8?
-- (Hint: The top three genres would have the most number of movies with
an average rating > 8.)
/* Output format:
+---------------+-------------------+
| director_name | movie_count |
+---------------+-------------------|
|James Mangold | 4 |
| . | . |
| . | . |
+---------------+-------------------+ */
-- Type your code below:
WITH top_rated_genres AS
(
SELECT
genre,
COUNT(m.id) AS movie_count,
RANK () OVER (ORDER BY COUNT(m.id) DESC) AS genre_rank
FROM
genre AS g
LEFT JOIN
movie AS m
ON g.movie_id = m.id
INNER JOIN
ratings AS r
ON m.id=r.movie_id
WHERE avg_rating>8
GROUP BY genre
)
SELECT
n.name as director_name,
COUNT(m.id) AS movie_count
FROM
names AS n
INNER JOIN
director_mapping AS d
ON n.id=d.name_id
INNER JOIN
movie AS m
ON d.movie_id = m.id
INNER JOIN
ratings AS r
ON m.id=r.movie_id
INNER JOIN
genre AS g
ON g.movie_id = m.id
WHERE g.genre IN (SELECT DISTINCT genre FROM top_rated_genres
WHERE genre_rank<=3)
AND avg_rating>8
GROUP BY name
ORDER BY movie_count DESC
LIMIT 3;
/* James Mangold can be hired as the director for RSVP's next project. Do
you remeber his movies, 'Logan' and 'The Wolverine'.
Now, let’s find out the top two actors.*/
-- Q20. Who are the top two actors whose movies have a median rating >=
8?
/* Output format:
+---------------+-------------------+
| actor_name | movie_count |
+-------------------+----------------
|Christain Bale | 10 |
| . | . |
+---------------+-------------------+ */
-- Type your code below:
select name, count(id)
inner join names, role_mapping, movie, rating
where median_rating>=8 and category=actors
group by actorname
order by movie_cnt decs
limit 2;
-- Debosmita (correct)
SELECT nm.name, COUNT(m.id) AS movie_count FROM names nm INNER
JOIN role_mapping rm ON nm.id = rm.name_id INNER JOIN movie m ON
rm.movie_id = m.id INNER JOIN ratings r ON m.id = r.movie_id WHERE
r.median_rating >= 8 AND rm.category = 'actor' GROUP BY nm.name
ORDER BY movie_count DESC LIMIT 2;
-- Darshan (correct)
SELECT
n.name as actor_name,
COUNT(m.id) AS movie_count
FROM
names AS n
INNER JOIN
role_mapping AS a
ON n.id=a.name_id
INNER JOIN
movie AS m
ON a.movie_id = m.id
INNER JOIN
ratings AS r
ON m.id=r.movie_id
WHERE median_rating>=8 AND category = 'actor'
GROUP BY actor_name
ORDER BY movie_count DESC
LIMIT 2;
/* Have you find your favourite actor 'Mohanlal' in the list. If no, please
check your code again.
RSVP Movies plans to partner with other global production houses.
Let’s find out the top three production houses in the world.*/
-- Q21. Which are the top three production houses based on the number of
votes received by their movies?
/* Output format:
+------------------+--------------------+---------------------+
|production_company|vote_count |
prod_comp_rank|
+------------------+--------------------+---------------------+
| The Archers | 830 | 1
|
| . | . |
. |
| . | . |
. |
+-------------------+-------------------+---------------------+*/
-- Type your code below:
WITH top_prod AS
(
SELECT
m.production_company,
SUM(r.total_votes) AS vote_count,
ROW_NUMBER() OVER (ORDER BY SUM(r.total_votes) DESC) AS
prod_company_rank
FROM
movie AS m
LEFT JOIN
ratings AS r
ON m.id = r.movie_id
WHERE m.production_company IS NOT NULL
GROUP BY m.production_company
)
SELECT
*
FROM
top_prod
WHERE
prod_company_rank <= 3;
Since RSVP Movies is based out of Mumbai, India also wants to woo its
local audience.
RSVP Movies also wants to hire a few Indian actors for its upcoming
project to give a regional feel.
Let’s find who these actors could be.*/
-- Q22. Rank actors with movies released in India based on their average
ratings. Which actor is at the top of the list?
-- Note: The actor should have acted in at least five Indian movies.
-- (Hint: You should use the weighted average based on votes. If the ratings
clash, then the total number of votes should act as the tie breaker.)
/* Output format:
+---------------+-------------------+---------------------+----------------------+-----------------+
| actor_name | total_votes | movie_count |
actor_avg_rating |actor_rank |
+---------------+-------------------+---------------------+----------------------+-----------------+
| Yogi Babu | 3455 | 11 |
8.42 | 1 |
| . | . | . |
. | . |
| . | . | . |
. | . |
| . | . | . |
. | . |
+---------------+-------------------+---------------------+----------------------+-----------------+*/
-- Type your code below:
WITH actor_ratings AS
(
SELECT
n.name as actor_name,
SUM(r.total_votes) AS total_votes,
COUNT(m.id) as movie_count,
ROUND(
SUM(r.avg_rating*r.total_votes)
/
SUM(r.total_votes)
,2) AS actor_avg_rating
FROM
names AS n
INNER JOIN
role_mapping AS a
ON n.id=a.name_id
INNER JOIN
movie AS m
ON a.movie_id = m.id
INNER JOIN
ratings AS r
ON m.id=r.movie_id
WHERE category = 'actor' AND LOWER(country) like '%india%'
GROUP BY actor_name
)
SELECT *,
RANK() OVER (ORDER BY actor_avg_rating DESC, total_votes DESC)
AS actor_rank
FROM
actor_ratings
WHERE movie_count>=5;
-- Q23.Find out the top five actresses in Hindi movies released in India
based on their average ratings?
-- Note: The actresses should have acted in at least three Indian movies.
-- (Hint: You should use the weighted average based on votes. If the ratings
clash, then the total number of votes should act as the tie breaker.)
/* Output format:
+---------------+-------------------+---------------------+----------------------+-----------------+
| actress_name | total_votes | movie_count |
actress_avg_rating |actress_rank |
+---------------+-------------------+---------------------+----------------------+-----------------+
| Tabu | 3455 | 11 |
8.42 | 1 |
| . | . | . |
. | . |
| . | . | . |
. | . |
| . | . | . |
. | . |
+---------------+-------------------+---------------------+----------------------+-----------------+*/
-- Type your code below:
WITH actress_ratings AS
(
SELECT
n.name as actress_name,
SUM(r.total_votes) AS total_votes,
COUNT(m.id) as movie_count,
ROUND(
SUM(r.avg_rating*r.total_votes)
/
SUM(r.total_votes)
,2) AS actress_avg_rating
FROM
names AS n
INNER JOIN
role_mapping AS a
ON n.id=a.name_id
INNER JOIN
movie AS m
ON a.movie_id = m.id
INNER JOIN
ratings AS r
ON m.id=r.movie_id
WHERE category = 'actress' AND LOWER(languages) like '%hindi%'
GROUP BY actress_name
)
SELECT *,
ROW_NUMBER() OVER (ORDER BY actress_avg_rating DESC,
total_votes DESC) AS actress_rank
FROM
actress_ratings
WHERE movie_count>=3
LIMIT 5;
/* Until now, you have analysed various tables of the data set.
Now, you will perform some tasks that will give you a broader
understanding of the data in this segment.*/
-- Segment 4:
-- Q25. What is the genre-wise running total and moving average of the
average movie duration?
-- (Note: You need to show the output table in the question.)
/* Output format:
+---------------+-------------------+---------------------+----------------------+
| genre | avg_duration |running_total_duration|
moving_avg_duration |
+---------------+-------------------+---------------------+----------------------+
| comdy | 145 | 106.2 |
128.42 |
| . | . | . |
. |
| . | . | . |
. |
| . | . | . |
. |
+---------------+-------------------+---------------------+----------------------+*/
-- Type your code below:
with genre_summary as
(select genere, round(avg(duration,2) as avg_duration
from genre left join movies
group by genre;
select *,
SUM(avg_duration) over (order by rows unbounded preceding) as
running_total_duration,
AVG(avg_duration) over (order by rows unbounded preceding) as
moving_avg_duration,
from genre_summary;
-- Round is good to have and not a must have; Same thing applies to
sorting
-- Q26. Which are the five highest-grossing movies of each year that belong
to the top three genres?
-- (Note: The top 3 genres would have the most number of movies.)
/* Output format:
+---------------+-------------------+---------------------+----------------------+-----------------+
| genre | year | movie_name
|worldwide_gross_income|movie_rank |
+---------------+-------------------+---------------------+----------------------+-----------------+
| comedy | 2017 | indian |
$103244842 | 1 |
| . | . | . |
. | . |
| . | . | . |
. | . |
| . | . | . |
. | . |
+---------------+-------------------+---------------------+----------------------+-----------------+*/
-- Type your code below:
-- Finally, let’s find out the names of the top two production houses that
have produced the highest number of hits among multilingual movies.
-- Q27. Which are the top two production houses that have produced the
highest number of
-- hits (median rating >= 8) among multilingual movies?
/* Output format:
+-------------------+-------------------+---------------------+
|production_company |movie_count |
prod_comp_rank|
+-------------------+-------------------+---------------------+
| The Archers | 830 | 1
|
| . | . |
. |
| . | . |
. |
+-------------------+-------------------+---------------------+*/
-- Type your code below:
WITH top_prod AS
(
SELECT
m.production_company,
COUNT(m.id) AS movie_count,
ROW_NUMBER() OVER (ORDER BY COUNT(m.id) DESC) AS
prod_company_rank
FROM
movie AS m
LEFT JOIN
ratings AS r
ON m.id = r.movie_id
WHERE median_rating>=8 AND m.production_company IS NOT NULL AND
POSITION(',' IN languages)>0
GROUP BY m.production_company
)
SELECT
*
FROM
top_prod
WHERE
prod_company_rank <= 2;
-- Q28. Who are the top 3 actresses based on number of Super Hit movies
(average rating >8) in drama genre?
/* Output format:
+---------------+-------------------+---------------------+----------------------+-----------------+
| actress_name | total_votes | movie_count |
actress_avg_rating |actress_rank |
+---------------+-------------------+---------------------+----------------------+-----------------+
| Laura Dern | 1016 | 1 | 9.60
| 1 |
| . | . | . |
. | . |
| . | . | . |
. | . |
+---------------+-------------------+---------------------+----------------------+-----------------+*/
-- Type your code below:
WITH actress_ratings AS
(
SELECT
n.name as actress_name,
SUM(r.total_votes) AS total_votes,
COUNT(m.id) as movie_count,
ROUND(
SUM(r.avg_rating*r.total_votes)
/
SUM(r.total_votes)
,2) AS actress_avg_rating
FROM
names AS n
INNER JOIN
role_mapping AS a
ON n.id=a.name_id
INNER JOIN
movie AS m
ON a.movie_id = m.id
INNER JOIN
ratings AS r
ON m.id=r.movie_id
INNER JOIN
genre AS g
ON m.id=g.movie_id
WHERE category = 'actress' AND lower(g.genre) ='drama'
GROUP BY actress_name
)
SELECT *,
ROW_NUMBER() OVER (ORDER BY actress_avg_rating DESC,
total_votes DESC) AS actress_rank
FROM
actress_ratings
LIMIT 3;
SELECT
n.name as actress_name,
SUM(r.total_votes) AS total_votes,
COUNT(m.id) as movie_count,
ROUND(
SUM(r.avg_rating*r.total_votes)
/
SUM(r.total_votes)
,2) AS actress_avg_rating
FROM
names AS n
INNER JOIN
role_mapping AS a
ON n.id=a.name_id
INNER JOIN
movie AS m
ON a.movie_id = m.id
INNER JOIN
ratings AS r
ON m.id=r.movie_id
INNER JOIN
genre AS g
ON m.id=g.movie_id
WHERE category = 'actress' AND lower(g.genre) ='drama'
GROUP BY actress_name;
/* Q29. Get the following details for top 9 directors (based on number of
movies)
Director id
Name
Number of movies
Average inter movie duration in days
Average movie ratings
Total votes
Min rating
Max rating
total movie durations
Format:
+---------------+-------------------+---------------------+----------------------+--------------
+--------------+------------+------------+----------------+
| director_id | director_name | number_of_movies |
avg_inter_movie_days | avg_rating | total_votes | min_rating
| max_rating | total_duration |
+---------------+-------------------+---------------------+----------------------+--------------
+--------------+------------+------------+----------------+
|nm1777967 | A.L. Vijay | 5
| 177 | 5.65 | 1754 | 3.7
| 6.9 | 613 |
| . | . | .
| . | . | . | .
| . | . |
| . | . | .
| . | . | . | .
| . | . |
| . | . | .
| . | . | . | .
| . | . |
| . | . | .
| . | . | . | .
| . | . |
| . | . | .
| . | . | . | .
| . | . |
| . | . | .
| . | . | . | .
| . | . |
| . | . | .
| . | . | . | .
| . | . |
| . | . | .
| . | . | . | .
| . | . |
+---------------+-------------------+---------------------+----------------------+--------------
+--------------+------------+------------+----------------+
--------------------------------------------------------------------------------------------*/
-- Type you code below:
WITH top_directors AS
(
SELECT
n.id as director_id,
n.name as director_name,
COUNT(m.id) AS movie_count,
RANK() OVER (ORDER BY COUNT(m.id) DESC) as director_rank
FROM
names AS n
INNER JOIN
director_mapping AS d
ON n.id=d.name_id
INNER JOIN
movie AS m
ON d.movie_id = m.id
GROUP BY n.id
),
movie_summary AS
(
SELECT
n.id as director_id,
n.name as director_name,
m.id AS movie_id,
m.date_published,
r.avg_rating,
r.total_votes,
m.duration,
LEAD(date_published) OVER (PARTITION BY n.id ORDER BY
m.date_published) AS next_date_published,
DATEDIFF(LEAD(date_published) OVER (PARTITION BY n.id ORDER BY
m.date_published),date_published) AS inter_movie_days
FROM
names AS n
INNER JOIN
director_mapping AS d
ON n.id=d.name_id
INNER JOIN
movie AS m
ON d.movie_id = m.id
INNER JOIN
ratings AS r
ON m.id=r.movie_id
WHERE n.id IN (SELECT director_id FROM top_directors WHERE
director_rank<=9)
)
SELECT
director_id,
director_name,
COUNT(DISTINCT movie_id) as number_of_movies,
ROUND(AVG(inter_movie_days),0) AS avg_inter_movie_days,
ROUND(
SUM(avg_rating*total_votes)
/
SUM(total_votes)
,2) AS avg_rating,
SUM(total_votes) AS total_votes,
MIN(avg_rating) AS min_rating,
MAX(avg_rating) AS max_rating,
SUM(duration) AS total_duration
FROM
movie_summary
GROUP BY director_id
ORDER BY number_of_movies DESC, avg_rating DESC;