diff --git a/sql/products/core.sql b/sql/products/core.sql new file mode 100644 index 0000000..05a7a46 --- /dev/null +++ b/sql/products/core.sql @@ -0,0 +1,60 @@ +CREATE TABLE films( + id serial PRIMARY KEY, + title VARCHAR(200) NOT NULL, + genre VARCHAR(200) NOT NULL, + release_year int NOT NULL, + score int NOT NULL, + directorid serial, + UNIQUE(title) + ) + +INSERT INTO films ( title, genre, release_year, score) VALUES ('The Shawshank Redemption', 'Drama', 1994, 9); +INSERT INTO films ( title, genre, release_year, score) VALUES ('The Godfather', 'Crime', 1972, 9); +INSERT INTO films ( title, genre, release_year, score) VALUES ('The Dark Knight', 'Action', 2008, 9); +INSERT INTO films ( title, genre, release_year, score) VALUES ('Alien', 'SciFi', 1979, 9); +INSERT INTO films ( title, genre, release_year, score) VALUES ('Total Recall', 'SciFi', 1990, 8); +INSERT INTO films ( title, genre, release_year, score) VALUES ('The Matrix', 'SciFi', 1999, 8); +INSERT INTO films ( title, genre, release_year, score) VALUES ('The Matrix Resurrections', 'SciFi', 2021, 5); +INSERT INTO films ( title, genre, release_year, score) VALUES ('The Matrix Reloaded', 'SciFi', 2003, 6); +INSERT INTO films ( title, genre, release_year, score) VALUES ('The Hunt for Red October', 'Thriller', 1990, 7); +INSERT INTO films ( title, genre, release_year, score) VALUES ('Misery', 'Thriller', 1990, 7); +INSERT INTO films ( title, genre, release_year, score) VALUES ('The Power Of The Dog', 'Western', 2021, 6); +INSERT INTO films ( title, genre, release_year, score) VALUES ('Hell or High Water', 'Western', 2016, 8); +INSERT INTO films ( title, genre, release_year, score) VALUES ('The Good the Bad and the Ugly', 'Western', 1966, 9); +INSERT INTO films ( title, genre, release_year, score) VALUES ('Unforgiven', 'Western', 1992, 7); + + +SELECT * FROM films + +SELECT * FROM films AS f ORDER BY f.score DESC + +SELECT * FROM films AS f ORDER BY f.release_year ASC + +SELECT * FROM films AS f WHERE f.score >=8 + +SELECT * FROM films AS f WHERE f.score <=7 + +SELECT * FROM films AS f WHERE f.release_year = 1990 + +SELECT * FROM films AS f WHERE f.release_year < 2000 + +SELECT * FROM films AS f WHERE f.release_year > 1990 + +SELECT * FROM films AS f WHERE f.release_year > 1990 AND f.release_year < 2000 + +SELECT * FROM films AS f WHERE f.genre = 'SciFi' + +SELECT * FROM films AS f WHERE f.genre = 'SciFi' OR f.genre = 'Western' + +SELECT * FROM films AS f WHERE f.genre != 'SciFi' + +SELECT * FROM films AS f WHERE f.genre = 'Western' AND f.release_year < 2000 + +SELECT * FROM films AS f WHERE f.title LIKE '%Matrix%' + + +SELECT AVG(films.score) FROM films + +SELECT COUNT(films.title) FROM films + +SELECT films.genre, AVG(films.score) AS average_score FROM films GROUP BY films.genre \ No newline at end of file diff --git a/sql/products/extension.sql b/sql/products/extension.sql new file mode 100644 index 0000000..443db13 --- /dev/null +++ b/sql/products/extension.sql @@ -0,0 +1,23 @@ +SELECT AVG(films.score) FROM films + +SELECT COUNT(films.title) FROM films + +SELECT films.genre, AVG(films.score) AS average_score FROM films GROUP BY films.genre +CREATE TABLE directors ( + id serial PRIMARY KEY, + name VARCHAR(200) NOT NULL, + film_count int NOT NULL +); + +INSERT INTO directors (name,film_count) VALUES ('Christopher Nolan',10); +INSERT INTO directors (name,film_count) VALUES ('Steven Spielberg',3); +INSERT INTO directors (name,film_count) VALUES ('Quentin Tarantino',8); +INSERT INTO directors (name,film_count) VALUES ('Martin Scorsese',1); +INSERT INTO directors (name,film_count) VALUES ('Ridley Scott',45); + +SELECT films.title, directors.name +FROM films +INNER JOIN directors ON films.directorid = directors.directorid; + +SELECT directors.name, directors.film_count +FROM directors \ No newline at end of file