Skip to content

Commit fd51530

Browse files
committed
add many to many relationship
1 parent 104edff commit fd51530

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed

Ultimate MySQL/13.Many To Many.sql

+241
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
/*Series Reviews*/
2+
CREATE TABLE reviewers(
3+
id INT AUTO_INCREMENT PRIMARY KEY,
4+
first_name VARCHAR(150),
5+
last_name VARCHAR(150)
6+
);
7+
8+
CREATE TABLE series(
9+
id INT auto_increment PRIMARY KEY,
10+
title VARCHAR(200),
11+
released_year YEAR(4),
12+
genre VARCHAR(100)
13+
);
14+
15+
CREATE TABLE reviews(
16+
id INT AUTO_INCREMENT PRIMARY KEY,
17+
rating DECIMAL (2,1),
18+
reviewer_id INT,
19+
series_id INT,
20+
FOREIGN KEY(reviewer_id) REFERENCES reviewers(id),
21+
FOREIGN KEY(series_id) REFERENCES series(id)
22+
);
23+
24+
25+
INSERT INTO series (title, released_year, genre) VALUES
26+
('Archer', 2009, 'Animation'),
27+
('Arrested Development', 2003, 'Comedy'),
28+
("Bob's Burgers", 2011, 'Animation'),
29+
('Bojack Horseman', 2014, 'Animation'),
30+
("Breaking Bad", 2008, 'Drama'),
31+
('Curb Your Enthusiasm', 2000, 'Comedy'),
32+
("Fargo", 2014, 'Drama'),
33+
('Freaks and Geeks', 1999, 'Comedy'),
34+
('General Hospital', 1963, 'Drama'),
35+
('Halt and Catch Fire', 2014, 'Drama'),
36+
('Malcolm In The Middle', 2000, 'Comedy'),
37+
('Pushing Daisies', 2007, 'Comedy'),
38+
('Seinfeld', 1989, 'Comedy'),
39+
('Stranger Things', 2016, 'Drama');
40+
41+
42+
INSERT INTO reviewers (first_name, last_name) VALUES
43+
('Thomas', 'Stoneman'),
44+
('Wyatt', 'Skaggs'),
45+
('Kimbra', 'Masters'),
46+
('Domingo', 'Cortes'),
47+
('Colt', 'Steele'),
48+
('Pinkie', 'Petit'),
49+
('Marlon', 'Crafford');
50+
51+
INSERT INTO reviews(series_id, reviewer_id, rating) VALUES
52+
(1,1,8.0),(1,2,7.5),(1,3,8.5),(1,4,7.7),(1,5,8.9),
53+
(2,1,8.1),(2,4,6.0),(2,3,8.0),(2,6,8.4),(2,5,9.9),
54+
(3,1,7.0),(3,6,7.5),(3,4,8.0),(3,3,7.1),(3,5,8.0),
55+
(4,1,7.5),(4,3,7.8),(4,4,8.3),(4,2,7.6),(4,5,8.5),
56+
(5,1,9.5),(5,3,9.0),(5,4,9.1),(5,2,9.3),(5,5,9.9),
57+
(6,2,6.5),(6,3,7.8),(6,4,8.8),(6,2,8.4),(6,5,9.1),
58+
(7,2,9.1),(7,5,9.7),
59+
(8,4,8.5),(8,2,7.8),(8,6,8.8),(8,5,9.3),
60+
(9,2,5.5),(9,3,6.8),(9,4,5.8),(9,6,4.3),(9,5,4.5),
61+
(10,5,9.9),
62+
(13,3,8.0),(13,4,7.2),
63+
(14,2,8.5),(14,3,8.9),(14,4,8.9);
64+
65+
/*------------------------------*/
66+
67+
/*------------- Challenges --------------------*/
68+
69+
/*+----------------------+--------+
70+
| title | rating |
71+
+----------------------+--------+
72+
| Archer | 8.0 |
73+
| Archer | 7.5 |
74+
| Archer | 8.5 |
75+
| Archer | 7.7 |
76+
| Archer | 8.9 |
77+
| Arrested Development | 8.1 |
78+
| Arrested Development | 6.0 |
79+
| Arrested Development | 8.0 |
80+
| Arrested Development | 8.4 |
81+
| Arrested Development | 9.9 |
82+
| Bob's Burgers | 7.0 |
83+
...
84+
+----------------------+--------+
85+
*/
86+
87+
SELECT title,rating
88+
FROM series
89+
JOIN reviews ON series.id = reviews.series_id;
90+
91+
92+
93+
/*+----------------------+------------+
94+
| title | avg_rating |
95+
+----------------------+------------+
96+
| General Hospital | 5.38000 |
97+
| Bob's Burgers | 7.52000 |
98+
| Seinfeld | 7.60000 |
99+
| Bojack Horseman | 7.94000 |
100+
| Arrested Development | 8.08000 |
101+
| Curb Your Enthusiasm | 8.12000 |
102+
| Archer | 8.12000 |
103+
| Freaks and Geeks | 8.60000 |
104+
| Stranger Things | 8.76667 |
105+
| Breaking Bad | 9.36000 |
106+
| Fargo | 9.40000 |
107+
| Halt and Catch Fire | 9.90000 |
108+
+----------------------+------------+
109+
*/
110+
/*+----------------------+------------+
111+
| title | avg_rating |
112+
+----------------------+------------+
113+
| General Hospital | 5.38000 |
114+
| Bob's Burgers | 7.52000 |
115+
| Seinfeld | 7.60000 |
116+
| Bojack Horseman | 7.94000 |
117+
| Arrested Development | 8.08000 |
118+
| Curb Your Enthusiasm | 8.12000 |
119+
| Archer | 8.12000 |
120+
| Freaks and Geeks | 8.60000 |
121+
| Stranger Things | 8.76667 |
122+
| Breaking Bad | 9.36000 |
123+
| Fargo | 9.40000 |
124+
| Halt and Catch Fire | 9.90000 |
125+
+----------------------+------------+
126+
*/
127+
SELECT title,AVG(rating) AS avg_rating
128+
FROM series
129+
JOIN reviews ON series.id = reviews.series_id
130+
GROUP BY series.id
131+
ORDER BY avg_rating;
132+
133+
134+
/*
135+
+------------+-----------+--------+
136+
| first_name | last_name | rating |
137+
+------------+-----------+--------+
138+
| Thomas | Stoneman | 8.0 |
139+
| Thomas | Stoneman | 8.1 |
140+
| Thomas | Stoneman | 7.0 |
141+
| Thomas | Stoneman | 7.5 |
142+
| Thomas | Stoneman | 9.5 |
143+
| Wyatt | Skaggs | 7.5 |
144+
| Wyatt | Skaggs | 7.6 |
145+
| Wyatt | Skaggs | 9.3 |
146+
| Wyatt | Skaggs | 6.5 |
147+
| Wyatt | Skaggs | 8.4 |
148+
| Wyatt | Skaggs | 9.1 |
149+
| Wyatt | Skaggs | 7.8 |
150+
| Wyatt | Skaggs | 5.5 |
151+
| Wyatt | Skaggs | 8.5 |
152+
| Kimbra | Masters | 8.5 |
153+
| Kimbra | Masters | 8.0 |
154+
| Kimbra | Masters | 7.1 |
155+
| Kimbra | Masters | 7.8 |
156+
| Kimbra | Masters | 9.0 |
157+
| Kimbra | Masters | 7.8 |
158+
+------------+-----------+--------+
159+
*/
160+
161+
SELECT first_name,last_name
162+
FROM reviewers
163+
JOIN reviews ON reviewers.id = reviews.reviewer_id;
164+
165+
166+
/*+-----------------------+
167+
| unreviewed_series |
168+
+-----------------------+
169+
| Malcolm In The Middle |
170+
| Pushing Daisies |
171+
+-----------------------+*/
172+
SELECT title AS unreviewed_series
173+
FROM series
174+
LEFT JOIN reviews ON series.id = reviews.series_id
175+
WHERE reviews.id IS NULL;
176+
177+
178+
/*+-----------+------------+
179+
| genre | avg_rating |
180+
+-----------+------------+
181+
| Animation | 7.86000 |
182+
| Comedy | 8.16250 |
183+
| Drama | 8.04375 |
184+
+-----------+------------+*/
185+
SELECT genre, ROUND(AVG(rating),2) AS avg_rating
186+
FROM series
187+
JOIN reviews ON series.id = reviews.series_id
188+
GROUP BY genre;
189+
190+
191+
/*+------------+-----------+-------+-----+-----+---------+----------+
192+
| first_name | last_name | COUNT | MIN | MAX | AVG | STATUS |
193+
+------------+-----------+-------+-----+-----+---------+----------+
194+
| Thomas | Stoneman | 5 | 7.0 | 9.5 | 8.02000 | ACTIVE |
195+
| Wyatt | Skaggs | 9 | 5.5 | 9.3 | 7.80000 | ACTIVE |
196+
| Kimbra | Masters | 9 | 6.8 | 9.0 | 7.98889 | ACTIVE |
197+
| Domingo | Cortes | 10 | 5.8 | 9.1 | 7.83000 | ACTIVE |
198+
| Colt | Steele | 10 | 4.5 | 9.9 | 8.77000 | ACTIVE |
199+
| Pinkie | Petit | 4 | 4.3 | 8.8 | 7.25000 | ACTIVE |
200+
| Marlon | Crafford | 0 | 0.0 | 0.0 | 0.00000 | INACTIVE |
201+
+------------+-----------+-------+-----+-----+---------+----------+*/
202+
SELECT first_name,last_name,
203+
COUNT(rating) AS 'COUNT',
204+
IFNULL(MIN(rating),0) AS 'MIN',
205+
IFNULL(MAX(rating),0) AS 'MAX',
206+
IFNULL(ROUND(AVG(rating),2),0) AS 'AVG',
207+
CASE
208+
WHEN COUNT(rating) > 10 THEN 'POWER USER'
209+
WHEN COUNT(rating) > 0 THEN 'ACTIVE'
210+
ELSE 'INACTIVE'
211+
END AS 'STATUS'
212+
FROM reviewers
213+
LEFT JOIN reviews ON reviewers.id = reviews.reviewer_id
214+
GROUP BY reviewers.id;
215+
216+
217+
218+
/*+----------------------+--------+-----------------+
219+
| title | rating | reviewer |
220+
+----------------------+--------+-----------------+
221+
| Archer | 8.0 | Thomas Stoneman |
222+
| Archer | 7.7 | Domingo Cortes |
223+
| Archer | 8.5 | Kimbra Masters |
224+
| Archer | 7.5 | Wyatt Skaggs |
225+
| Archer | 8.9 | Colt Steele |
226+
| Arrested Development | 8.4 | Pinkie Petit |
227+
| Arrested Development | 9.9 | Colt Steele |
228+
| Arrested Development | 8.1 | Thomas Stoneman |
229+
| Arrested Development | 6.0 | Domingo Cortes |
230+
| Arrested Development | 8.0 | Kimbra Masters |
231+
| Bob's Burgers | 7.0 | Thomas Stoneman |
232+
| Bob's Burgers | 8.0 | Domingo Cortes |
233+
| Bob's Burgers | 7.1 | Kimbra Masters |
234+
| Bob's Burgers | 7.5 | Pinkie Petit |
235+
| Bob's Burgers | 8.0 | Colt Steele |
236+
+----------------------+--------+-----------------+*/
237+
SELECT title,rating,CONCAT(first_name,' ',last_name) AS 'reviewer'
238+
FROM series
239+
JOIN reviews ON series.id = reviews.series_id
240+
JOIN reviewers ON reviewers.id = reviews.reviewer_id
241+
ORDER BY title, rating DESC;

0 commit comments

Comments
 (0)