From 182d18ddf582c945c920492ca4fdf30cdd9135de Mon Sep 17 00:00:00 2001 From: David Stern Date: Tue, 26 Aug 2025 15:59:19 +0200 Subject: [PATCH] David Stern --- .DS_Store | Bin 0 -> 6148 bytes sql/.DS_Store | Bin 0 -> 6148 bytes sql/products/core.sql | 100 ++++++++++++++++++++++++++++++++++++ sql/products/extensions.sql | 80 +++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 .DS_Store create mode 100644 sql/.DS_Store create mode 100644 sql/products/core.sql create mode 100644 sql/products/extensions.sql diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d1e0ed60e3d690ef0b5835e66339c1f5b14650ec GIT binary patch literal 6148 zcmeHKOH0E*5T5PBCKRCugykJ_F+2WuOeV;6pt3{^~ea{*WRMVnQ2QP=_X*AeP|)yt#a`NULE%t9hr*JSES-1suZ+aqJwB_ TOcbI8g?S5T0$TZV{meg&r5Y7Oh%E@e->Zyc*GiN^MNhV9b^#wTDv3SzpK}@pW`& zcPmQus92eS*>5sGN%q^Yn*jjP?1yUr6#$T^goQE=UkJrXC#2v!MMPol(T8iuAU%rp z^+Yy1{vrdkb`rYK0}Ua3Sid0F0mkUV*h5`;HGwu{1L~ zC+Fn6y!3DNz|Y!QKW(;q=X7)U`V%W|ty9S%1(8#T4o*dEo?aA&<LG2CG;HT z7R}aygJ}T}OXxO&HvKYEjdJKY%q?OBMc7hATPj?OA#6GNmCN%S<`!)^2-kcF_hjKZ z6k(o@_g5wzglCaQW`G&^%mDR%C>1*Y55MpKO%e~x05kAE84#rd|DcI;b7$+sEa|LO ts8^^Yl$Tq4FG0f`#h6P+aTV1F`V|?7p2OTCdQkXBz|g=0Gw`bnyaA0ZT0#H- literal 0 HcmV?d00001 diff --git a/sql/products/core.sql b/sql/products/core.sql new file mode 100644 index 0000000..3613013 --- /dev/null +++ b/sql/products/core.sql @@ -0,0 +1,100 @@ + +create table if not exists films ( +id integer generated always as identity primary key, +title VARCHAR(64) not null unique, +genre VARCHAR(32) not null, +release_year integer not null, +score integer not null, +constraint score_of_ten check (score > 0 and score < 11) +); + +-- Insert +insert into films (title, genre, release_year, 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); + + +-- Select +-- Select 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 release_year asc + +-- All films with a rating of 8 or higher +select * +from films +where score > 7 + +-- All films with a rating of 7 or lower +select * +from films +where score < 8 + +-- films released in 1990 +select * +from films +where release_year = 1990 + +-- films released before 2000 +select * +from films +where release_year < 2000 + +-- films released after 1990 +select * +from films +where release_year > 1990 + +-- films released between 1990 and 1999 +select * +from films +where (release_year < 2000 and release_year > 1979) + +-- 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 release_year < 2000) + + +-- films that have the word "Matrix" in their title +select * +from films +where title like '%Matrix%' + + diff --git a/sql/products/extensions.sql b/sql/products/extensions.sql new file mode 100644 index 0000000..79fdb84 --- /dev/null +++ b/sql/products/extensions.sql @@ -0,0 +1,80 @@ +-- Extension 1 +-- Select average score +select avg(score) as average_score +from films + +-- Select total number of films +select count(title) +from films + +-- Select average score grouped by genre +select genre, avg(score) as average_score +from films +group by genre + + +-- Extension 2 +-- Create directors table +create table if not exists directors ( +directorId integer generated always as identity primary key, +name varchar(50) not null unique +) + +-- Create new version of films that include reference to director +create table if not exists films_extension ( + id integer generated always as identity primary key, + title varchar(100) not null unique, + genre varchar(32) not null, + release_year integer not null, + score integer not null, + directorId integer not null, + constraint score_of_ten check (score > 0 and score < 11), + constraint fk_director foreign key (directorId) references directors(directorId) +) + +-- Insert into directors +insert into directors (name) values +('Frank Darabont'), +('Francis Ford Coppola'), +('Christopher Nolan'), +('Ridley Scott'), +('Paul Verhoeven'), +('Lana Wachowski'), +('John McTiernan'), +('Rob Reiner'), +('Jane Campion'), +('David Mackenzie'), +('Sergio Leone'), +('Clint Eastwood') + +-- insert into new films table +insert into films_extension (title, genre, release_year, score, directorId) values +('The Shawshank Redemption', 'Drama', 1994, 9, 1), +('The Godfather', 'Crime', 1972, 9, 2), +('The Dark Knight', 'Action', 2008, 9, 3), +('Alien', 'SciFi', 1979, 9, 4), +('Total Recall', 'SciFi', 1990, 8, 5), +('The Matrix', 'SciFi', 1999, 8, 6), +('The Matrix Resurrections', 'SciFi', 2021, 5, 6), +('The Matrix Reloaded', 'SciFi', 2003, 6, 6), +('The Hunt for Red October', 'Thriller', 1990, 7, 7), +('Misery', 'Thriller', 1990, 7, 8), +('The Power Of The Dog', 'Western', 2021, 6, 9), +('Hell or High Water', 'Western', 2016, 8, 10), +('The Good the Bad and the Ugly', 'Western', 1966, 9, 11), +('Unforgiven', 'Western', 1992, 7, 12); + + +-- Select films and their director +select f.title, f.genre, f.release_year, f.score, d.name as director +from films_extension f +join directors d on f.directorId = d.directorId; + + +-- Extension 3 +-- Select all directors and number of films they have directed +select count(f.title) as film_count, d.name as director +from directors d +join films_extension f on d.directorId = f.directorId +group by d.name +order by film_count desc