-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneftlix_db_postgre.sql
More file actions
190 lines (131 loc) · 3.08 KB
/
neftlix_db_postgre.sql
File metadata and controls
190 lines (131 loc) · 3.08 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
190
-- Netflix db project
DROP TABLE IF EXISTS netflix;
CREATE TABLE netflix
(
show_id VARCHAR (40),
type VARCHAR (50),
title VARCHAR (150),
director VARCHAR (220),
casts VARCHAR (1000),
country VARCHAR (150),
date_added VARCHAR (50),
release_year INT,
rating VARCHAR (10),
duration VARCHAR (20),
listed_in VARCHAR (300),
description VARCHAR (250)
);
-- inserting data
SELECT * FROM netflix;
-- double-check
SELECT
DISTINCT type
FROM netflix;
-- << Solving 15 Business Problems>> - part 1
-- task 1: Count the Number of Movies vs TV Shows
SELECT * FROM netflix;
SELECT
type,
COUNT(*) as total_content
FROM netflix
GROUP BY type
-- task 2: Find the Most Common Rating for Movies and TV Shows
SELECT
type,
rating
FROM
(
SELECT
type,
rating,
COUNT(*),
RANK() OVER(PARTITION BY type ORDER BY COUNT(*) DESC) as ranking
FROM netflix
GROUP BY 1, 2
) as t1
WHERE
ranking = 1
-- task 3: List All Movies Released in a Specific Year (e.g., 2020)
--filter 2020
--movies
SELECT * FROM netflix;
SELECT
release_year,
COUNT(*) as total_new
FROM netflix
WHERE release_year = 2021
GROUP BY 1;
-- << (only totals)
SELECT * FROM netflix
WHERE
type = 'Movie'
AND
release_year = 2020;
-- << (the list of movies)
-- task 5: Find the Top 5 Countries with the Most Content on Netflix
SELECT * FROM netflix;
SELECT
UNNEST(STRING_TO_ARRAY(country, ',')) as new_country,
COUNT(show_id) as total_content
FROM netflix
GROUP BY 1
ORDER BY 2 DESC
LIMIT 5;
--<< writing a function >>
SELECT
UNNEST(STRING_TO_ARRAY(country, ',')) as new_country
FROM netflix;
-- task 6: Identify the Longest Movie
SELECT * FROM netflix
WHERE
type = 'Movie'
ORDER BY duration DESC
LIMIT 10;
-- as a result full movie data including NULL
-- or
SELECT * FROM netflix
WHERE
type = 'Movie'
AND
duration = (SELECT MAX(duration) FROM netflix);
-- task 6: Find Content Added in the Last 5 Years
SELECT
*
FROM netflix
WHERE
TO_DATE(date_added, 'Month DD, YYYY') >= CURRENT_DATE - INTERVAL '5 years';
--SELECT CURRENT_DATE - INTERVAL '5 years'
-- task 7: Find All Movies/TV Shows by Director 'Rajiv Chilaka'
SELECT * FROM netflix
WHERE director LIKE '%Rajiv Chilaka%';
-- task 8: List All TV Shows with More Than 5 Seasons
SELECT
type as total_shows,
duration
FROM netflix
WHERE
type = 'TV Show'
duration > '5 Seasons'
SELECT
*
FROM netflix
WHERE
type = 'TV Show'
AND
SPLIT_PART(duration, ' ', 1)::numeric > 5;
-- task 9: Count the Number of Content Items in Each Genre
SELECT
UNNEST(STRING_TO_ARRAY(listed_in, ',')) as genre,
COUNT(show_id) as total_content
FROM netflix
GROUP by 1
-- task 10: Find each year and the average numbers of content release in India on Netflix
SELECT
EXTRACT (YEAR FROM TO_DATE(date_added,'Month DD, YYYY')) as date,
COUNT(*),
ROUND (
COUNT(*):: numeric / (SELECT COUNT(*) FROM netflix WHERE country = 'India')::numeric * 100, 2) as avg_content_year
FROM netflix
WHERE country = 'India'
GROUP BY 1
---- <<end of part 1>>