forked from Code-Social/Query-quest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovie.sql
More file actions
84 lines (74 loc) · 2.21 KB
/
Movie.sql
File metadata and controls
84 lines (74 loc) · 2.21 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
#1.a Movie & Director
SELECT M.title, M.release_date, D.name AS director_name
FROM Movies M
JOIN Directors D ON M.director_id = D.director_id;
#1.b Original Content
SELECT title
FROM Movies
WHERE is_original = TRUE;
#1.c Series Overview
SELECT title, start_date, seasons
FROM WebSeries;
#2.a Actor Film Counts
SELECT A.actor_id, A.name, COUNT(MC.movie_id) AS movie_count
FROM Actors A
LEFT JOIN MovieCast MC ON A.actor_id = MC.actor_id
GROUP BY A.actor_id, A.name
ORDER BY movie_count DESC;
#2.b Genre Popularity
SELECT G.genre_id, G.name, COUNT(MG.movie_id) AS movie_count
FROM Genres G
LEFT JOIN MovieGenres MG ON G.genre_id = MG.genre_id
GROUP BY G.genre_id, G.name;
#2.c Series Reviews
SELECT WS.series_id, WS.title, AVG(R.rating) AS average_rating
FROM Reviews R
JOIN WebSeries WS ON R.content_id = WS.series_id
WHERE R.content_type = 'SERIES'
GROUP BY WS.series_id, WS.title
HAVING COUNT(R.review_id) >= 2;
#2.d Episode Listing
SELECT season_number, episode_number, title, air_date
FROM Episodes
WHERE series_id = 101
ORDER BY season_number, episode_number;
#3.a Top-Rated Movies
WITH MovieRatings AS (
SELECT M.movie_id, M.title, AVG(R.rating) AS average_rating
FROM Movies M
JOIN Reviews R ON M.movie_id = R.content_id
WHERE R.content_type = 'MOVIE'
GROUP BY M.movie_id, M.title
)
SELECT movie_id, title, average_rating,
RANK() OVER (ORDER BY average_rating DESC) AS rank
FROM MovieRatings;
#3.b Cross-Type Top Content
WITH MovieAvg AS (
SELECT 'MOVIE' AS content_type, M.title, AVG(R.rating) AS avg_rating
FROM Movies M
JOIN Reviews R ON M.movie_id = R.content_id
WHERE R.content_type = 'MOVIE'
GROUP BY M.title
),
SeriesAvg AS (
SELECT 'SERIES' AS content_type, S.title, AVG(R.rating) AS avg_rating
FROM WebSeries S
JOIN Reviews R ON S.series_id = R.content_id
WHERE R.content_type = 'SERIES'
GROUP BY S.title
)
SELECT * FROM (
SELECT * FROM MovieAvg
UNION
SELECT * FROM SeriesAvg
) AS Combined
WHERE avg_rating >= 9.0;
#3.c Director Impact
SELECT D.name AS director_name, AVG(R.rating) AS average_movie_rating
FROM Directors D
JOIN Movies M ON D.director_id = M.director_id
JOIN Reviews R ON M.movie_id = R.content_id
WHERE R.content_type = 'MOVIE'
GROUP BY D.name
HAVING AVG(R.rating) >= 8.5;