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
98 changes: 98 additions & 0 deletions dbTablesAndData.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
CREATE TABLE Roles (
id SERIAL primary key,
name VARCHAR(100) UNIQUE NOT NULL
);

CREATE TABLE Genres (
id SERIAL primary key,
name VARCHAR(100) UNIQUE NOT NULL
);

CREATE TABLE Countries (
id SERIAL primary key,
name VARCHAR(100) UNIQUE NOT NULL
);

CREATE table People (
id SERIAL primary key,
name VARCHAR(100) UNIQUE NOT NULL,
dob date NULL,
email VARCHAR(100) NULL,
country_fk INTEGER NULL,
FOREIGN KEY (country_fk) REFERENCES Countries(id)
);

CREATE TABLE PeopleRole (
people_fk INTEGER NOT NULL,
role_fk INTEGER NOT NULL,
FOREIGN KEY (people_fk) REFERENCES People(id),
FOREIGN KEY (role_fk) REFERENCES Roles(id),
PRIMARY KEY (people_fk, role_fk)
);

CREATE TABLE Movies (
id SERIAL primary key,
title VARCHAR(100) UNIQUE NOT NULL,
year INTEGER NOT NULL,
rating INTEGER CONSTRAINT rating_range CHECK (rating BETWEEN 0 AND 10) NOT NULL,
genre_fk INTEGER NOT NULL,
director_fk INTEGER NOT NULL,
star_fk INTEGER NOT NULL,
writer_fk INTEGER NOT NULL,
FOREIGN KEY (genre_fk) references Genres(id),
FOREIGN KEY (director_fk) REFERENCES People(id),
FOREIGN KEY (star_fk) REFERENCES People(id),
FOREIGN KEY (writer_fk) REFERENCES People(id)
);


INSERT INTO roles (name)
VALUES
('director'),
('star'),
('writer');


INSERT INTO countries (name)
VALUES
('USA'),
('CANADA'),
('UK'),
('Greece'),
('Netherlands'),
('Poland'),
('France');


INSERT INTO Genres (name)
VALUES
('Science Fiction'),
('Drama'),
('Romance'),
('Historical'),
('Thriller');

INSERT INTO people (name, dob, email, country_fk)
VALUES
('Stanley Kubrick', NULL, null, 1),
('George Lucas', NULL, '[email protected]', 1),
('Robert Mulligan', NULL, null, 1),
('James Cameron', NULL, '[email protected]', 2),
('David Lean', null, null, 3),
('Anthony Mann', null, null, 1),
('Theodoros Angelopoulos', null, '[email protected]', 4),
('Paul Verhoeven', null, null, 5),
('Krzysztof Kieslowski', null, '[email protected]', 6),
('Jean-Paul Rappeneau', null, null, 7),
('Keir Dullea', '1936-05-30', NULL, NULL),
('Mark Hamill', '1951-09-25', NULL, NULL),
('Gregory Peck', '1916-04-05', NULL, NULL),
('Leonardo DiCaprio', '1974-11-11', NULL, NULL),
('Julie Christie', '1940-04-14', NULL, NULL),
('Charlton Heston', '1923-10-04', NULL, NULL),
('Manos Katrakis', '1908-08-14', NULL, NULL),
('Rutger Hauer', '1944-01-23', NULL, NULL),
('Juliette Binoche', '1964-03-09', NULL, NULL),
('Gerard Depardieu', '1948-12-27', NULL, NULL),
('Arthur C Clarke', NULL, '[email protected]', null),
('Harper Lee', NULL, '[email protected]', null),
40 changes: 40 additions & 0 deletions extension2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
CREATE TABLE "Cast" (
movie_fk INTEGER NOT NULL,
actor_fk INTEGER NOT NULL,
PRIMARY KEY (movie_fk, actor_fk),
FOREIGN KEY (movie_fk) REFERENCES Movies(id),
FOREIGN KEY (actor_fk) REFERENCES People(id)
);

