diff --git a/sql/films/create-films.sql b/sql/films/create-films.sql new file mode 100644 index 0000000..0946058 --- /dev/null +++ b/sql/films/create-films.sql @@ -0,0 +1,7 @@ +CREATE TABLE films( + id serial primary key, + title varchar(1000) unique, + genre varchar(1000), + released int, + score int + ) diff --git a/sql/films/extentions.sql b/sql/films/extentions.sql new file mode 100644 index 0000000..94b3819 --- /dev/null +++ b/sql/films/extentions.sql @@ -0,0 +1,43 @@ +-- Extention 2 + +CREATE TABLE directors( + id serial primary key, + name varchar(70) +) + +CREATE TABLE films_2( + id serial primary key, + title varchar(1000) unique, + genre varchar(1000), + released int, + score int, + directorID int, + FOREIGN KEY(directorID) REFERENCES directors(id) +) + + +INSERT INTO directors (name) VALUES +('Bob'), +('John'), +('Trauma'); + + + +insert into films_2(title, genre, released, score) VALUES ('The Shawshank Redemption', 'Drama', 1994, 9, 1); +insert into films_2(title, genre, released, score) VALUES ('The Godfather', 'Crime', 1972, 9, 1); +insert into films_2(title, genre, released, score) VALUES ('The Dark Knight', 'Action', 2008, 9, 1); +insert into films_2(title, genre, released, score) VALUES ('Alien', 'SciFi', 1979, 9, 1); +insert into films_2(title, genre, released, score) VALUES ('Total Recall', 'SciFi', 1990, 8, 1); +insert into films_2(title, genre, released, score) VALUES ('The Matrix', 'SciFi', 1999, 8, 1); +insert into films_2(title, genre, released, score) VALUES ('The Matrix Resurrections', 'SciFi', 2021, 5, 1); +insert into films_2(title, genre, released, score) VALUES ('The Matrix Reloaded', 'SciFi', 2003, 6, 1); +insert into films_2(title, genre, released, score) VALUES ('The Hunt for Red October', 'Thriller', 1990, 7, 1); +insert into films_2(title, genre, released, score) VALUES ('Misery', 'Thriller', 1990, 7, 1); +insert into films_2(title, genre, released, score) VALUES ('The Power of the Dog', 'Western', 2021, 6, 1); +insert into films_2(title, genre, released, score) VALUES ('Hell or High Water', 'Western', 2016, 8, 1); +insert into films_2(title, genre, released, score) VALUES ('The Good the Bad and the Ugly', 'Western', 1966, 9, 1); +insert into films_2(title, genre, released, score) VALUES ('Unforgiven', 'Western', 1992, 7, 1); + + +SELECT * FROM films_2 INNER JOIN directors ON films_2.directorID=directors.id; +SELECT directors.name, COUNT(films_2.title) from directors inner join films_2 on directors.id=films_dk.director_id GROUP BY directors.name; diff --git a/sql/films/insert-films.sql b/sql/films/insert-films.sql new file mode 100644 index 0000000..63d6059 --- /dev/null +++ b/sql/films/insert-films.sql @@ -0,0 +1,15 @@ +insert into films(title, genre, released, score) VALUES ('The Shawshank Redemption', 'Drama', 1994, 9); +insert into films(title, genre, released, score) VALUES ('The Godfather', 'Crime', 1972, 9); +insert into films(title, genre, released, score) VALUES ('The Dark Knight', 'Action', 2008, 9); +insert into films(title, genre, released, score) VALUES ('Alien', 'SciFi', 1979, 9); +insert into films(title, genre, released, score) VALUES ('Total Recall', 'SciFi', 1990, 8); +insert into films(title, genre, released, score) VALUES ('The Matrix', 'SciFi', 1999, 8); +insert into films(title, genre, released, score) VALUES ('The Matrix Resurrections', 'SciFi', 2021, 5); +insert into films(title, genre, released, score) VALUES ('The Matrix Reloaded', 'SciFi', 2003, 6); +insert into films(title, genre, released, score) VALUES ('The Hunt for Red October', 'Thriller', 1990, 7); +insert into films(title, genre, released, score) VALUES ('Misery', 'Thriller', 1990, 7); +insert into films(title, genre, released, score) VALUES ('The Power of the Dog', 'Western', 2021, 6); +insert into films(title, genre, released, score) VALUES ('Hell or High Water', 'Western', 2016, 8); +insert into films(title, genre, released, score) VALUES ('The Good the Bad and the Ugly', 'Western', 1966, 9); +insert into films(title, genre, released, score) VALUES ('Unforgiven', 'Western', 1992, 7); + diff --git a/sql/films/select-films.sql b/sql/films/select-films.sql new file mode 100644 index 0000000..8f2f7d7 --- /dev/null +++ b/sql/films/select-films.sql @@ -0,0 +1,20 @@ +SELECT * FROM films; +SELECT * FROM films ORDER BY score DESC; +SELECT * FROM films ORDER BY released ASC; +SELECT * FROM films WHERE score > 8; +SELECT * FROM films WHERE score < 7; +SELECT * FROM films WHERE released = 1990; +SELECT * FROM films WHERE released < 2000; +SELECT * FROM films WHERE released > 1990; +SELECT * FROM films WHERE released > 1990 AND released < 2000; +SELECT * FROM films WHERE genre = 'SciFi'; +SELECT * FROM films WHERE genre = 'SciFi' OR genre = 'Western'; +SELECT * FROM films WHERE NOT genre = 'SciFi'; +SELECT * FROM films WHERE genre = 'Western' AND released < 2000; +SELECT * FROM films WHERE title LIKE '%Matrix%'; + +-- Extention 1 + +SELECT AVG(score) FROM films; +SELECT COUNT(*) FROM films; +SELECT genre, AVG(score) FROM films GROUP BY genre;