From e7c95769fbd228f5e20f4363f319b18f56cd1ad8 Mon Sep 17 00:00:00 2001 From: Oyvind Timian Dokk Husveg Date: Mon, 18 Aug 2025 16:13:59 +0200 Subject: [PATCH] Finished core, extension1 and extension2 --- dbTablesAndData.sql | 98 +++++++++++++++++++++++++++++++++++++++++++++ extension2.sql | 40 ++++++++++++++++++ query1.sql | 8 ++++ query10.sql | 5 +++ query2.sql | 6 +++ query3.sql | 9 +++++ query4.sql | 6 +++ query5.sql | 9 +++++ query6.sql | 11 +++++ query7.sql | 10 +++++ query8.sql | 4 ++ query9.sql | 6 +++ 12 files changed, 212 insertions(+) create mode 100644 dbTablesAndData.sql create mode 100644 extension2.sql create mode 100644 query1.sql create mode 100644 query10.sql create mode 100644 query2.sql create mode 100644 query3.sql create mode 100644 query4.sql create mode 100644 query5.sql create mode 100644 query6.sql create mode 100644 query7.sql create mode 100644 query8.sql create mode 100644 query9.sql diff --git a/dbTablesAndData.sql b/dbTablesAndData.sql new file mode 100644 index 0000000..654b9ae --- /dev/null +++ b/dbTablesAndData.sql @@ -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, 'george@email.com', 1), + ('Robert Mulligan', NULL, null, 1), + ('James Cameron', NULL, 'james@cameron.com', 2), + ('David Lean', null, null, 3), + ('Anthony Mann', null, null, 1), + ('Theodoros Angelopoulos', null, 'theo@angelopoulos.com', 4), + ('Paul Verhoeven', null, null, 5), + ('Krzysztof Kieslowski', null, 'email@email.com', 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, 'arthur@clarke.com', null), + ('Harper Lee', NULL, 'harper@lee.com', null), diff --git a/extension2.sql b/extension2.sql new file mode 100644 index 0000000..5b713e2 --- /dev/null +++ b/extension2.sql @@ -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 +; + + + + diff --git a/query1.sql b/query1.sql new file mode 100644 index 0000000..f366e28 --- /dev/null +++ b/query1.sql @@ -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; diff --git a/query10.sql b/query10.sql new file mode 100644 index 0000000..ac12a55 --- /dev/null +++ b/query10.sql @@ -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'; diff --git a/query2.sql b/query2.sql new file mode 100644 index 0000000..e71e6b4 --- /dev/null +++ b/query2.sql @@ -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; diff --git a/query3.sql b/query3.sql new file mode 100644 index 0000000..1083f9a --- /dev/null +++ b/query3.sql @@ -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'; diff --git a/query4.sql b/query4.sql new file mode 100644 index 0000000..3f62cd2 --- /dev/null +++ b/query4.sql @@ -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; diff --git a/query5.sql b/query5.sql new file mode 100644 index 0000000..0256f85 --- /dev/null +++ b/query5.sql @@ -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; diff --git a/query6.sql b/query6.sql new file mode 100644 index 0000000..693df7f --- /dev/null +++ b/query6.sql @@ -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; diff --git a/query7.sql b/query7.sql new file mode 100644 index 0000000..bb4faf0 --- /dev/null +++ b/query7.sql @@ -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); diff --git a/query8.sql b/query8.sql new file mode 100644 index 0000000..034b859 --- /dev/null +++ b/query8.sql @@ -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; diff --git a/query9.sql b/query9.sql new file mode 100644 index 0000000..fe5d4ad --- /dev/null +++ b/query9.sql @@ -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);