From 701ebfcbf29b0997b0be260491db5b0bc2802c6e Mon Sep 17 00:00:00 2001 From: hannaklh Date: Tue, 26 Aug 2025 22:54:27 +0200 Subject: [PATCH] exercise complete --- sql/products/core-queries.sql | 68 +++++++++++++++++++++++++++++ sql/products/create-products.sql | 15 ++++--- sql/products/extension1-queries.sql | 15 +++++++ sql/products/extension2-queries.sql | 48 ++++++++++++++++++++ sql/products/insert-products.sql | 21 +++++++-- 5 files changed, 156 insertions(+), 11 deletions(-) create mode 100644 sql/products/core-queries.sql create mode 100644 sql/products/extension1-queries.sql create mode 100644 sql/products/extension2-queries.sql diff --git a/sql/products/core-queries.sql b/sql/products/core-queries.sql new file mode 100644 index 0000000..fc55dbf --- /dev/null +++ b/sql/products/core-queries.sql @@ -0,0 +1,68 @@ +-- 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 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 + +--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 like 'SciFi' + +-- films with the genre of "Western" or "SciFi" +select * +from films +where genre like 'SciFi' or genre like 'Western' + +-- films with any genre apart from "SciFi" +select * +from films +where genre not like 'SciFi' + +-- films with the genre of "Western" released before 2000 +select * +from films +where genre like 'Western' and releaseYear < 2000 + +-- films that have the world "Matrix" in their title +select * +from films +where title like '%Matrix%' \ No newline at end of file diff --git a/sql/products/create-products.sql b/sql/products/create-products.sql index e7a5100..784532c 100644 --- a/sql/products/create-products.sql +++ b/sql/products/create-products.sql @@ -1,8 +1,9 @@ -CREATE TABLE products( - id serial primary key, - name varchar(255) not null, - price int not null, - discount boolean, - unique(name) -) +-- film table +CREATE TABLE IF NOT EXISTS films ( +id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY, +title VARCHAR(128) NOT NULL UNIQUE, +genre VARCHAR(64) NOT NULL, +releaseYear integer NOT NULL, +score integer NOT NULL +); diff --git a/sql/products/extension1-queries.sql b/sql/products/extension1-queries.sql new file mode 100644 index 0000000..03949cd --- /dev/null +++ b/sql/products/extension1-queries.sql @@ -0,0 +1,15 @@ +-- Return the average film rating +select AVG(score) +from films +as average_score + +-- Return the total number of films +select Count(*) as total_films +from films + +-- Return the average film rating by genre +select genre, AVG(score) +from films +as average_score +GROUP BY genre + diff --git a/sql/products/extension2-queries.sql b/sql/products/extension2-queries.sql new file mode 100644 index 0000000..a3c0628 --- /dev/null +++ b/sql/products/extension2-queries.sql @@ -0,0 +1,48 @@ + +-- director table +CREATE TABLE IF NOT EXISTS directors ( +id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY, +name VARCHAR(64) NULL UNIQUE, +); + +-- film table +CREATE TABLE IF NOT EXISTS films ( +id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY, +directorsId integer NOT NULL, +title VARCHAR(128) NOT NULL UNIQUE, +genre VARCHAR(64) NOT NULL, +release_year integer NOT NULL, +rating integer NOT NULL, +CONSTRAINT fk_director FOREIGN KEY (directorsId) REFERENCES directors(id) +); + + +-- Insert sample data into users table +INSERT INTO directors (name) +VALUES + ('Steven Spielberg'), + ('James Cameron'), + ('Michael Scorcese'); + +-- Insert sample data into users table +INSERT INTO films (directorsId,title, genre, release_year, rating) +VALUES + (1,'The Shawshank Redemption', 'Drama', 1994, 9), + (2,'The Godfather', 'Crime', 1972, 9), + (3,'The Dark Knight', 'Action', 2008, 9), + (3,'Alien', 'SciFi', 1979, 9), + (2,'Total Recall', 'SciFi', 1990, 8), + (3,'The Matrix', 'SciFi', 1999, 8), + (1,'The Matrix Resurrections', 'SciFi', 2021, 5), + (1,'The Matrix Reloaded', 'SciFi', 2003, 6), + (1,'The Hunt for Red October', 'Thriller', 1990, 7), + (2,'Misery', 'Thriller', 1990, 7), + (3,'The Power Of The Dog', 'Western', 2021, 6), + (2,'Hell or High Water', 'Western', 2016, 8), + (2,'The Good the Bad and the Ugly', 'Western', 1966, 9), + (1,'Unforgiven', 'Western', 1992, 7); + + select f.title as movie_title, f.directorsId as directors_id, d.name as directors_name + from films f + join directors d + on f.directorsId = d.id diff --git a/sql/products/insert-products.sql b/sql/products/insert-products.sql index 91cb667..8650c83 100644 --- a/sql/products/insert-products.sql +++ b/sql/products/insert-products.sql @@ -1,4 +1,17 @@ -insert into products(name, price, discount) VALUES ('apple', 135, false); -insert into products(name, price, discount) VALUES ('orange', 55, true); -insert into products(name, price, discount) VALUES ('kiwi', 165, true); -insert into products(name, price, discount) VALUES ('banana', 035, false); \ No newline at end of file +-- Insert sample data into users table +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);