-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMDB+question_Harsha.sql
1564 lines (1271 loc) · 51.9 KB
/
IMDB+question_Harsha.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
IMDB Case Study by Harsha Vardhan Tamma
*/
USE imdb;
/* 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 table_name,
table_rows
FROM information_schema.tables
WHERE table_schema = 'imdb';
-- Q1 - Output:
/*
TABLE_NAME, TABLE_ROWS
'director_mapping','3867'
'genre','14662'
'movie','8344'
'names','23934'
'ratings','8230'
'role_mapping','15158'
*/
-- Q2. Which columns in the movie table have null values?
-- Type your code below:
SELECT Sum(CASE
WHEN id IS NULL THEN 1
ELSE 0
end) AS ID_nulls,
Sum(CASE
WHEN title IS NULL THEN 1
ELSE 0
end) AS title_nulls,
Sum(CASE
WHEN year IS NULL THEN 1
ELSE 0
end) AS year_nulls,
Sum(CASE
WHEN date_published IS NULL THEN 1
ELSE 0
end) AS date_published_nulls,
Sum(CASE
WHEN duration IS NULL THEN 1
ELSE 0
end) AS duration_nulls,
Sum(CASE
WHEN country IS NULL THEN 1
ELSE 0
end) AS country_nulls,
Sum(CASE
WHEN worlwide_gross_income IS NULL THEN 1
ELSE 0
end) AS worlwide_gross_income_nulls,
Sum(CASE
WHEN languages IS NULL THEN 1
ELSE 0
end) AS languages_nulls,
Sum(CASE
WHEN production_company IS NULL THEN 1
ELSE 0
end) AS production_company_nulls
FROM movie;
/*
Q2 - Output:
ID_nulls, title_nulls, year_nulls, date_published_nulls, duration_nulls, country_nulls, worlwide_gross_income_nulls, languages_nulls, production_company_nulls
'0','0','0','0','0','20','3724','194','528'
*
-- Null values in columns : country - 20, worldwide_gross_income : 3724, language : 194, production_company : 528
-- 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)
/* Output format for the first part:
+---------------+-------------------+
| Year | number_of_movies|
+-------------------+----------------
| 2017 | 2134 |
| 2018 | . |
| 2019 | . |
+---------------+-------------------+
Output format for the second part of the question:
+---------------+-------------------+
| month_num | number_of_movies|
+---------------+----------------
| 1 | 134 |
| 2 | 231 |
| . | . |
+---------------+-------------------+ */
-- Type your code below:
-- Total number of movies released each year
SELECT year as Year,
Count(id) AS number_of_movies_per_year
FROM movie
GROUP BY year
ORDER BY year;
/* Output :
Year, number_of_movies_per_year
'2017','3052'
'2018','2944'
'2019','2001'
*/
-- Total number of movies released per month
SELECT Month(date_published) AS Month,
Count(id) AS number_of_movies_per_month
FROM movie
GROUP BY Month
ORDER BY Month;
/* Output:
Month, number_of_movies_per_month
'1','804'
'2','640'
'3','824'
'4','680'
'5','625'
'6','580'
'7','493'
'8','678'
'9','809'
'10','801'
'11','625'
'12','438'
*/
/*The highest number of movies is produced in the month of March.
So, now that you have understood the month-wise trend of movies, let’s take a look at the other details in the movies table.
We know USA and India produces huge number of movies each year. Lets find the number of movies produced by USA or India for the last year.*/
-- Q4. How many movies were produced in the USA or India in the year 2019??
-- Type your code below:
-- Pattern matching using LIKE operator for country column
SELECT year AS Year,
Count(id) AS number_of_movies_produced
FROM movie
WHERE ( country LIKE '%INDIA%'
OR country LIKE '%USA%' )
AND year = 2019;
-- Q4. Solution : 1059 movies were produced in USA or India
/* Output
Year, number_of_movies_produced
'2019','1059'
*/
/* USA and India produced more than a thousand movies(you know the exact number!) in the year 2019.
Exploring table Genre would be fun!!
Let’s find out the different genres in the dataset.*/
-- Q5. Find the unique list of the genres present in the data set?
-- Type your code below:
SELECT DISTINCT genre AS 'Genre List'
FROM genre;
/* Output:
Genre List
'Drama'
'Fantasy'
'Thriller'
'Comedy'
'Horror'
'Family'
'Romance'
'Adventure'
'Action'
'Sci-Fi'
'Crime'
'Mystery'
'Others'
*/
/* 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. */
-- Q6.Which genre had the highest number of movies produced overall?
-- Type your code below:
SELECT genre,
Count(m.id) AS number_of_movies
FROM movie AS m
INNER JOIN genre AS g
where g.movie_id = m.id
GROUP BY genre
ORDER BY number_of_movies DESC
LIMIT 1 ;
/* Output
genre, number_of_movies
'Drama','4285'
*/
-- Drama genre had highest number of movies with a count of 4285.
/* 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.*/
-- Q7. How many movies belong to only one genre?
-- Type your code below:
WITH genre_count
AS (SELECT movie_id,
Count(DISTINCT genre) AS count_of_genre
FROM genre
GROUP BY movie_id)
SELECT Count(movie_id) AS 'Number of Movies with Single Genre'
FROM genre_count
WHERE count_of_genre = 1;
/* Output
Number of Movies with Single Genre
'3289'
*/
-- 3289 movies are of Single Genre
/* There are more than three thousand movies which has only one genre associated with them.
So, this figure appears significant.
Now, let's find out the possible duration of RSVP Movies’ next project.*/
-- Q8.What is the average duration of movies in each genre?
-- (Note: The same movie can belong to multiple genres.)
/* Output format:
+---------------+-------------------+
| genre | avg_duration |
+-------------------+----------------
| thriller | 105 |
| . | . |
| . | . |
+---------------+-------------------+ */
-- Type your code below:
-- Finding the average duration of movies by grouping the genres that movies belong to
-- Approach-1 : Use JOIN between movies and genre to get the data
SELECT genre AS 'Genre',
Round(Avg(duration),2) AS 'avg_duration'
FROM movie AS m
INNER JOIN genre AS g
ON m.id = g.movie_id
GROUP BY genre
ORDER BY avg_duration DESC;
/* Output
Genre, avg_duration
'Action','112.88'
'Romance','109.53'
'Crime','107.05'
'Drama','106.77'
'Fantasy','105.14'
'Comedy','102.62'
'Adventure','101.87'
'Mystery','101.80'
'Thriller','101.58'
'Family','100.97'
'Others','100.16'
'Sci-Fi','97.94'
'Horror','92.72'
*/
/* Now you know, movies of genre 'Drama' (produced highest in number in 2019) has the average duration of 106.77 mins.
Lets find where the movies of genre 'thriller' on the basis of number of movies.*/
-- 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 genre_summary
AS (SELECT genre,
Count(movie_id) AS movie_count,
Rank()
OVER(
ORDER BY Count(movie_id) DESC) AS genre_rank
FROM movie AS mv
LEFT JOIN genre AS gn
ON mv.id = gn.movie_id
GROUP BY genre)
SELECT *
FROM genre_summary
WHERE genre = "thriller";
/* Output
genre, movie_count, genre_rank
'Thriller','1484','3'
*/
-- Thriller is rank 3 with 1484 movies.
/*Thriller movies is in top 3 among all genres in terms of number of movies
In the previous segment, you analysed the movies and genres tables.
In this segment, you will analyse the ratings table as well.
To start with lets get the min and max values of different columns in the table*/
-- 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 ROUND(MIN(avg_rating))AS 'min_avg_rating',
ROUND(MAX(avg_rating)) AS 'max_avg_rating',
ROUND(MIN(total_votes)) AS 'min_total_votes',
ROUND(MAX(total_votes)) AS 'max_total_votes',
ROUND(MIN(median_rating)) AS 'min_median_rating',
ROUND(MAX(median_rating)) AS 'max_median_rating'
FROM ratings;
/* Output:
min_avg_rating, max_avg_rating, min_total_votes, max_total_votes, min_median_rating, max_median_rating
'1','10','100','725138','1','10'
*/
/* 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.*/
-- Q11. Which are the top 10 movies based on average rating?
/* Output format:
+---------------+-------------------+---------------------+
| title | avg_rating | movie_rank |
+---------------+-------------------+---------------------+
| Fan | 9.6 | 5 |
| . | . | . |
| . | . | . |
| . | . | . |
+---------------+-------------------+---------------------+*/
-- Type your code below:
-- It's ok if RANK() or DENSE_RANK() is used too
-- Finding the rank of each movie based on it's average rating
-- Approach-1:
-- Displaying the top 10 movies using CTE with LIMIT clause
WITH movie_ratings AS
(
SELECT mv.title,
rt.avg_rating
FROM movie AS mv
INNER JOIN ratings AS rt
ON mv.id = rt.movie_id )
SELECT title,
avg_rating,
Dense_rank() OVER(ORDER BY avg_rating DESC) AS 'movie_rank'
FROM movie_ratings LIMIT 10;
/* Output:
title, avg_rating, movie_rank
'Kirket','10.0','1'
'Love in Kilnerry','10.0','1'
'Gini Helida Kathe','9.8','2'
'Runam','9.7','3'
'Fan','9.6','4'
'Android Kunjappan Version 5.25','9.6','4'
'Yeh Suhaagraat Impossible','9.5','5'
'Safe','9.5','5'
'The Brighton Miracle','9.5','5'
'Shibu','9.4','6'
*/
-- Approach-2: Displaying top 10 RANKED movies using CTE with a condition on Rank
WITH movie_ratings
AS (SELECT mv.title,
rt.avg_rating,
Dense_rank()
OVER(
ORDER BY avg_rating DESC) AS 'movie_rank'
FROM movie AS mv
INNER JOIN ratings AS rt
ON mv.id = rt.movie_id)
SELECT title,
avg_rating,
movie_rank
FROM movie_ratings
WHERE movie_rank <= 10;
/* Output:
'Kirket','10.0','1'
'Love in Kilnerry','10.0','1'
'Gini Helida Kathe','9.8','2'
'Runam','9.7','3'
'Fan','9.6','4'
'Android Kunjappan Version 5.25','9.6','4'
'Yeh Suhaagraat Impossible','9.5','5'
'Safe','9.5','5'
'The Brighton Miracle','9.5','5'
'Shibu','9.4','6'
'Our Little Haven','9.4','6'
'Zana','9.4','6'
'Family of Thakurganj','9.4','6'
'Ananthu V/S Nusrath','9.4','6'
'Eghantham','9.3','7'
'Wheels','9.3','7'
'Turnover','9.2','8'
'Digbhayam','9.2','8'
'Tõde ja õigus','9.2','8'
'Ekvtime: Man of God','9.2','8'
'Leera the Soulmate','9.2','8'
'AA BB KK','9.2','8'
'Peranbu','9.2','8'
'Dokyala Shot','9.2','8'
'Ardaas Karaan','9.2','8'
'Kuasha jakhon','9.1','9'
'Oththa Seruppu Size 7','9.1','9'
'Adutha Chodyam','9.1','9'
'The Colour of Darkness','9.1','9'
'Aloko Udapadi','9.1','9'
'C/o Kancharapalem','9.1','9'
'Nagarkirtan','9.1','9'
'Jelita Sejuba: Mencintai Kesatria Negara','9.1','9'
'Shindisi','9.0','10'
'Officer Arjun Singh IPS','9.0','10'
'Oskars Amerika','9.0','10'
'Delaware Shore','9.0','10'
'Abstruse','9.0','10'
'National Theatre Live: Angels in America Part Two - Perestroika','9.0','10'
'Innocent','9.0','10'
*/
/* While selecting based on Rank, we see a lot of entries when compared to limit. Many movies have same rating which were given equal rank
However, if we need to get fair idea on top 10 ranked movies, it's better to avoid limit*/
/* 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
-- Finding the number of movies based on median rating and sorting based on movie count.
-- Approach-1: Getting data using Join between movie and ratings
SELECT r.median_rating,
Count(m.id) AS 'movie_count'
FROM ratings AS r
LEFT JOIN movie AS m
ON r.movie_id = m.id
GROUP BY r.median_rating
ORDER BY movie_count;
-- Approach-2: Getting data directly from ratings table
SELECT median_rating,
Count(movie_id) AS movie_count
FROM ratings
GROUP BY median_rating
ORDER BY movie_count;
/* Output:
median_rating, movie_count
'1','94'
'2','119'
'3','283'
'10','346'
'9','429'
'4','479'
'5','985'
'8','1030'
'6','1975'
'7','2257'
*/
/* 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:
-- Using CTE to find the rank of production company based on movie count with average rating > 8 using DENSE_RANK function.
-- Querying the CTE to find the production company with rank=1
WITH production_movie_count
AS (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)
SELECT *
FROM production_movie_count
WHERE prod_company_rank = 1;
/* Output:
production_company, movie_count, prod_company_rank
'Dream Warrior Pictures','3','1'
'National Theatre Live','3','1'
*/
-- It's ok if RANK() or DENSE_RANK() is used too
-- Answer can be Dream Warrior Pictures or National Theatre Live or both
-- 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(m.id) AS movie_count
FROM movie AS m
INNER JOIN genre AS g
ON g.movie_id = m.id
INNER JOIN ratings AS r
ON r.movie_id = m.id
WHERE year = 2017
AND Month(date_published) = 3
AND country LIKE '%USA%'
AND total_votes > 1000
GROUP BY genre
ORDER BY movie_count DESC;
/* Output:
genre, movie_count
'Drama','24'
'Comedy','9'
'Action','8'
'Thriller','8'
'Sci-Fi','7'
'Crime','6'
'Horror','6'
'Mystery','4'
'Romance','4'
'Fantasy','3'
'Adventure','3'
'Family','1'
*/
-- 24 Drama movies were released during March 2017 in the USA which had more than 1,000 votes
-- Top 3 genres are drama, comedy and action during March 2017 in the USA which had more than 1,000 votes
-- Lets try to analyse with a unique problem statement.
-- Q15. Find movies of each genre that start with the word ‘The’ and which have an average rating > 8?
/* Output format:
+---------------+-------------------+---------------------+
| title | avg_rating | genre |
+---------------+-------------------+---------------------+
| Theeran | 8.3 | Thriller |
| . | . | . |
| . | . | . |
| . | . | . |
+---------------+-------------------+---------------------+*/
-- Type your code below:
-- Approach-1 : Using LIKE Operator
SELECT title,
avg_rating,
genre
FROM movie AS m
INNER JOIN genre AS g
ON g.movie_id = m.id
INNER JOIN ratings AS r
ON r.movie_id = m.id
WHERE avg_rating > 8
AND title LIKE 'The%'
ORDER BY avg_rating DESC;
-- Approach-1 : Using REGEX Operator
SELECT m.title, r.avg_rating, g.genre
FROM movie AS m
INNER JOIN genre AS g
ON g.movie_id = m.id
INNER JOIN ratings AS r
ON r.movie_id = m.id
WHERE title REGEXP '^The'
AND avg_rating > 8
ORDER BY r.avg_rating DESC;
/* Output:
title, avg_rating, genre
'The Brighton Miracle','9.5','Drama'
'The Colour of Darkness','9.1','Drama'
'The Blue Elephant 2','8.8','Drama'
'The Blue Elephant 2','8.8','Horror'
'The Blue Elephant 2','8.8','Mystery'
'The Irishman','8.7','Crime'
'The Irishman','8.7','Drama'
'The Mystery of Godliness: The Sequel','8.5','Drama'
'The Gambinos','8.4','Crime'
'The Gambinos','8.4','Drama'
'Theeran Adhigaaram Ondru','8.3','Action'
'Theeran Adhigaaram Ondru','8.3','Crime'
'Theeran Adhigaaram Ondru','8.3','Thriller'
'The King and I','8.2','Drama'
'The King and I','8.2','Romance'
*/
/*
The above code returns list of all movies starting with 'The' and having ratings > 8.
However, we will see that same movie belongs to more than one genre.
If we try to group by title, MySQL rejects the query for which the select list, HAVING condition, or ORDER BY list refer to non-aggregated columns
that are neither named in the GROUP BY clause nor are functionally dependent on them.
This is dependent on a SQL setting called "sql_mode" which is set to "ONLY_FULL_GROUP_BY" by default.
To work around this, we have to set the sql_mode variable.
If we set the SQL setting - SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));, we can temporarily disable this error for that SQL session.
MYSQL will pick one of the two possible results RANDOMLY!!. However, this is not recommended as it modifies the select functionality.
*/
SHOW session variables LIKE '%sql_mode%';
-- Output: 'sql_mode','ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
-- Replace ONLY_FULL_GROUP_BY with ''
SET session sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
SHOW session variables LIKE '%sql_mode%';
-- Output: 'sql_mode', 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
-- Rewriting the code again grouping by title
SELECT title,
avg_rating,
genre
FROM movie AS M
INNER JOIN genre AS G
ON G.movie_id = M.id
INNER JOIN ratings AS R
ON R.movie_id = M.id
WHERE avg_rating > 8
AND title LIKE 'The%'
GROUP BY title
ORDER BY avg_rating DESC;
/* Output:
title, avg_rating, genre
'The Brighton Miracle','9.5','Drama'
'The Colour of Darkness','9.1','Drama'
'The Blue Elephant 2','8.8','Drama'
'The Irishman','8.7','Crime'
'The Mystery of Godliness: The Sequel','8.5','Drama'
'The Gambinos','8.4','Crime'
'Theeran Adhigaaram Ondru','8.3','Action'
'The King and I','8.2','Drama'
*/
-- Restoring the sql_mode value
set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
SHOW session variables LIKE '%sql_mode%';
-- Output: 'sql_mode','ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
/*
##################################################################################################################
Note:
The above workaround will not be used in subsequent queries as we are not sure whether it is a recommended approach.
The last/above implementation is just to check whether the changes work or not.
##################################################################################################################
*/
-- You should also try your hand at median rating and check whether the ‘median rating’ column gives any significant insights.
-- Q16. Of the movies released between 1 April 2018 and 1 April 2019, how many were given a median rating of 8?
-- Type your code below:
-- Using BETWEEN to find the movies released between 1 April 2018 and 1 April 2019
SELECT median_rating, Count(movie_id) AS movie_count
FROM movie AS m
INNER JOIN ratings AS r
ON r.movie_id = m.id
WHERE median_rating = 8
AND date_published BETWEEN '2018-04-01' AND '2019-04-01'
GROUP BY median_rating;
/* Output:
median_rating, movie_count
'8','361'
*/
-- 361 movies were released between 1 April 2018 and 1 April 2019 which were given a median rating of 8.
-- Once again, try to solve the problem given below.
-- Q17. Do German movies get more votes than Italian movies?
-- Hint: Here you have to find the total number of votes for both German and Italian movies.
-- Type your code below:
-- Approach-1:
-- Comparing total votes with respect to country of origin, between Germany and Italy
SELECT country,
Sum(total_votes) AS Total_votes
FROM ratings r
INNER JOIN movie m
ON r.movie_id = m.id
WHERE country IN ( 'Germany', 'Italy' )
GROUP BY country;
/* Output:
country, Total_votes
'Germany','106710'
'Italy','77965'
*/
-- Approach-2:
-- Comparing total votes with respect to language the movies are available in, between German and Italian
WITH german_movies
AS (SELECT languages,
SUM(total_votes) AS Total_votes
FROM ratings r
inner join movie m
ON r.movie_id = m.id
WHERE languages LIKE '%German%'
GROUP BY languages) SELECT 'German' AS LANGUAGE,
SUM(total_votes) AS Total_votes
FROM german_movies
UNION
(
WITH italian_movies
AS (SELECT languages,
SUM(total_votes) AS Total_votes
FROM ratings r
inner join movie m
ON r.movie_id = m.id
WHERE languages LIKE '%Italian%'
GROUP BY languages)
SELECT 'Italian' AS LANGUAGE,
SUM(total_votes) AS Total_votes
FROM italian_movies);
/* Output:
LANGUAGE, Total_votes
'German','4421525'
'Italian','2559540'
*/
-- In both of these cases, German movies get more votes than Italian movies.
-- 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:
-- Q18. Which columns in the names table have null values??
/*Hint: You can find null values for individual columns or follow below output format
+---------------+-------------------+---------------------+----------------------+
| name_nulls | height_nulls |date_of_birth_nulls |known_for_movies_nulls|
+---------------+-------------------+---------------------+----------------------+
| 0 | 123 | 1234 | 12345 |
+---------------+-------------------+---------------------+----------------------+*/
-- Type your code below:
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;
/* Output:
name_nulls, height_nulls, date_of_birth_nulls, known_for_movies_nulls
'0','17335','13431','15226'
*/
-- Height, date of birth, known for movies have null values
/* There are no Null value in the column 'name'.
The director is the most important person in a movie crew.
Let’s find out the top three directors in the top three genres who can be hired by RSVP Movies.*/
-- 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:
-- using CTE with genre ranking
WITH top_genres AS
(
SELECT genre
FROM genre
INNER JOIN ratings
using (movie_id)
WHERE avg_rating > 8
GROUP BY genre
ORDER BY Count(movie_id) DESC limit 3 ),
-- top 3 directors based on total number of movies in top 3 genres using CTE
top_directors AS
(
SELECT n.NAME AS director_name,
Count(g.movie_id) AS movie_count,
Rank() OVER(ORDER BY Count(g.movie_id) DESC) AS director_rank
FROM genre g
INNER JOIN director_mapping dm
ON g.movie_id = dm.movie_id
INNER JOIN names n
ON dm.name_id = n.id
INNER JOIN ratings r
ON g.movie_id = r.movie_id,
top_genres
WHERE avg_rating > 8
AND g.genre IN (top_genres.genre)
GROUP BY n.NAME )
SELECT director_name,
movie_count
FROM top_directors
WHERE director_rank <=3;
/* Did not use LIMIT clause as it will only give top 'n' records as output irrespective of movie_count.
Here Soubin Shahir, Joe Russo and Anthony Russo have same movie_count and therefore it's important that we display
them all intead of just first 3.*/
/* James Mangold can be hired as the director for RSVP's next project. Do you remember 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 n.name AS actor_name,
Count(r.movie_id) AS movie_count
FROM names n
INNER JOIN role_mapping rm
ON n.id = rm.name_id
INNER JOIN ratings r
ON rm.movie_id = r.movie_id
WHERE median_rating >= 8
GROUP BY n.name
ORDER BY movie_count DESC
LIMIT 2;
/* Output:
actor_name, movie_count
'Mammootty','8'
'Mohanlal','5'
*/
/*Used limit clause here as the movie_count corresponding to 3rd actor is less than that of second actor.
It can be easily verified by setting LIMIT as 3.*/
-- Mammootty is the top actor based on total number of movies with median rating greater than 8. He is followed by Mohanlal.
/* 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.