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
8 changes: 8 additions & 0 deletions sql/films/create-films.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE films(
id serial primary key,
title varchar(255) not null,
genre varchar(255) not null,
releaseYear int not null,
score int,
unique(title)
)
10 changes: 10 additions & 0 deletions sql/films/ext1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Return the average film rating */
select avg(score) from films;

/* Return the total number of films */
select count(*) from films;

/* Return the average film rating by genre */
select genre, avg(score) from films
group by genre
order by avg desc;
43 changes: 43 additions & 0 deletions sql/films/ext2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE TABLE directors(
directorId int,
name varchar(255),
unique(directorId)
);

CREATE TABLE films(
id serial primary key,
title varchar(255) not null,
genre varchar(255) not null,
releaseYear int not null,
score int,
directorId int,
CONSTRAINT fk_directorId
FOREIGN KEY(directorId) REFERENCES directors(directorId),
unique(title)
);

INSERT INTO directors (directorId, name) VALUES
(1, 'Quentin Tarantino'),
(2, 'Christopher Nolan'),
(3, 'Martin Scorsese'),
(4, 'Stanley Kubrick');

INSERT INTO films (title, genre, releaseYear, score, directorId) VALUES
('The Shawshank Redemption', 'Drama', 1994, 9, 1),
('The Godfather', 'Crime', 1972, 9, 3),
('The Dark Knight', 'Action', 2008, 9, 2),
('Alien', 'SciFi', 1979, 9, 1),
('Total Recall', 'SciFi', 1990, 8, 2),
('The Matrix', 'SciFi', 1999, 8, 4),
('The Matrix Resurrections', 'SciFi', 2021, 5, 4),
('The Matrix Reloaded', 'SciFi', 2003, 6, 4),
('The Hunt for Red October', 'Thriller', 1990, 7, 1),
('Misery', 'Thriller', 1990, 7, 1),
('The Power Of The Dog', 'Western', 2021, 6, 3),
('Hell or High Water', 'Western', 2016, 8, 1),
('The Good the Bad and the Ugly', 'Western', 1966, 9, 4),
('Unforgiven', 'Western', 1992, 7, 4);

/* Return all movies with director names */
select title, directors.name from films
inner join directors on films.directorId = directors.directorId;
3 changes: 3 additions & 0 deletions sql/films/ext3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select directors.name, count(*) from films
inner join directors on films.directorId = directors.directorId
group by directors.name;
15 changes: 15 additions & 0 deletions sql/films/insert-films.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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);
54 changes: 54 additions & 0 deletions sql/films/select-films.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* 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;

/* 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 = '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 releaseYear < 2000;

/* films that have the word "Matrix" in their title */
select * from films
where title like '%Matrix%';