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
38 changes: 38 additions & 0 deletions SQL/Core/create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
CREATE TABLE IF NOT EXISTS Directors(
id SERIAL PRIMARY KEY,
name TEXT,
country TEXT
);

CREATE TABLE IF NOT EXISTS Stars(
id SERIAL PRIMARY KEY,
name TEXT,
DOB TEXT
);

CREATE TABLE IF NOT EXISTS Writers(
id SERIAL PRIMARY KEY,
name TEXT,
email TEXT
);

CREATE TABLE IF NOT EXISTS Films(
id SERIAL PRIMARY KEY,
title TEXT,
year INT,
genre TEXT,
score INT,
director_id INT,
star_id INT,
writer_id INT,

CONSTRAINT fk_director
FOREIGN KEY(director_id)
REFERENCES Directors(id),
CONSTRAINT fk_star
FOREIGN KEY(star_id)
REFERENCES Stars(id),
CONSTRAINT fk_writer
FOREIGN KEY(writer_id)
REFERENCES Writers(id)
);
47 changes: 47 additions & 0 deletions SQL/Core/insert.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
INSERT INTO Directors (name, country) VALUES
('Stanley Kubrick', 'USA'),
('George Lucas', 'USA'),
('Robert Mulligan', 'USA'),
('James Cameron', 'Canada'),
('David Lean', 'UK'),
('Anthony Mann', 'USA'),
('Theodoros Angelopoulos', 'Greece'),
('Paul Verhoeven', 'Netherlands'),
('Krzysztof Kieslowski', 'Poland'),
('Jean-Paul Rappeneau', 'France');

INSERT INTO Stars(name, DOB) VALUES
('Keir Dullea', '30/05/1936'),
('Mark Hamill', '25/09/1951'),
('Gregory Peck', '05/04/1916'),
('Leonardo DiCaprio', '11/11/1974'),
('Julie Christie', '14/04/1940'),
('Charlton Heston', '04/10/1923'),
('Manos Katrakis', '14/08/1908'),
('Rutger Hauer', '23/01/1944'),
('Juliette Binoche', '09/03/1964'),
('Gerard Depardieu', '27/12/1948');

INSERT INTO Writers(name, email) VALUES
('Arthur C Clarke', '[email protected]'),
('George Lucas', '[email protected]'),
('Harper Lee', '[email protected]'),
('James Cameron', '[email protected]'),
('Boris Pasternak', '[email protected]'),
('Frederick Frank', '[email protected]'),
('Theodoros Angelopoulos', '[email protected]'),
('Erik Hazelhoff Roelfzema', '[email protected]'),
('Krzysztof Kieslowski', '[email protected]'),
('Edmond Rostand', '[email protected]');

INSERT INTO films (title, genre, year, score, director_id, star_id, writer_id) VALUES
('2001: A Space Odyssey', 'Science Fiction', 1968, 10, 1, 1, 1),
('Star Wars: A New Hope', 'Science Fiction', 1977, 7, 2, 2, 2),
('To Kill A Mockingbird', 'Drama', 1962, 10, 3, 3, 3),
('Titanic', 'Romance', 1997, 5, 4, 4, 4),
('Dr Zhivago', 'Historical', 1965, 8, 5, 5, 5),
('El Cid', 'Historical', 1961, 6, 6, 6, 6),
('Voyage to Cythera', 'Drama', 1984, 8, 7, 7, 7),
('Soldier of Orange', 'Thriller', 1977, 8, 8, 8, 8),
('Three Colours: Blue', 'Drama', 1993, 8, 9, 9, 9),
('Cyrano de Bergerac', 'Historical', 1990, 9, 10, 10, 10);
62 changes: 62 additions & 0 deletions SQL/Core/select_core.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- I
SELECT title, directors.name as director FROM films
JOIN directors ON films.director_id = directors.id;

-- II

SELECT title, directors.name as director, stars.name as star FROM films
JOIN directors ON films.director_id = directors.id
JOIN stars ON films.star_id = stars.id;

-- III

SELECT title, directors.name AS director, directors.country AS country
FROM films
JOIN directors ON films.director_id = directors.id
WHERE directors.country = 'USA';

-- IV
SELECT title, directors.name AS director, writers.name AS writer
FROM films
JOIN directors ON films.director_id = directors.id
JOIN writers ON films.writer_id = writers.id
WHERE writers.name = directors.name;

-- V

SELECT title, directors.name as director, score
FROM films
JOIN directors ON films.director_id = directors.id
WHERE score >= 8;

-- VI

-- Show films and directors between 1965 and 1980
SELECT title, directors.name as director, year
FROM films
JOIN directors ON films.director_id = directors.id
WHERE year BETWEEN 1965 AND 1980;

-- Show stars on movies rated 8 or higher
SELECT title, stars.name, score
FROM films
JOIN stars ON films.star_id = stars.id
WHERE score >= 8;

-- Show directors from the genre Historical
SELECT title, directors.name, genre, score
FROM films
JOIN directors ON films.director_id = directors.id
WHERE genre = 'Historical';

-- Show who wrote Star wars
SELECT title, writers.name as writer, genre, score
FROM films
JOIN writers ON films.writer_id = writers.id
WHERE title LIKE '%Star Wars%';

-- Show the stars of the highest scored movies
SELECT title, stars.name as star, score
FROM films
JOIN stars ON films.star_id = stars.id
WHERE score = 10;
40 changes: 40 additions & 0 deletions SQL/Extension 1/create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
CREATE TABLE IF NOT EXISTS Directors(
id SERIAL PRIMARY KEY,
name TEXT,
country TEXT
);

