diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7dbc2e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +devbox/ diff --git a/sql/directors-Film-Amount.sql b/sql/directors-Film-Amount.sql new file mode 100644 index 0000000..962d7ef --- /dev/null +++ b/sql/directors-Film-Amount.sql @@ -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; \ No newline at end of file diff --git a/sql/directors/create-directors.sql b/sql/directors/create-directors.sql new file mode 100644 index 0000000..e0432f2 --- /dev/null +++ b/sql/directors/create-directors.sql @@ -0,0 +1,6 @@ +/* EXTENSION 2 */ + +CREATE TABLE directors ( + directorId serial primary key, + name varchar(255) not null +); \ No newline at end of file diff --git a/sql/directors/insert-directors.sql b/sql/directors/insert-directors.sql new file mode 100644 index 0000000..2788ca0 --- /dev/null +++ b/sql/directors/insert-directors.sql @@ -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'); \ No newline at end of file diff --git a/sql/films/create-films.sql b/sql/films/create-films.sql new file mode 100644 index 0000000..a0f92d2 --- /dev/null +++ b/sql/films/create-films.sql @@ -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) +); \ No newline at end of file diff --git a/sql/films/insert-films.sql b/sql/films/insert-films.sql new file mode 100644 index 0000000..a1d57f1 --- /dev/null +++ b/sql/films/insert-films.sql @@ -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); \ No newline at end of file diff --git a/sql/films/select-films.sql b/sql/films/select-films.sql new file mode 100644 index 0000000..2153b73 --- /dev/null +++ b/sql/films/select-films.sql @@ -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; \ No newline at end of file diff --git a/sql/select-films-with-directors.sql b/sql/select-films-with-directors.sql new file mode 100644 index 0000000..2006f70 --- /dev/null +++ b/sql/select-films-with-directors.sql @@ -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; \ No newline at end of file