Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added sql/.DS_Store
Binary file not shown.
100 changes: 100 additions & 0 deletions sql/products/core.sql
Original file line number Diff line number Diff line change
@@ -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%'


80 changes: 80 additions & 0 deletions sql/products/extensions.sql
Original file line number Diff line number Diff line change
@@ -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