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
37 changes: 37 additions & 0 deletions create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
create table people(
id serial primary key,
name varchar(255) not null,
dob date,
email varchar(255)
);

create table directors(
id serial primary key,
country varchar(255),
personId int references people(id) not null,
);


create table films(
id serial primary key,
title varchar(255) not null unique,
release_year int not null,
score int not null,
directorId int references directors(id) not null,
starId int references people(id) not null,
writerId int references people(id) not null,
genreId int references genre(id) not null
);


create table genres(
id serial primary key,
name varchar(255) not null unique,
);

create table cast(
id serial primary key,
personId int references people(id) not null,
filmId int references films(id) not null
);

34 changes: 34 additions & 0 deletions design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Person

- int id pk
- string name
- datetime dob
- string email


## Director

- int id pk
- string country
- int personId fk

## Movie

- int id pk
- string title
- int releaseYear
- int score
- int directorId fk
- int star fk
- int writer fk
- int genre fk

## Genre

- int id pk
- string name

## Cast

- int personId fk pk
- int movieId fk pk
56 changes: 56 additions & 0 deletions populate_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
insert into genres(name) values ('Science Fiction'), ('Drama'), ('Romance'), ('Historical'), ('Thriller');
insert into people(name, dob, email) values
('Stanley Kubrick', null, null),
('George Lucas', null, '[email protected]'),
('Robert Mulligan', null, null),
('James Cameron', null, '[email protected]'),
('David Lean', null, null),
('Anthony Mann', null, null),
('Theodoros Angelopoulos', null, '[email protected]'),
('Paul Verhoeven', null, null),
('Krzysztof Kieslowski', null, null),
('Jean-Paul Rappeneau', null, null),
-- 11
('Keir Dullea', '30/05/1936', null),
('Mark Hamill', '25/09/1951', null),
('Gregory Peck', '05/04/1916', null),
('Leonardo DiCaprio', '11/11/1974', null),
('Julie Christie', '14/04/1940', null),
('Charlton Heston', '04/10/1923', null),
('Manos Katrakis', '14/08/1908', null),
('Rutger Hauer', '23/01/1944', null),
('Juliette Binoche', '09/03/1964', null),
('Gerard Depardieu', '27/12/1948', null),
-- 21
('Arthur C Clarke', null, '[email protected]'),
('Harper Lee', null, '[email protected]'),
('Boris Pasternak', null, '[email protected]'),
('Frederick Frank', null, '[email protected]'),
('Erik Hazelhoff Roelfzema', null, '[email protected]'),
('Krzysztof Kieslowski', null, '[email protected]'),
('Edmond Rostand', null, '[email protected]');

insert into directors(country, personId) values
('USA', 1),
('USA', 2),
('USA', 3),
('Canada', 4),
('UK', 5),
('USA', 6),
('Greece', 7),
('Netherlands', 8),
('Poland', 9),
('France', 10);

insert into films(title, release_year, score, directorId, starId, writerId, genreId) values
('2001: A Space Odyssey', 1968, 10, 1, 11, 21, 1),
('Star Wars: A New Hope', 1977, 7, 2, 12, 2, 1),
('To Kill A Mockingbird', 1962, 10, 3, 13, 22, 2),
('Titanic', 1997, 5, 4, 14, 4, 3),
('Dr Zhivago', 1965, 8, 5, 15, 23, 4),
('El Cid', 1961, 6, 6, 16, 24, 4),
('Voyage to Cythera', 1984, 8, 7, 17, 7, 2),
('Soldier of Orange', 1977, 8, 8, 18, 25, 5),
('Three Colours: Blue', 1993, 8, 9, 19, 26, 2),
('Cyrano de Bergerac', 1990, 9, 10, 20, 27, 3),

6 changes: 6 additions & 0 deletions q1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Show the title and director name for all films
select title, director_name
from films
inner join directorPerson
on films.directorId = directorPerson.id;

6 changes: 6 additions & 0 deletions q2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Show the title, director and star name for all films
select title, director_name, people.name star_name
from directorFilms
inner join people
on directorFilms.starId = people.id;

5 changes: 5 additions & 0 deletions q3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Show the title of films where the director is from the USA
select title
from directorFilms
where director_country = "USA";

5 changes: 5 additions & 0 deletions q4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Show only those films where the writer and the director are the same person
select title
from films
where directorId = writerId;

5 changes: 5 additions & 0 deletions q5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--Show directors and film titles for films with a score of 8 or higher
select title, director_name
from directorFilms
where score >= 8;

30 changes: 30 additions & 0 deletions q6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--Make at least 5 more queries to demonstrate your understanding of joins, and other relationships between tables.

create view directorPerson as
select name director_name, country director_country
from directors inner join people
on people.id = directors.personId;

create view directorFilms as
select *,
from films
inner join directorPerson on films.directorId = directorPerson.id;

create view directorStarFilms as
select directorFilms.*, dob star_dob, name star_name from
from directorFilms
inner join people on directorFilms.starId = people.id;

-- Show all films Leonardo DiCaprio has starred in
select title
from films
inner join people
on films.starId = people.id
where people.name = "Leonardo DiCaprio";

-- Show drama films
select title
from films
inner join genres
on films.genreId = genres.id
where genres.name = "Drama";