CREATE TABLE IF NOT EXISTS Stars(
id SERIAL PRIMARY KEY,
name TEXT,
DOB TEXT
);

CREATE TABLE IF NOT EXISTS Writers(
id SERIAL PRIMARY KEY,
name TEXT,
email TEXT
);

CREATE TABLE IF NOT EXISTS People (
id SERIAL PRIMARY KEY,
director_id INT,
star_id INT,
writer_id INT,
FOREIGN KEY (director_id) REFERENCES Directors(id),
FOREIGN KEY (star_id) REFERENCES Stars(id),
FOREIGN KEY (writer_id) REFERENCES Writers(id)
);

CREATE TABLE IF NOT EXISTS Films(
id SERIAL PRIMARY KEY,
title TEXT,
year INT,
genre TEXT,
score INT,
people_id INT,
UNIQUE(title),
CONSTRAINT fk_people_id
FOREIGN KEY(people_id)
REFERENCES People(id)
);
48 changes: 48 additions & 0 deletions SQL/Extension 1/insert.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
INSERT INTO Directors (name, country) VALUES
('Stanley Kubrick', 'USA'),
('George Lucas', 'USA'),
('Robert Mulligan', 'USA'),
('James Cameron', 'Canada'),
('David Lean', 'UK'),
('Anthony Mann', 'USA'),
('Theodoros Angelopoulos', 'Greece'),
('Paul Verhoeven', 'Netherlands'),
('Krzysztof Kieslowski', 'Poland'),
('Jean-Paul Rappeneau', 'France');

INSERT INTO Stars(name, DOB) VALUES
('Keir Dullea', '30/05/1936'),
('Mark Hamill', '25/09/1951'),
('Gregory Peck', '05/04/1916'),
('Leonardo DiCaprio', '11/11/1974'),
('Julie Christie', '14/04/1940'),
('Charlton Heston', '04/10/1923'),
('Manos Katrakis', '14/08/1908'),
('Rutger Hauer', '23/01/1944'),
('Juliette Binoche', '09/03/1964'),
('Gerard Depardieu', '27/12/1948');

INSERT INTO Writers(name, email) VALUES
('Arthur C Clarke', '[email protected]'),
('George Lucas', '[email protected]'),
('Harper Lee', '[email protected]'),
('James Cameron', '[email protected]'),
('Boris Pasternak', '[email protected]'),
('Frederick Frank', '[email protected]'),
('Theodoros Angelopoulos', '[email protected]'),
('Erik Hazelhoff Roelfzema', '[email protected]'),
('Krzysztof Kieslowski', '[email protected]'),
('Edmond Rostand', '[email protected]');

-- People id starting index is 21
INSERT INTO films (title, genre, year, score, people_id) VALUES
('2001: A Space Odyssey', 'Science Fiction', 1968, 10, 21),
('Star Wars: A New Hope', 'Science Fiction', 1977, 7, 22),
('To Kill A Mockingbird', 'Drama', 1962, 10, 23),
('Titanic', 'Romance', 1997, 5, 24),
('Dr Zhivago', 'Historical', 1965, 8, 25),
('El Cid', 'Historical', 1961, 6, 26),
('Voyage to Cythera', 'Drama', 1984, 8, 27),
('Soldier of Orange', 'Thriller', 1977, 7, 28),
('Three Colours: Blue', 'Drama', 1993, 8, 29),
('Cyrano de Bergerac', 'Historical', 1990, 9, 30);
72 changes: 72 additions & 0 deletions SQL/Extension 1/select.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- I
SELECT title, directors.name as director FROM films
JOIN people on films.people_id = people.id
JOIN directors on people.director_id = directors.id;
-- II

SELECT title, directors.name as director, stars.name as star
FROM films
JOIN people on films.people_id = people.id
JOIN directors ON people.director_id = directors.id
JOIN stars ON people.star_id = stars.id;

-- III

SELECT title, directors.name AS director, directors.country AS country
FROM films
JOIN people on films.people_id = people.id
JOIN directors ON people.director_id = directors.id
WHERE directors.country = 'USA';

-- IV
SELECT title, directors.name AS director, writers.name AS writer
FROM films
JOIN people on films.people_id = people.id
JOIN directors ON people.director_id = directors.id
JOIN writers ON people.writer_id = writers.id
WHERE writers.name = directors.name;

-- V

SELECT title, directors.name as director, score
FROM films
JOIN people on films.people_id = people.id
JOIN directors ON people.director_id = directors.id
WHERE score >= 8;

-- VI

-- Show films and directors between 1965 and 1980
SELECT title, directors.name as director, year
FROM films
JOIN people on films.people_id = people.id
JOIN directors ON people.director_id = directors.id
WHERE year BETWEEN 1965 AND 1980;

-- Show stars on movies rated 8 or higher
SELECT title, stars.name, score
FROM films
JOIN people on films.people_id = people.id
JOIN stars ON people.star_id = stars.id
WHERE score >= 8;

-- Show directors from the genre Historical
SELECT title, directors.name, genre, score
FROM films
JOIN people on films.people_id = people.id
JOIN directors ON people.director_id = directors.id
WHERE genre = 'Historical';

-- Show who wrote Star wars
SELECT title, writers.name as writer, genre, score
FROM films
JOIN people on films.people_id = people.id
JOIN writers ON people.writer_id = writers.id
WHERE title LIKE '%Star Wars%';

-- Show the stars of the highest scored movies
SELECT title, stars.name as star, score
FROM films
JOIN people on films.people_id = people.id
JOIN stars ON people.star_id = stars.id
WHERE score = 10;