Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
devbox/
5 changes: 5 additions & 0 deletions sql/directors-Film-Amount.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT directors.name AS director, COUNT(films.id) AS film_count
FROM directors
LEFT JOIN films ON directors.directorId = films.directorId
GROUP BY directors.directorId, directors.name
ORDER BY film_count DESC;
6 changes: 6 additions & 0 deletions sql/directors/create-directors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* EXTENSION 2 */

CREATE TABLE directors (
directorId serial primary key,
name varchar(255) not null
);
12 changes: 12 additions & 0 deletions sql/directors/insert-directors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INSERT INTO directors (name) VALUES ('Frank Darabont');
INSERT INTO directors (name) VALUES ('Francis Ford Coppola');
INSERT INTO directors (name) VALUES ('Christopher Nolan');
INSERT INTO directors (name) VALUES ('Ridley Scott');
INSERT INTO directors (name) VALUES ('Paul Verhoeven');
INSERT INTO directors (name) VALUES ('Lana Wachowski');
INSERT INTO directors (name) VALUES ('John McTiernan');
INSERT INTO directors (name) VALUES ('Rob Reiner');
INSERT INTO directors (name) VALUES ('Jane Campion');
INSERT INTO directors (name) VALUES ('David Mackenzie');
INSERT INTO directors (name) VALUES ('Sergio Leone');
INSERT INTO directors (name) VALUES ('Clint Eastwood');
8 changes: 8 additions & 0 deletions sql/films/create-films.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE films (
id serial primary key,
title varchar(255) not null,
genre varchar(255) not null,
release_year int not null,
score int not null,
directorId int references directors(directorId)
);
14 changes: 14 additions & 0 deletions sql/films/insert-films.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('The Shawshank Redemption', 'Drama', 1994, 9.3, 1);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('The Godfather', 'Crime', 1972, 9, 2);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('The Dark Knight', 'Action', 2008, 9, 3);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('alien', 'Sci-Fi', 1979, 9, 4);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('Total Recall', 'Sci-Fi', 1990, 8, 5);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('The Matrix', 'Sci-Fi', 1999, 8, 6);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('The Matrix Resurrections', 'Sci-Fi', 2021, 5, 6);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('The Matrix Reloaded', 'Sci-Fi', 2003, 6, 6);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('The Hunt for Red October', 'Thriller', 1990, 7, 7);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('Misery', 'Thriller', 1990, 7, 8);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('The Power Of The Dog', 'Western', 2021, 6, 9);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('Hell or High Water', 'Western', 2016, 8, 10);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('The Good the Bad and the Ugly', 'Western', 1966, 9, 11);
INSERT INTO films (title, genre, release_year, score, directorId) VALUES ('Unforgiven', 'Western', 1992, 7, 12);
49 changes: 49 additions & 0 deletions sql/films/select-films.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* select all films */
SELECT * FROM films;

/* All films ordered by rating descending */
SELECT * FROM films ORDER BY score DESC;

/* All films ordered by release year ascending */
SELECT * FROM films ORDER BY release_year ASC;

/* All films with a rating of 8 or higher */
SELECT * FROM films WHERE score >= 8;

/* All films with a rating of 7 or lower */
SELECT * FROM films WHERE score <= 7;

/* All films released in 1990 */
SELECT * FROM films WHERE release_year = 1990;

/*films released after 1990*/
SELECT * FROM films WHERE release_year > 1990;

/* films released between 1990 and 2000 */
SELECT * FROM films WHERE release_year BETWEEN 1990 AND 2000;

/* films with genre of "SciFi" */
SELECT * FROM films WHERE genre = "Sci-Fi";

/* films with the genre of "western" or "Sci-Fi" */
SELECT * FROM films WHERE genre = "Western" OR genre = "Sci-Fi";

/* films with any genre apart from "Sci-Fi" */
SELECT * FROM films WHERE genre != "Sci-Fi";

/* films with the genre of "western" released before 2000 */
SELECT * FROM films WHERE genre = "Western" AND release_year < 2000;

/* films that have the world "matrix" in their title */
SELECT * FROM films WHERE title LIKE "%matrix%";

/* ------ EXTENSION 1 ------ */

/* retyrn the average score of all films */
SELECT AVG(score) FROM films;

/* return the total number of films */
SELECT COUNT(*) FROM films;

/* return the average film rating by genre */
SELECT genre, AVG(score) FROM films GROUP BY genre;
3 changes: 3 additions & 0 deletions sql/select-films-with-directors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT films.title, films.genre, films.release_year, films.score, directors.name AS director
FROM films
JOIN directors ON films.directorId = directors.directorId;