From 20834a19271ac8355655cbedc137f74f5370014d Mon Sep 17 00:00:00 2001 From: Andre Velkov Janusev Date: Tue, 26 Aug 2025 12:24:44 +0200 Subject: [PATCH 1/2] Core + Extension 1 complete. --- sql/products/create-products.sql | 3 +- sql/products/exercise-sql-intro.sql | 85 +++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 sql/products/exercise-sql-intro.sql diff --git a/sql/products/create-products.sql b/sql/products/create-products.sql index e7a5100..6abfe17 100644 --- a/sql/products/create-products.sql +++ b/sql/products/create-products.sql @@ -4,5 +4,4 @@ CREATE TABLE products( price int not null, discount boolean, unique(name) -) - +) \ No newline at end of file diff --git a/sql/products/exercise-sql-intro.sql b/sql/products/exercise-sql-intro.sql new file mode 100644 index 0000000..7365f9f --- /dev/null +++ b/sql/products/exercise-sql-intro.sql @@ -0,0 +1,85 @@ +-- +--Exercise Core +-- + +--CREATE +CREATE TABLE films( +id integer primary key AUTOINCREMENT, +title varchar(255) not null, +genre varchar(255) not null, +releaseYear integer not null, +rating integer, +) + +--INSERT +INSERT INTO films (title, genre, releaseYear, rating) +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); + +--SELECT STATEMENTS +--All films +SELECT * from films; + +--* All films ordered by rating descending +SELECT * from films ORDER BY rating 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 rating >= 8; + +--* All films with a rating of 7 or lower +SELECT * from films WHERE rating <= 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 NOT genre = "SciFi"; + +--* films with the genre of "Western" released before 2000 +SELECT * from films WHERE genre = "Western" AND releaseYear < 2000; + +--* films that have the world "Matrix" in their title +SELECT * from films WHERE title LIKE '%Matrix%'; + +-- +-- Exercise Extensions +-- + +-- Extension 1 + +--* Return the average film rating +select AVG(rating) from films; +--* Return the total number of films +select count(*) from films; +--* Return the average film rating by genre +select avg(rating), genre from films group by genre; \ No newline at end of file From d93a4a7c94b2c9ead128206306158ae478497d1e Mon Sep 17 00:00:00 2001 From: Andre Velkov Janusev Date: Tue, 26 Aug 2025 13:18:29 +0200 Subject: [PATCH 2/2] Extension 2 + 3 complete. --- .idea/.gitignore | 3 ++ .idea/api-sql-intro.iml | 9 ++++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ sql/products/exercise-sql-intro.sql | 48 ++++++++++++++++++++++++++--- 6 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/api-sql-intro.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/api-sql-intro.iml b/.idea/api-sql-intro.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/api-sql-intro.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..29434d4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3f39777 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sql/products/exercise-sql-intro.sql b/sql/products/exercise-sql-intro.sql index 7365f9f..0494247 100644 --- a/sql/products/exercise-sql-intro.sql +++ b/sql/products/exercise-sql-intro.sql @@ -4,11 +4,12 @@ --CREATE CREATE TABLE films( -id integer primary key AUTOINCREMENT, +id serial primary key, title varchar(255) not null, genre varchar(255) not null, releaseYear integer not null, rating integer, +director integer ) --INSERT @@ -75,11 +76,48 @@ SELECT * from films WHERE title LIKE '%Matrix%'; -- Exercise Extensions -- +-- -- Extension 1 - --* Return the average film rating -select AVG(rating) from films; +SELECT AVG(rating) from films; --* Return the total number of films -select count(*) from films; +SELECT count(*) from films; --* Return the average film rating by genre -select avg(rating), genre from films group by genre; \ No newline at end of file +SELECT avg(rating), genre from films group by genre; + +-- +-- Extension 2 +CREATE TABLE directors( +id integer primary key autoincrement, +name varchar(255), +) + +insert into directors (name) +values ('Steven Spielberg'), ('Christopher Nolan'), ('Quentin Tarantino'); + +CREATE TABLE films( +id SERIAL PRIMARY KEY , +title varchar(255) not null, +genre varchar(255) not null, +releaseYear integer not null, +rating integer, +director integer, +foreign key (director) references directors(id) +) + +--INSERT +INSERT INTO films (title, genre, releaseYear, rating, director) +VALUES ('The Shawshank Redemption', 'Drama', 1994, 9, 1), + ('The Godfather', 'Crime', 1972, 9, 2), + ('The Dark Knight', 'Action', 2008, 9, 2), + ('Alien', 'SciFi', 1979, 9, 3); + +--* Using and SQL JOIN, write a SELECT statement that returns a list of films with their director +SELECT * from films join directors on directors.id = films.director; + +-- +-- Extension 3 +SELECT count(*), directors.name +from films2 +join directors on directors.id = films2.director +group by directors.name; \ No newline at end of file