-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetflix - Query.sql
More file actions
189 lines (143 loc) · 3.74 KB
/
Netflix - Query.sql
File metadata and controls
189 lines (143 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
drop table if exists netflix;
create table netflix
(
show_id varchar(10),
type varchar(20),
title varchar(150),
director varchar(250),
casts varchar(800),
country varchar(150),
date_added varchar(30),
release_year int,
rating varchar(20),
duration varchar(30),
listed_in varchar(100),
description varchar(300)
);
select * from netflix;
select count(*) as total_content from netflix;
select distinct type from netflix;
select * from netflix;
-- 15 Business Problems & Solutions
-- 1. Count the number of Movies vs TV Shows
select type, count(*) from netflix group by type;
-- 2. Find the most common rating for movies and TV shows
select * from
(select
type,
rating,
count(*),
rank() over(partition by type order by count(*) desc) as rank
from netflix
group by
1,2)
where rank=1;
-- 3. List all movies released in a specific year (e.g., 2020)
select
*
from netflix
where type='Movie' and release_year=2020;
-- 4. Find the top 5 countries with the most content on Netflix
select
trim(unnest(string_to_array(country,','))) as new_country,
count(show_id)
from netflix
group by 1
order by 2 desc
limit 5;
-- 5. Identify the longest movie
select
*
from netflix
where
type='Movie'
and
duration=(select max(duration) from netflix);
-- 6. Find content added in the last 5 years
select
*
from netflix
where
to_date(date_added,'Month DD,YEAR')>=current_date-interval '5 years';
-- 7. Find all the movies/TV shows by director 'Rajiv Chilaka'!
select
*
from netflix
where
director ilike '%Rajiv Chilaka%';
-- 8. List all TV shows with more than 5 seasons
select * from netflix
where
cast(split_part(duration,' ',1) as integer)>5
and type='TV Show';
---------------------------- OR -------------------------
select * from netflix
where
split_part(duration,' ',1)::numeric >5
and type='TV Show';
-- 9. Count the number of content items in each genre
select
listed_in,
trim(unnest(string_to_array(listed_in,','))),
show_id
from netflix;
select
trim(unnest(string_to_array(listed_in,','))),
count(*)
from netflix
group by 1 order by 2 desc;
-- 10. Find each year and the average numbers of content release in India on netflix.
-- Return top 5 year with highest avg content release
with new_netflix as
(select
release_year,
trim(unnested_country) as country
from netflix,
unnest(string_to_array(country,',')) as unnested_country)
select
release_year,
count(*)
from new_netflix
WHERE
country='India'
group by 1
order by 2 desc
limit 5
;
-- 11. List all movies that are documentaries
select
*
from netflix
where listed_in ilike '%documentaries%'
-- 12. Find all content without a director
select * from netflix where director isnull;
13. Find how many movies actor 'Salman Khan' appeared in last 10 years!
select
*
from netflix
where casts ilike '%Salman Khan%'
and
release_year> extract(year from current_date)-10;
14. Find the top 10 actors who have appeared in the highest number of movies produced in India.
select
trim(unnest(string_to_array(casts,','))) as actors,
count(*) as total_movies
from netflix
where
country ilike '%india%'
group by 1
order by 2 desc
limit 10;
-- 15. Categorize the content based on the presence of the keywords 'kill' and 'violence' in
the description field. Label content containing these keywords as 'Bad' and all other
content as 'Good'. Count how many items fall into each category.
with cat AS
(
select *,
case
when description ilike '%kill%' or description ilike '%violence%' then 'Bad'
else 'Good'
end as category
from netflix
)
select category,count(*) from cat group by 1;