diff --git a/sql/films/create-films.sql b/sql/films/create-films.sql new file mode 100644 index 0000000..bd6243a --- /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, + releaseYear int not null, + score int, + unique(title) +) diff --git a/sql/films/ext1.sql b/sql/films/ext1.sql new file mode 100644 index 0000000..f75798a --- /dev/null +++ b/sql/films/ext1.sql @@ -0,0 +1,10 @@ +/* Return the average film rating */ +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 +order by avg desc; diff --git a/sql/films/ext2.sql b/sql/films/ext2.sql new file mode 100644 index 0000000..4473de3 --- /dev/null +++ b/sql/films/ext2.sql @@ -0,0 +1,43 @@ +CREATE TABLE directors( + directorId int, + name varchar(255), + unique(directorId) +); + +CREATE TABLE films( + id serial primary key, + title varchar(255) not null, + genre varchar(255) not null, + releaseYear int not null, + score int, + directorId int, + CONSTRAINT fk_directorId + FOREIGN KEY(directorId) REFERENCES directors(directorId), + unique(title) +); + +INSERT INTO directors (directorId, name) VALUES + (1, 'Quentin Tarantino'), + (2, 'Christopher Nolan'), + (3, 'Martin Scorsese'), + (4, 'Stanley Kubrick'); + +INSERT INTO films (title, genre, releaseYear, score, directorId) VALUES +('The Shawshank Redemption', 'Drama', 1994, 9, 1), +('The Godfather', 'Crime', 1972, 9, 3), +('The Dark Knight', 'Action', 2008, 9, 2), +('Alien', 'SciFi', 1979, 9, 1), +('Total Recall', 'SciFi', 1990, 8, 2), +('The Matrix', 'SciFi', 1999, 8, 4), +('The Matrix Resurrections', 'SciFi', 2021, 5, 4), +('The Matrix Reloaded', 'SciFi', 2003, 6, 4), +('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 Water', 'Western', 2016, 8, 1), +('The Good the Bad and the Ugly', 'Western', 1966, 9, 4), +('Unforgiven', 'Western', 1992, 7, 4); + +/* Return all movies with director names */ +select title, directors.name from films +inner join directors on films.directorId = directors.directorId; diff --git a/sql/films/ext3.sql b/sql/films/ext3.sql new file mode 100644 index 0000000..0e30c02 --- /dev/null +++ b/sql/films/ext3.sql @@ -0,0 +1,3 @@ +select directors.name, count(*) from films +inner join directors on films.directorId = directors.directorId +group by directors.name; diff --git a/sql/films/insert-films.sql b/sql/films/insert-films.sql new file mode 100644 index 0000000..9148b71 --- /dev/null +++ b/sql/films/insert-films.sql @@ -0,0 +1,15 @@ +INSERT INTO films (title, genre, releaseYear, 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 Water', 'Western', 2016, 8), +('The Good the Bad and the Ugly', 'Western', 1966, 9), +('Unforgiven', 'Western', 1992, 7); diff --git a/sql/films/select-films.sql b/sql/films/select-films.sql new file mode 100644 index 0000000..bb9b995 --- /dev/null +++ b/sql/films/select-films.sql @@ -0,0 +1,54 @@ +/* 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 releaseYear; + +/* 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; + +/* films released in 1990 */ +select * from films +where releaseYear = 1990; + +/* films released before 2000 */ +select * from films +where releaseYear < 2000; + +/* films released after 1990 */ +select * from films +where releaseYear > 1990; + +/* films released between 1990 and 1999 */ +select * from films +where releaseYear > 1990 and releaseYear < 1999; + +/* films with the genre of "SciFi" */ +select * from films +where genre = 'SciFi'; + +/* films with the genre of "Western" or "SciFi" */ +select * from films +where genre = 'SciFi' or genre = 'Western'; + +/* films with any genre apart from "SciFi" */ +select * from films +where genre != 'SciFi'; + +/* films with the genre of "Western" released before 2000 */ +select * from films +where genre = 'Western' and releaseYear < 2000; + +/* films that have the word "Matrix" in their title */ +select * from films +where title like '%Matrix%';