-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmany-to-many.sql
More file actions
executable file
·150 lines (117 loc) · 3.47 KB
/
many-to-many.sql
File metadata and controls
executable file
·150 lines (117 loc) · 3.47 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
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
/*
A database for a series reviewing website.
*/
-----------------------------------------------------------------------
-- CREATE TABLE reviewers (
-- id INT AUTO_INCREMENT PRIMARY KEY,
-- first_name VARCHAR(100),
-- last_name VARCHAR(100)
-- );
-- CREATE TABLE series(
-- id INT AUTO_INCREMENT PRIMARY KEY,
-- title VARCHAR(100),
-- released_year YEAR(4),
-- genre VARCHAR(100)
-- );
-- CREATE TABLE reviews(
-- id INT AUTO_INCREMENT PRIMARY KEY,
-- rating DECIMAL (2,1),
-- series_id INT,
-- reviewer_id INT,
-- FOREIGN KEY (series_id) REFERENCES series(id),
-- FOREIGN KEY (reviewer_id) REFERENCES reviewers(id)
-- );
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
1. Print for each serie all the existent ratings.
*/
SELECT title, rating FROM series
JOIN reviews
ON series.id = reviews.series_id;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
2. Print in ascending order the title and average rating for each series.
*/
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;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
3. Print for each existent review, the the rating and the first and
last name of the reviewer.
*/
SELECT
first_name,
last_name,
rating
FROM reviewers
JOIN reviews
ON reviewers.id = reviews.reviewer_id;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
4. Identify and print the unreviewed series.
*/
SELECT
title
FROM series
LEFT JOIN reviews
ON reviews.series_id = series.id
WHERE rating IS NULL;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
5. For each existent genre, print the average rating
*/
SELECT genre,
CAST(AVG (rating) AS DECIMAL(2,1))
FROM series
JOIN reviews
ON reviews.series_id = series.id
GROUP BY series.genre;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
6. For each existent user, print the first name, the last name
the number of reviews, the minimum and maximum and the average rating
given by him and his status (status = "ACTIVE" if he has provided
one or more reviews, "INACTIVE" if he has provided no reviews )
*/
SELECT
first_name,
last_name,
IFNULL(COUNT(rating),0) AS "COUNT",
IFNULL(MIN(rating),0.0) AS "MIN",
IFNULL(MAX(rating),0.0) AS "MAX",
IFNULL(AVG(rating),0.00000) AS "AVG",
CASE
WHEN COUNT(rating)=0 THEN "INACTIVE"
ELSE "ACTIVE"
END AS "STATUS"
FROM reviewers
LEFT JOIN reviews
ON reviews.reviewer_id = reviewers.id
GROUP BY reviewers.id;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
7. For each review given, print the title of the reviewed series,
the rating and the name of the reviewer. (double join)
*/
SELECT
title,
rating,
CONCAT(first_name," ",last_name) AS "reviewer"
FROM reviewers
INNER JOIN reviews
ON reviewers.id = reviews.reviewer_id
INNER JOIN series
ON series.id = reviews.series_id
ORDER BY title;