-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSVP Movies case study.sql
473 lines (357 loc) · 12.2 KB
/
RSVP Movies case study.sql
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
--In this file some large script questions are solved with the help of google.
USE imdb;
-- Q1. Find the total number of rows in each table of the schema?
SELECT TABLE_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'imdb';
-- Q2. Which columns in the movie table have null values?
SELECT
SUM(CASE WHEN id IS NULL THEN 1 ELSE 0 END) AS ID,
SUM(CASE WHEN title IS NULL THEN 1 ELSE 0 END) AS TITLE,
SUM(CASE WHEN year IS NULL THEN 1 ELSE 0 END) AS YEAR,
SUM(CASE WHEN date_published IS NULL THEN 1 ELSE 0 END) AS DATE_PUBLISHED,
SUM(CASE WHEN duration IS NULL THEN 1 ELSE 0 END) AS DURATION,
SUM(CASE WHEN country IS NULL THEN 1 ELSE 0 END) AS COUNTRY,
SUM(CASE WHEN worlwide_gross_income IS NULL THEN 1 ELSE 0 END) AS WORLDWIDE_GROSS_INCOME,
SUM(CASE WHEN languages IS NULL THEN 1 ELSE 0 END) AS LANGUAGES,
SUM(CASE WHEN production_company IS NULL THEN 1 ELSE 0 END) AS PRODUCTION_COMPANY
FROM movie;
-- Q3. Find the total number of movies released each year? How does the trend look month wise? (Output expected)
# Total number of movies
Select year, count(title) AS TITLE from movie group by year;
# Month wise trend
SELECT MONTH(date_published) AS MONTH_NUM, COUNT(id) AS NUMBER_OF_MOVIES
FROM movie
GROUP BY MONTH(date_published)
ORDER BY MONTH(date_published);
-- Q4. How many movies were produced in the USA or India in the year 2019??
SELECT COUNT(id) AS No_of_movies, year
FROM movie
WHERE country = 'USA' OR country = 'India'
GROUP BY country
HAVING year=2019;
-- Q5. Find the unique list of the genres present in the data set?
SELECT genre FROM genre
UNION
SELECT genre FROM genre;
-- Q6.Which genre had the highest number of movies produced overall?
SELECT genre, year, COUNT(*) AS NO_OF_MOVIES
FROM genre AS g
INNER JOIN movie AS M
ON g.movie_id = M.id
WHERE year = 2019
GROUP BY genre
ORDER BY no_of_movies DESC
LIMIT 1;
-- Q7. How many movies belong to only one genre?
WITH genre_count
AS(
SELECT movie_id, count(genre) as count_of_genre
FROM genre group by movie_id)
SELECT count(movie_id) FROM genre_count where count_of_genre = 1;
-- Q8.What is the average duration of movies in each genre?
SELECT genre, ROUND(AVG(duration),2) AS avg_duration
FROM genre AS G
INNER JOIN movie AS M
ON G.movie_id = M.id
GROUP BY genre
ORDER BY AVG(duration) DESC;
-- Q9.What is the rank of the ‘thriller’ genre of movies among all the genres in terms of number of movies produced?
WITH genre_rank 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 genre_rank
WHERE genre='thriller';
-- Q10. Find the minimum and maximum values in each column of the ratings table except the movie_id column?
SELECT MIN(avg_rating) AS min_avg_rating,
MAX(avg_rating) AS max_avg_rating,
MIN(total_votes) AS min_total_votes,
MAX(total_votes) AS max_total_votes,
MIN(median_rating) AS min_median_rating,
MAX(median_rating) AS max_median_rating
FROM ratings;
-- Q11. Which are the top 10 movies based on average rating?
SELECT title, avg_rating,
DENSE_RANK() OVER(ORDER BY avg_rating DESC) AS movie_rank
FROM movie AS m
INNER JOIN ratings AS r
ON r.movie_id = m.id
LIMIT 10;
-- Q12. Summarise the ratings table based on the movie counts by median ratings.
SELECT median_rating, COUNT(movie_id) AS movie_count
FROM ratings
GROUP BY median_rating
ORDER BY median_rating;
-- Q13. Which production house has produced the most number of hit movies (average rating > 8)??
SELECT production_company, COUNT(id) AS movie_count,
DENSE_RANK() OVER(ORDER BY COUNT(id) DESC) AS prod_company_rank
FROM movie AS m
INNER JOIN ratings AS r
ON m.id = r.movie_id
WHERE avg_rating > 8 AND production_company IS NOT NULL
GROUP BY production_company
ORDER BY movie_count DESC;
-- Q14. How many movies released in each genre during March 2017 in the USA had more than 1,000 votes?
SELECT g.genre, COUNT(g.movie_id) AS movie_count
FROM genre AS g
INNER JOIN ratings AS r
ON g.movie_id = r.movie_id
INNER JOIN movie AS m
ON m.id = g.movie_id
WHERE m.country='USA' AND r.total_votes>1000 AND MONTH(date_published)=3 AND year=2017
GROUP BY g.genre
ORDER BY movie_count DESC;
-- Q15. Find movies of each genre that start with the word ‘The’ and which have an average rating > 8?
SELECT title, avg_rating, genre
FROM genre AS g
INNER JOIN ratings AS r
ON g.movie_id = r.movie_id
INNER JOIN movie AS m
ON m.id = g.movie_id
WHERE title LIKE 'The%' AND avg_rating > 8
ORDER BY avg_rating DESC;
-- Q16. Of the movies released between 1 April 2018 and 1 April 2019, how many were given a median rating of 8?
SELECT median_rating, COUNT(movie_id) AS movie_count
FROM movie AS m
INNER JOIN ratings AS r
ON m.id = r.movie_id
WHERE median_rating = 8 AND date_published BETWEEN '2018-04-01' AND '2019-04-01'
GROUP BY median_rating;
-- Q17. Do German movies get more votes than Italian movies?
SELECT total_votes, languages
FROM movie AS m
INNER JOIN ratings AS r
ON m.id = r.movie_id
WHERE languages LIKE 'German' OR languages LIKE 'Italian'
GROUP BY languages
ORDER BY total_votes DESC;
-- Q18. Which columns in the names table have null values??
SELECT
SUM(CASE WHEN name IS NULL THEN 1 ELSE 0 END) AS name_nulls,
SUM(CASE WHEN height IS NULL THEN 1 ELSE 0 END) AS height_nulls,
SUM(CASE WHEN date_of_birth IS NULL THEN 1 ELSE 0 END) AS date_of_birth_nulls,
SUM(CASE WHEN known_for_movies IS NULL THEN 1 ELSE 0 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?
WITH top_genre AS
(
SELECT g.genre, COUNT(g.movie_id) AS movie_count
FROM genre AS g
INNER JOIN ratings AS r
ON g.movie_id = r.movie_id
WHERE avg_rating > 8
GROUP BY genre
ORDER BY movie_count
LIMIT 3
),
top_director AS
(
SELECT n.name AS director_name,
COUNT(g.movie_id) AS movie_count,
ROW_NUMBER() OVER(ORDER BY COUNT(g.movie_id) DESC) AS director_row_rank
FROM names AS n
INNER JOIN director_mapping AS dm
ON n.id = dm.name_id
INNER JOIN genre AS g
ON dm.movie_id = g.movie_id
INNER JOIN ratings AS r
ON r.movie_id = g.movie_id,
top_genre
WHERE g.genre in (top_genre.genre) AND avg_rating>8
GROUP BY director_name
ORDER BY movie_count DESC
)
SELECT *
FROM top_director
WHERE director_row_rank <= 3
LIMIT 3;
-- Q20. Who are the top two actors whose movies have a median rating >= 8?
SELECT DISTINCT name AS actor_name, COUNT(r.movie_id) AS movie_count
FROM ratings AS r
INNER JOIN role_mapping AS rm
ON rm.movie_id = r.movie_id
INNER JOIN names AS n
ON rm.name_id = n.id
WHERE median_rating >= 8 AND category = 'actor'
GROUP BY name
ORDER BY movie_count DESC
LIMIT 2;
-- Q21. Which are the top three production houses based on the number of votes received by their movies?
SELECT production_company, SUM(total_votes) AS vote_count,
DENSE_RANK() OVER(ORDER BY SUM(total_votes) DESC) AS prod_comp_rank
FROM movie AS m
INNER JOIN ratings AS r
ON m.id = r.movie_id
GROUP BY production_company
LIMIT 3;
-- Q22. Rank actors with movies released in India based on their average ratings. Which actor is at the top of the list?
SELECT name AS actor_name, total_votes,
COUNT(m.id) as movie_count,
ROUND(SUM(avg_rating*total_votes)/SUM(total_votes),2) AS actor_avg_rating,
RANK() OVER(ORDER BY avg_rating DESC) AS actor_rank
FROM movie AS m
INNER JOIN ratings AS r
ON m.id = r.movie_id
INNER JOIN role_mapping AS rm
ON m.id=rm.movie_id
INNER JOIN names AS nm
ON rm.name_id=nm.id
WHERE category='actor' AND country= 'india'
GROUP BY name
HAVING COUNT(m.id)>=5
LIMIT 1;
-- Q23.Find out the top five actresses in Hindi movies released in India based on their average ratings?
SELECT name AS actress_name, total_votes,
COUNT(m.id) AS movie_count,
ROUND(SUM(avg_rating*total_votes)/SUM(total_votes),2) AS actress_avg_rating,
RANK() OVER(ORDER BY avg_rating DESC) AS actress_rank
FROM movie AS m
INNER JOIN ratings AS r
ON m.id = r.movie_id
INNER JOIN role_mapping AS rm
ON m.id=rm.movie_id
INNER JOIN names AS nm
ON rm.name_id=nm.id
WHERE category='actress' AND country='india' AND languages='hindi'
GROUP BY name
HAVING COUNT(m.id)>=3
LIMIT 1;
-- Q24. Select thriller movies as per avg rating and classify them in the following category:
SELECT title,
CASE WHEN avg_rating > 8 THEN 'Superhit movies'
WHEN avg_rating BETWEEN 7 AND 8 THEN 'Hit movies'
WHEN avg_rating BETWEEN 5 AND 7 THEN 'One-time-watch movies'
WHEN avg_rating < 5 THEN 'Flop movies'
END AS avg_rating_category
FROM movie AS m
INNER JOIN genre AS g
ON m.id=g.movie_id
INNER JOIN ratings as r
ON m.id=r.movie_id
WHERE genre='thriller';
-- Q25. What is the genre-wise running total and moving average of the average movie duration?
SELECT genre,
ROUND(AVG(duration),2) AS avg_duration,
SUM(ROUND(AVG(duration),2)) OVER(ORDER BY genre ROWS UNBOUNDED PRECEDING) AS running_total_duration,
AVG(ROUND(AVG(duration),2)) OVER(ORDER BY genre ROWS 10 PRECEDING) AS moving_avg_duration
FROM movie AS m
INNER JOIN genre AS g
ON m.id= g.movie_id
GROUP BY genre
ORDER BY genre;
-- Q26. Which are the five highest-grossing movies of each year that belong to the top three genres?
WITH top_3_genre AS
(
SELECT genre, COUNT(movie_id) AS number_of_movies
FROM genre AS g
INNER JOIN movie AS m
ON g.movie_id = m.id
GROUP BY genre
ORDER BY COUNT(movie_id) DESC
LIMIT 3
),
top_5 AS
(
SELECT genre,
year,
title AS movie_name,
worlwide_gross_income,
DENSE_RANK() OVER(PARTITION BY year ORDER BY worlwide_gross_income DESC) AS movie_rank
FROM movie AS m
INNER JOIN genre AS g
ON m.id= g.movie_id
WHERE genre IN (SELECT genre FROM top_3_genre)
)
SELECT *
FROM top_5
WHERE movie_rank<=5;
-- Q27. Which are the top two production houses that have produced the highest number of hits (median rating >= 8) among multilingual movies?
SELECT production_company,
COUNT(m.id) AS movie_count,
ROW_NUMBER() OVER(ORDER BY count(id) DESC) AS prod_comp_rank
FROM movie AS m
INNER JOIN ratings AS r
ON m.id=r.movie_id
WHERE median_rating>=8 AND production_company IS NOT NULL AND POSITION(',' IN languages)>0
GROUP BY production_company
LIMIT 2;
-- Q28. Who are the top 3 actresses based on number of Super Hit movies (average rating >8) in drama genre?
SELECT name, SUM(total_votes) AS total_votes,
COUNT(rm.movie_id) AS movie_count,
avg_rating,
DENSE_RANK() OVER(ORDER BY avg_rating DESC) AS actress_rank
FROM names AS n
INNER JOIN role_mapping AS rm
ON n.id = rm.name_id
INNER JOIN ratings AS r
ON r.movie_id = rm.movie_id
INNER JOIN genre AS g
ON r.movie_id = g.movie_id
WHERE category = 'actress' AND avg_rating > 8 AND genre = 'drama'
GROUP BY name
LIMIT 3;
/* 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
*/
WITH movie_date_info AS
(
SELECT d.name_id, name, d.movie_id,
m.date_published,
LEAD(date_published, 1) OVER(PARTITION BY d.name_id ORDER BY date_published, d.movie_id) AS next_movie_date
FROM director_mapping d
JOIN names AS n
ON d.name_id=n.id
JOIN movie AS m
ON d.movie_id=m.id
),
date_difference AS
(
SELECT *, DATEDIFF(next_movie_date, date_published) AS diff
FROM movie_date_info
),
avg_inter_days AS
(
SELECT name_id, AVG(diff) AS avg_inter_movie_days
FROM date_difference
GROUP BY name_id
),
final_result AS
(
SELECT d.name_id AS director_id,
name AS director_name,
COUNT(d.movie_id) AS number_of_movies,
ROUND(avg_inter_movie_days) AS inter_movie_days,
ROUND(AVG(avg_rating),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,
ROW_NUMBER() OVER(ORDER BY COUNT(d.movie_id) DESC) AS director_row_rank
FROM
names AS n
JOIN director_mapping AS d
ON n.id=d.name_id
JOIN ratings AS r
ON d.movie_id=r.movie_id
JOIN movie AS m
ON m.id=r.movie_id
JOIN avg_inter_days AS a
ON a.name_id=d.name_id
GROUP BY director_id
)
SELECT *
FROM final_result
LIMIT 9;