-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy path13.Many To Many.sql
241 lines (215 loc) · 7.72 KB
/
13.Many To Many.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
/*Series Reviews*/
CREATE TABLE reviewers(
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(150),
last_name VARCHAR(150)
);
CREATE TABLE series(
id INT auto_increment PRIMARY KEY,
title VARCHAR(200),
released_year YEAR(4),
genre VARCHAR(100)
);
CREATE TABLE reviews(
id INT AUTO_INCREMENT PRIMARY KEY,
rating DECIMAL (2,1),
reviewer_id INT,
series_id INT,
FOREIGN KEY(reviewer_id) REFERENCES reviewers(id),
FOREIGN KEY(series_id) REFERENCES series(id)
);
INSERT INTO series (title, released_year, genre) VALUES
('Archer', 2009, 'Animation'),
('Arrested Development', 2003, 'Comedy'),
("Bob's Burgers", 2011, 'Animation'),
('Bojack Horseman', 2014, 'Animation'),
("Breaking Bad", 2008, 'Drama'),
('Curb Your Enthusiasm', 2000, 'Comedy'),
("Fargo", 2014, 'Drama'),
('Freaks and Geeks', 1999, 'Comedy'),
('General Hospital', 1963, 'Drama'),
('Halt and Catch Fire', 2014, 'Drama'),
('Malcolm In The Middle', 2000, 'Comedy'),
('Pushing Daisies', 2007, 'Comedy'),
('Seinfeld', 1989, 'Comedy'),
('Stranger Things', 2016, 'Drama');
INSERT INTO reviewers (first_name, last_name) VALUES
('Thomas', 'Stoneman'),
('Wyatt', 'Skaggs'),
('Kimbra', 'Masters'),
('Domingo', 'Cortes'),
('Colt', 'Steele'),
('Pinkie', 'Petit'),
('Marlon', 'Crafford');
INSERT INTO reviews(series_id, reviewer_id, rating) VALUES
(1,1,8.0),(1,2,7.5),(1,3,8.5),(1,4,7.7),(1,5,8.9),
(2,1,8.1),(2,4,6.0),(2,3,8.0),(2,6,8.4),(2,5,9.9),
(3,1,7.0),(3,6,7.5),(3,4,8.0),(3,3,7.1),(3,5,8.0),
(4,1,7.5),(4,3,7.8),(4,4,8.3),(4,2,7.6),(4,5,8.5),
(5,1,9.5),(5,3,9.0),(5,4,9.1),(5,2,9.3),(5,5,9.9),
(6,2,6.5),(6,3,7.8),(6,4,8.8),(6,2,8.4),(6,5,9.1),
(7,2,9.1),(7,5,9.7),
(8,4,8.5),(8,2,7.8),(8,6,8.8),(8,5,9.3),
(9,2,5.5),(9,3,6.8),(9,4,5.8),(9,6,4.3),(9,5,4.5),
(10,5,9.9),
(13,3,8.0),(13,4,7.2),
(14,2,8.5),(14,3,8.9),(14,4,8.9);
/*------------------------------*/
/*------------- Challenges --------------------*/
/*+----------------------+--------+
| title | rating |
+----------------------+--------+
| Archer | 8.0 |
| Archer | 7.5 |
| Archer | 8.5 |
| Archer | 7.7 |
| Archer | 8.9 |
| Arrested Development | 8.1 |
| Arrested Development | 6.0 |
| Arrested Development | 8.0 |
| Arrested Development | 8.4 |
| Arrested Development | 9.9 |
| Bob's Burgers | 7.0 |
...
+----------------------+--------+
*/
SELECT title,rating
FROM series
JOIN reviews ON series.id = reviews.series_id;
/*+----------------------+------------+
| title | avg_rating |
+----------------------+------------+
| General Hospital | 5.38000 |
| Bob's Burgers | 7.52000 |
| Seinfeld | 7.60000 |
| Bojack Horseman | 7.94000 |
| Arrested Development | 8.08000 |
| Curb Your Enthusiasm | 8.12000 |
| Archer | 8.12000 |
| Freaks and Geeks | 8.60000 |
| Stranger Things | 8.76667 |
| Breaking Bad | 9.36000 |
| Fargo | 9.40000 |
| Halt and Catch Fire | 9.90000 |
+----------------------+------------+
*/
/*+----------------------+------------+
| title | avg_rating |
+----------------------+------------+
| General Hospital | 5.38000 |
| Bob's Burgers | 7.52000 |
| Seinfeld | 7.60000 |
| Bojack Horseman | 7.94000 |
| Arrested Development | 8.08000 |
| Curb Your Enthusiasm | 8.12000 |
| Archer | 8.12000 |
| Freaks and Geeks | 8.60000 |
| Stranger Things | 8.76667 |
| Breaking Bad | 9.36000 |
| Fargo | 9.40000 |
| Halt and Catch Fire | 9.90000 |
+----------------------+------------+
*/
SELECT title,AVG(rating) AS avg_rating
FROM series
JOIN reviews ON series.id = reviews.series_id
GROUP BY series.id
ORDER BY avg_rating;
/*
+------------+-----------+--------+
| first_name | last_name | rating |
+------------+-----------+--------+
| Thomas | Stoneman | 8.0 |
| Thomas | Stoneman | 8.1 |
| Thomas | Stoneman | 7.0 |
| Thomas | Stoneman | 7.5 |
| Thomas | Stoneman | 9.5 |
| Wyatt | Skaggs | 7.5 |
| Wyatt | Skaggs | 7.6 |
| Wyatt | Skaggs | 9.3 |
| Wyatt | Skaggs | 6.5 |
| Wyatt | Skaggs | 8.4 |
| Wyatt | Skaggs | 9.1 |
| Wyatt | Skaggs | 7.8 |
| Wyatt | Skaggs | 5.5 |
| Wyatt | Skaggs | 8.5 |
| Kimbra | Masters | 8.5 |
| Kimbra | Masters | 8.0 |
| Kimbra | Masters | 7.1 |
| Kimbra | Masters | 7.8 |
| Kimbra | Masters | 9.0 |
| Kimbra | Masters | 7.8 |
+------------+-----------+--------+
*/
SELECT first_name,last_name
FROM reviewers
JOIN reviews ON reviewers.id = reviews.reviewer_id;
/*+-----------------------+
| unreviewed_series |
+-----------------------+
| Malcolm In The Middle |
| Pushing Daisies |
+-----------------------+*/
SELECT title AS unreviewed_series
FROM series
LEFT JOIN reviews ON series.id = reviews.series_id
WHERE reviews.id IS NULL;
/*+-----------+------------+
| genre | avg_rating |
+-----------+------------+
| Animation | 7.86000 |
| Comedy | 8.16250 |
| Drama | 8.04375 |
+-----------+------------+*/
SELECT genre, ROUND(AVG(rating),2) AS avg_rating
FROM series
JOIN reviews ON series.id = reviews.series_id
GROUP BY genre;
/*+------------+-----------+-------+-----+-----+---------+----------+
| first_name | last_name | COUNT | MIN | MAX | AVG | STATUS |
+------------+-----------+-------+-----+-----+---------+----------+
| Thomas | Stoneman | 5 | 7.0 | 9.5 | 8.02000 | ACTIVE |
| Wyatt | Skaggs | 9 | 5.5 | 9.3 | 7.80000 | ACTIVE |
| Kimbra | Masters | 9 | 6.8 | 9.0 | 7.98889 | ACTIVE |
| Domingo | Cortes | 10 | 5.8 | 9.1 | 7.83000 | ACTIVE |
| Colt | Steele | 10 | 4.5 | 9.9 | 8.77000 | ACTIVE |
| Pinkie | Petit | 4 | 4.3 | 8.8 | 7.25000 | ACTIVE |
| Marlon | Crafford | 0 | 0.0 | 0.0 | 0.00000 | INACTIVE |
+------------+-----------+-------+-----+-----+---------+----------+*/
SELECT first_name,last_name,
COUNT(rating) AS 'COUNT',
IFNULL(MIN(rating),0) AS 'MIN',
IFNULL(MAX(rating),0) AS 'MAX',
IFNULL(ROUND(AVG(rating),2),0) AS 'AVG',
CASE
WHEN COUNT(rating) > 10 THEN 'POWER USER'
WHEN COUNT(rating) > 0 THEN 'ACTIVE'
ELSE 'INACTIVE'
END AS 'STATUS'
FROM reviewers
LEFT JOIN reviews ON reviewers.id = reviews.reviewer_id
GROUP BY reviewers.id;
/*+----------------------+--------+-----------------+
| title | rating | reviewer |
+----------------------+--------+-----------------+
| Archer | 8.0 | Thomas Stoneman |
| Archer | 7.7 | Domingo Cortes |
| Archer | 8.5 | Kimbra Masters |
| Archer | 7.5 | Wyatt Skaggs |
| Archer | 8.9 | Colt Steele |
| Arrested Development | 8.4 | Pinkie Petit |
| Arrested Development | 9.9 | Colt Steele |
| Arrested Development | 8.1 | Thomas Stoneman |
| Arrested Development | 6.0 | Domingo Cortes |
| Arrested Development | 8.0 | Kimbra Masters |
| Bob's Burgers | 7.0 | Thomas Stoneman |
| Bob's Burgers | 8.0 | Domingo Cortes |
| Bob's Burgers | 7.1 | Kimbra Masters |
| Bob's Burgers | 7.5 | Pinkie Petit |
| Bob's Burgers | 8.0 | Colt Steele |
+----------------------+--------+-----------------+*/
SELECT title,rating,CONCAT(first_name,' ',last_name) AS 'reviewer'
FROM series
JOIN reviews ON series.id = reviews.series_id
JOIN reviewers ON reviewers.id = reviews.reviewer_id
ORDER BY title, rating DESC;