Repository navigation
Expand file tree
/
Copy pathApplication_query
More file actions
158 lines (132 loc) · 3.96 KB
/
Copy pathApplication_query
File metadata and controls
158 lines (132 loc) · 3.96 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
Create table Netflix (
show_id VARCHAR(5) primary key,
type VARCHAR(10),
title VARCHAR(250),
director VARCHAR(550),
casts VARCHAR(1050),
country VARCHAR(550),
date_added VARCHAR(55),
release_year INT,
rating VARCHAR(15),
duration VARCHAR(15),
listed_in VARCHAR(250),
description VARCHAR(550)
);
copy Netflix
from 'D:\SQL Projects\netflix_sql_project-main\netflix_titles.csv'
delimiter ','
csv header;
select * from netflix;
-- Netflix Data Analysis using SQL
-- Solutions of 15 business problems
-- 1. Count the number of Movies vs TV Shows
select type,count(*) as total_number_of from netflix
group by 1;
---- 2. Find the most common rating for movies and TV shows
select rating, count(*) as rating_count from netflix
group by 1
order by 2 desc;
WITH RatingCounts AS (
SELECT
type,
rating,
COUNT(*) AS rating_count
FROM netflix
GROUP BY type, rating
),
RankedRatings AS (
SELECT
type,
rating,
rating_count,
RANK() OVER (PARTITION BY type ORDER BY rating_count DESC) AS rank
FROM RatingCounts
)
SELECT
type,
rating AS most_frequent_rating
FROM RankedRatings
WHERE rank = 1;
-- 3. List all movies released in a specific year (e.g., 2020)
SELECT show_id, type, date_added
FROM netflix
WHERE EXTRACT(YEAR FROM TO_DATE(date_added, 'Month DD, YYYY')) = 2020
AND type = 'Movie';
-- 4. Find the top 5 countries with the most content on Netflix
SELECT country, COUNT(*) AS number_of_content
FROM netflix
WHERE country IS NOT NULL
GROUP BY country
ORDER BY number_of_content DESC
LIMIT 5;
-- 5. Identify the longest movie
select * from netflix where type='Movie'
order by split_part(duration, ' ', 1)::INT DESC limit 1
-- 6. Find content added in the last 5 years
select date_added,description from netflix
where EXTRACT(YEAR FROM TO_DATE(date_added, 'Month DD, YYYY')) > 2020;
SELECT
*
FROM netflix
WHERE TO_DATE(date_added, 'Month DD, YYYY') >= CURRENT_DATE - INTERVAL '5 years';
-- 7. Find all the movies/TV shows by director 'Rajiv Chilaka'!
select director,type from netflix
where director='Rajiv Chilaka';
-- 8. List all TV shows with more than 5 seasons
select * from netflix;
select type,duration from netflix
where type='TV Show'
AND SPLIT_PART(duration, ' ', 1)::INT > 5
group by 1,2;
--9. Count the Number of Content Items in Each Genre
SELECT
UNNEST(STRING_TO_ARRAY(listed_in, ',')) AS genre,
COUNT(*) AS total_content
FROM netflix
GROUP BY 1;
-- 10.Find each year and the average numbers of content release in India on netflix.
SELECT year, round(AVG(content_count)) AS avg_content_release
FROM (
SELECT
EXTRACT(YEAR FROM TO_DATE(date_added, 'Month DD, YYYY')) AS year,
COUNT(*) AS content_count
FROM netflix
WHERE country = 'India'
GROUP BY year
) AS yearly_counts
GROUP BY year
ORDER BY year;
--11.List All Movies that are Documentaries
select * from netflix
where listed_in like '%Documentaries';
-- 12. Find All Content Without a Director
select * from netflix
where Director is null;
--13.Find How Many Movies Actor 'Salman Khan' Appeared in the Last 10 Years
SELECT COUNT(*) AS total_movies
FROM netflix
WHERE type = 'Movie'
AND casts ilike '%Salman Khan%'
AND TO_DATE(date_added, 'Month DD, YYYY') >= CURRENT_DATE - INTERVAL '10 years';
--14.Find the Top 10 Actors Who Have Appeared in the Highest Number of Movies Produced in India
SELECT
UNNEST(STRING_TO_ARRAY(casts, ',')) AS actor,
COUNT(*) as number_of_appearence
FROM netflix
WHERE country = 'India'
GROUP BY actor
ORDER BY COUNT(*) DESC
LIMIT 10;
--15. Categorize Content Based on the Presence of 'Kill' and 'Violence' Keywords
SELECT
category,
COUNT(*) AS content_count
FROM (
SELECT
CASE
WHEN description ILIKE '%kill%' OR description ILIKE '%violence%' THEN 'Bad'
ELSE 'Good'
END AS category
FROM netflix
) AS categorized_content
GROUP BY category;