diff --git a/Extension_2.sql b/Extension_2.sql new file mode 100644 index 0000000..44b2cba --- /dev/null +++ b/Extension_2.sql @@ -0,0 +1,80 @@ +-- Extension 2 + +-- Create a new table for film cast +CREATE TABLE film_cast ( + film_id INT REFERENCES films(film_id), + person_id INT REFERENCES people(person_id), + PRIMARY KEY (film_id, person_id) +); + +-- Add some actors +INSERT INTO people (full_name) VALUES + ('Gary Lockwood'), + ('Harrison Ford'), + ('Carrie Fisher'), + ('Alec Guinness'), + ('Kate Winslet'), + ('Omar Sharif'), + ('Sophia Loren'), + ('Jeroen Krabbé'), + ('Benoît Régent'), + ('Anne Brochet') + + +-- Insert actors into the film_cast table +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='2001: A Space Odyssey' AND p.full_name IN ('Keir Dullea','Gary Lockwood'); + + +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='Star Wars: A New Hope' AND p.full_name IN ('Mark Hamill','Harrison Ford','Carrie Fisher','Alec Guinness'); + + +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='To Kill A Mockingbird' AND p.full_name IN ('Gregory Peck'); + + +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='Titanic' AND p.full_name IN ('Leonardo DiCaprio','Kate Winslet'); + + +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='Dr Zhivago' AND p.full_name IN ('Julie Christie','Omar Sharif'); + + +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='El Cid' AND p.full_name IN ('Charlton Heston','Sophia Loren'); + + +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='Voyage to Cythera' AND p.full_name IN ('Manos Katrakis'); + + +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='Soldier of Orange' AND p.full_name IN ('Rutger Hauer','Jeroen Krabbé'); + + +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='Three Colours: Blue' AND p.full_name IN ('Juliette Binoche','Benoît Régent'); + + +INSERT INTO film_cast (film_id, person_id) +SELECT f.film_id, p.person_id FROM films f, people p +WHERE f.title='Cyrano de Bergerac' AND p.full_name IN ('Gerard Depardieu','Anne Brochet'); + + +-- Show the full name of actors in the film 'El Cid' +SELECT p.full_name AS actor +FROM film_cast fc +INNER JOIN films f ON f.film_id = fc.film_id +INNER JOIN people p ON p.person_id = fc.person_id +WHERE f.title = 'El Cid'; \ No newline at end of file diff --git a/table_setup.sql b/table_setup.sql new file mode 100644 index 0000000..64c1e81 --- /dev/null +++ b/table_setup.sql @@ -0,0 +1,119 @@ +-- Create tables 'people' and 'films' +CREATE TABLE people ( + person_id SERIAL PRIMARY KEY, + full_name TEXT NOT NULL UNIQUE, + country TEXT, + dob DATE, + email TEXT +); + +CREATE TABLE films ( + film_id SERIAL PRIMARY KEY, + title TEXT NOT NULL, + year INT, + genre TEXT, + score INT, + director_id INT REFERENCES people(person_id), + star_id INT REFERENCES people(person_id), + writer_id INT REFERENCES people(person_id) +); + +-- Insert data to tables +INSERT INTO people (full_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 people (full_name, dob) VALUES + ('Keir Dullea','1936-05-30'), + ('Mark Hamill','1951-09-25'), + ('Gregory Peck','1916-04-05'), + ('Leonardo DiCaprio','1974-11-11'), + ('Julie Christie','1940-04-14'), + ('Charlton Heston','1923-10-04'), + ('Manos Katrakis','1908-08-14'), + ('Rutger Hauer','1944-01-23'), + ('Juliette Binoche','1964-03-09'), + ('Gerard Depardieu','1948-12-27'); + +INSERT INTO people (full_name, email) VALUES + ('Arthur C Clarke','arthur@clarke.com'), + ('George Lucas','george@email.com'), + ('Harper Lee','harper@lee.com'), + ('James Cameron','james@cameron.com'), + ('Boris Pasternak','boris@boris.com'), + ('Frederick Frank','fred@frank.com'), + ('Theodoros Angelopoulos','theo@angelopoulos.com'), + ('Erik Hazelhoff Roelfzema','erik@roelfzema.com'), + ('Krzysztof Kieslowski','email@email.com'), + ('Edmond Rostand','edmond@rostand.com') +ON CONFLICT (full_name) DO UPDATE +SET email = EXCLUDED.email; + + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT '2001: A Space Odyssey',1968,'Science Fiction',10, + (SELECT person_id FROM people WHERE full_name='Stanley Kubrick'), + (SELECT person_id FROM people WHERE full_name='Keir Dullea'), + (SELECT person_id FROM people WHERE full_name='Arthur C Clarke'); + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT 'Star Wars: A New Hope',1977,'Science Fiction',7, + (SELECT person_id FROM people WHERE full_name='George Lucas'), + (SELECT person_id FROM people WHERE full_name='Mark Hamill'), + (SELECT person_id FROM people WHERE full_name='George Lucas'); + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT 'To Kill A Mockingbird',1962,'Drama',10, + (SELECT person_id FROM people WHERE full_name='Robert Mulligan'), + (SELECT person_id FROM people WHERE full_name='Gregory Peck'), + (SELECT person_id FROM people WHERE full_name='Harper Lee'); + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT 'Titanic',1997,'Romance',5, + (SELECT person_id FROM people WHERE full_name='James Cameron'), + (SELECT person_id FROM people WHERE full_name='Leonardo DiCaprio'), + (SELECT person_id FROM people WHERE full_name='James Cameron'); + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT 'Dr Zhivago',1965,'Historical',8, + (SELECT person_id FROM people WHERE full_name='David Lean'), + (SELECT person_id FROM people WHERE full_name='Julie Christie'), + (SELECT person_id FROM people WHERE full_name='Boris Pasternak'); + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT 'El Cid',1961,'Historical',6, + (SELECT person_id FROM people WHERE full_name='Anthony Mann'), + (SELECT person_id FROM people WHERE full_name='Charlton Heston'), + (SELECT person_id FROM people WHERE full_name='Frederick Frank'); + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT 'Voyage to Cythera',1984,'Drama',8, + (SELECT person_id FROM people WHERE full_name='Theodoros Angelopoulos'), + (SELECT person_id FROM people WHERE full_name='Manos Katrakis'), + (SELECT person_id FROM people WHERE full_name='Theodoros Angelopoulos'); + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT 'Soldier of Orange',1977,'Thriller',8, + (SELECT person_id FROM people WHERE full_name='Paul Verhoeven'), + (SELECT person_id FROM people WHERE full_name='Rutger Hauer'), + (SELECT person_id FROM people WHERE full_name='Erik Hazelhoff Roelfzema'); + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT 'Three Colours: Blue',1993,'Drama',8, + (SELECT person_id FROM people WHERE full_name='Krzysztof Kieslowski'), + (SELECT person_id FROM people WHERE full_name='Juliette Binoche'), + (SELECT person_id FROM people WHERE full_name='Krzysztof Kieslowski'); + +INSERT INTO films (title, year, genre, score, director_id, star_id, writer_id) +SELECT 'Cyrano de Bergerac',1990,'Historical',9, + (SELECT person_id FROM people WHERE full_name='Jean-Paul Rappeneau'), + (SELECT person_id FROM people WHERE full_name='Gerard Depardieu'), + (SELECT person_id FROM people WHERE full_name='Edmond Rostand'); \ No newline at end of file diff --git a/task1_to_5.sql b/task1_to_5.sql new file mode 100644 index 0000000..65e136a --- /dev/null +++ b/task1_to_5.sql @@ -0,0 +1,42 @@ +-- Task 1 - Show the title and director name for all films +SELECT + f.title, + d.full_name AS director +FROM films f +JOIN people d ON d.person_id = f.director_id; + + +-- Task 2 - Show the title, director and star name for all films +SELECT + f.title, + d.full_name AS director, + s.full_name AS star +FROM films f +JOIN people d ON d.person_id = f.director_id +JOIN people s ON s.person_id = f.star_id; + +-- Task 3 - Show the title of films where the director is from the USA +SELECT + f.title, + d.full_name AS director, + d.country +FROM films f +JOIN people d ON d.person_id = f.director_id +WHERE d.country = 'USA'; + +-- Task 4 - Show only those films where the writer and the director are the same person +SELECT + f.title, + d.full_name AS writer_and_director +FROM films f +INNER JOIN people d ON d.person_id = f.director_id +WHERE f.director_id = f.writer_id; + +-- Task 5 - Show directors and film titles for films with a score of 8 or higher +SELECT + f.title, + f.score, + d.full_name AS director +FROM films f +INNER JOIN people d ON d.person_id = f.director_id +WHERE score > 7; \ No newline at end of file diff --git a/task6.sql b/task6.sql new file mode 100644 index 0000000..52a491b --- /dev/null +++ b/task6.sql @@ -0,0 +1,46 @@ +-- Task 6 - Make at least 5 more queries to demonstrate your understanding of joins, and other relationships between tables. + +-- 1 - Show title and star name for all films with a score lower than 6 +SELECT + f.title, + f.score, + d.full_name AS star +FROM films f +INNER JOIN people d ON d.person_id = f.star_id +WHERE score < 6; + +-- 2 - Show all films with their year and genre, ordered by year +SELECT + f.title, + f.year, + f.genre, + d.full_name AS star +FROM films f +INNER JOIN people d ON d.person_id = f.star_id +ORDER BY f.year; + +-- 3 - Show the title and star name for all films in the 'Drama' genre +SELECT + f.title, + s.full_name AS star +FROM films f +INNER JOIN people s ON s.person_id = f.star_id +WHERE f.genre = 'Drama'; + +-- 4 - Show the title and star name for all films where the stars date of birth is before 1950 +SELECT + f.title, + s.full_name AS star, + s.dob +FROM films f +INNER JOIN people s ON s.person_id = f.star_id +WHERE s.dob < '1950-01-01'; + +-- 5 - Show the number of films directed by each country +SELECT + d.country, + COUNT(f.film_id) AS number_of_films +FROM films f +INNER JOIN people d ON d.person_id = f.director_id +GROUP BY d.country +ORDER BY number_of_films DESC; \ No newline at end of file