INSERT INTO Roles (name)
VALUES ('actor');


INSERT INTO people (name, dob, email, country_fk)
VALUES
('Kate Winslet', '1975-08-05', NULL, 3),
('Harrison Ford', '1942-07-13', NULL, 1),
('Carrie Fisher', '1956-09-21', NULL, 1),
('Alec Guinness', '1914-04-2', NULL, 3);

INSERT INTO "Cast" (movie_fk, actor_fk)
VALUES
(4, 26), -- Kate winslet in titanic
(2, 27), -- Harrison Ford in star wars 1977
(2, 28), -- Carrie Fishe in star wars 1977
(2, 29), -- Alec Guinness in star wars 1977
(1, 11), -- keir dullea
(2, 12), -- mark hamill
(3, 13), -- gregory peck
(4, 14), -- leonardo dicaprio
(5, 15), -- Julie christie
(6, 16), -- Charlton Heston
(7, 17), -- Manos Katrakis
(8, 18), -- Rugter hauer
(9, 19), -- Jliette binoche
(10, 20) -- Gerard depardiue
;




8 changes: 8 additions & 0 deletions query1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[200~-- Show title and director name for all films
SELECT
m.title,
p.name AS Director
FROM
Movies m
LEFT JOIN
People p ON p.id = m.director_fk;
5 changes: 5 additions & 0 deletions query10.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Bonus 5: List movies that are either Historical or Romance
Select m.title, g.name AS Genre
FROM movies m
LEFT JOIN genres g on m.genre_fk = g.id
WHERE g.name = 'Historical' OR g.name = 'Romance';
6 changes: 6 additions & 0 deletions query2.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 m.title, p.name as Director, p2.name as Star
FROM
Movies m
LEFT JOIN People p ON p.id = m.director_fk
LEFT JOIN People p2 On p2.id = m.star_fk;
9 changes: 9 additions & 0 deletions query3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Show the title of films where the director is from the USA
SELECT
m.title
FROM
Movies m
JOIN
People p ON p.id = m.director_fk
LEFT JOIN countries c on p.country_fk = c.id
Where c.name = 'USA';
6 changes: 6 additions & 0 deletions query4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Show only those films where the writer and the director are the same person
SELECT
title
FROM
Movies
WHERE director_fk = writer_fk;
9 changes: 9 additions & 0 deletions query5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Show directors and film titles for films with a score of 8 or higher
SELECT
m.title,
p.name AS Director
FROM
Movies m
LEFT JOIN
People p ON p.id = m.director_fk
WHERE m.rating >= 8;
11 changes: 11 additions & 0 deletions query6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Bonus query 1: List all people and the roles they have
SELECT
p.name AS Person,
String_AGG(r.name, ', ') AS roles
FROM
People p
LEFT JOIN PeopleRole pr ON p.id = pr.people_fk
LEFT JOIN Roles r ON pr.role_fk = r.id
Group BY
p.id
ORDER BY p.id;
10 changes: 10 additions & 0 deletions query7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Bonus query 2: Show the number of roles each person has
SELECT
p.name AS Person,
Count(r.name) AS roles_count
FROM People p
LEFT JOIN PeopleRole pr ON p.id = pr.people_fk
LEFT JOIN Roles r ON pr.role_fk = r.id
GROUP BY p.name, p.id
HAVING Count(r.name) > 1
ORDER BY p.id, Count(r.name);
4 changes: 4 additions & 0 deletions query8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Bonus query 3: List all movies along with their genre name
SELECT m.title, g.name
FROM Movies m
LEFT JOIN Genres g ON g.id = m.genre_fk;
6 changes: 6 additions & 0 deletions query9.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Bonus query 4: Show how many directors each country has
SELECT c.name, Count(p.country_fk)
FROM People p
LEFT JOIN countries c on c.id = p.country_fk
GROUP BY c.name
ORDER BY Count(p.country_fk);