-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathqueries.sql
More file actions
155 lines (124 loc) · 3.66 KB
/
queries.sql
File metadata and controls
155 lines (124 loc) · 3.66 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
151
152
153
154
155
DORP TABLE FILMS;
CREATE TABLE IF NOT EXISTS films (
id SERIAL PRIMARY KEY,
title TEXT,
genre TEXT,
release_year INTEGER,
score INTEGER,
UNIQUE(title)
);
TRUNCATE TABLE films;
INSERT INTO films
(title, genre, release_year, score)
VALUES
('The Shawshank Redemption', 'Drama', 1994, 9),
('The Godfather', 'Crime', 1972, 9),
('The Dark Knight', 'Action', 2008, 9),
('Alien', 'SciFi', 1979, 9),
('Total Recall', 'SciFi', 1990, 8),
('The Matrix', 'SciFi', 1999, 8),
('The Matrix Resurrections', 'SciFi', 2021, 5),
('The Matrix Reloaded', 'SciFi', 2003, 6),
('The Hunt for Red October', 'Thriller', 1990, 7),
('Misery', 'Thriller', 1990, 7),
('The Power Of The Dog', 'Western', 2021, 6),
('Hell or High Wate', 'Western', 2016, 8),
('The Good the Bad and the Ugly', 'Western', 1966, 9),
('Unforgiven', 'Western', 1992, 7);
-- All films
SELECT * FROM films;
-- All films ordered by rating desc
SELECT * FROM films
ORDER BY score DESC;
-- All films ordered by year asc
SELECT * FROM films
ORDER BY release_year;
-- All films rating >= 8
SELECT * FROM films
WHERE score >= 8;
-- All films rating <= 7
SELECT * FROM films
WHERE SCORE <= 7;
-- All films 1990
SELECT * FROM films
WHERE release_year = 1990;
-- Films before 2000
SELECT * FROM films
WHERE release_year <= 2000;
-- Films after 1990
SELECT * FROM films
WHERE release_year >= 1990;
-- Films between 1990-2000
SELECT * FROM films
WHERE release_year BETWEEN 1990 AND 1999; -- inclusive on both ends
-- SciFi films
SELECT * FROM films
WHERE genre = 'SciFi';
-- Western or SciFi
SELECT * FROM films
WHERE genre = 'SciFi' OR genre = 'Western';
-- No SciFi
SELECT * FROM films
WHERE genre != 'SciFi';
-- Pre-2000 Westerns
SELECT * FROM films
WHERE genre = 'Western' AND release_year < 2000;
-- Films with Matrix in title
SELECT * FROM films
WHERE title LIKE '%Matrix%';
-- ######## EXTENSION 1 ########
-- Average film rating
SELECT AVG(score) as average_score FROM films;
-- Number of films
SELECT COUNT(*) AS number_of_films FROM films;
-- Average rating by genre
SELECT genre, AVG(score) as average_score FROM films
GROUP BY genre;
-- ######## EXTENSION 2 ########
CREATE TABLE IF NOT EXISTS directors (
id INTEGER PRIMARY KEY,
name TEXT
);
TRUNCATE TABLE directors;
INSERT INTO directors (id, name)
VALUES
(1, 'Mr. Director'),
(2, 'Director Jr.'),
(3, 'Director Directorson');
DROP TABLE films2;
CREATE TABLE IF NOT EXISTS films2 (
id SERIAL PRIMARY KEY,
title TEXT,
genre TEXT,
release_year INTEGER,
score INTEGER,
director_id INTEGER REFERENCES directors(id),
UNIQUE(title)
);
TRUNCATE TABLE films2;
INSERT INTO films2
(title, genre, release_year, score, director_id)
VALUES
('The Shawshank Redemption', 'Drama', 1994, 9, 1),
('The Godfather', 'Crime', 1972, 9, 1),
('The Dark Knight', 'Action', 2008, 9, 2),
('Alien', 'SciFi', 1979, 9, 3),
('Total Recall', 'SciFi', 1990, 8, 2),
('The Matrix', 'SciFi', 1999, 8, 2),
('The Matrix Resurrections', 'SciFi', 2021, 5, 2),
('The Matrix Reloaded', 'SciFi', 2003, 6, 1),
('The Hunt for Red October', 'Thriller', 1990, 7, 1),
('Misery', 'Thriller', 1990, 7, 1),
('The Power Of The Dog', 'Western', 2021, 6, 3),
('Hell or High Wate', 'Western', 2016, 8, 1),
('The Good the Bad and the Ugly', 'Western', 1966, 9, 2),
('Unforgiven', 'Western', 1992, 7, 3);
-- Films with director
SELECT title, name FROM films2
INNER JOIN directors
ON films2.director_id = directors.id;
-- EXTENSION 3: directors with number of films
SELECT name, COUNT(*) FROM directors
INNER JOIN films2
ON directors.id = films2.director_id
GROUP BY name;