diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/api-sql-intro.iml b/.idea/api-sql-intro.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/api-sql-intro.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..37813b7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3f39777 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/sql/extension/extension1/select.sql b/sql/extension/extension1/select.sql new file mode 100644 index 0000000..2a2575d --- /dev/null +++ b/sql/extension/extension1/select.sql @@ -0,0 +1,8 @@ +select AVG(score) as avg_score from films + +SELECT COUNT(*) AS total_films FROM films; + +SELECT genre, ROUND(AVG(score), 2) AS avg_score +FROM films +GROUP BY genre +ORDER BY avg_score DESC, genre; \ No newline at end of file diff --git a/sql/extension/extension2/directors.sql b/sql/extension/extension2/directors.sql new file mode 100644 index 0000000..cc86e5f --- /dev/null +++ b/sql/extension/extension2/directors.sql @@ -0,0 +1,46 @@ +DROP TABLE IF EXISTS films CASCADE; +DROP TABLE IF EXISTS directors CASCADE; + +CREATE TABLE directors ( + director_id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL UNIQUE +); + +CREATE TABLE films ( + film_id SERIAL PRIMARY KEY, + title VARCHAR(255) NOT NULL UNIQUE, + genre VARCHAR(255) NOT NULL, + release_year INTEGER NOT NULL, + score INTEGER NOT NULL CHECK (score BETWEEN 0 AND 10), + director_id INTEGER REFERENCES directors(director_id) +); + +INSERT INTO directors (name) VALUES + ('Frank Darabont'), + ('Francis Ford Coppola'), + ('Christopher Nolan'), + ('Ridley Scott'), + ('Paul Verhoeven'), + ('Lana Wachowski'), + ('John McTiernan'), + ('Rob Reiner'), + ('Jane Campion'), + ('David Mackenzie'), + ('Sergio Leone'), + ('Clint Eastwood'); + +INSERT INTO films (title, genre, release_year, score, director_id) VALUES + ('The Shawshank Redemption','Drama', 1994, 9, (SELECT director_id FROM directors WHERE name='Frank Darabont')), + ('The Godfather', 'Crime', 1972, 9, (SELECT director_id FROM directors WHERE name='Francis Ford Coppola')), + ('The Dark Knight', 'Action', 2008, 9, (SELECT director_id FROM directors WHERE name='Christopher Nolan')), + ('Alien', 'SciFi', 1979, 9, (SELECT director_id FROM directors WHERE name='Ridley Scott')), + ('Total Recall', 'SciFi', 1990, 8, (SELECT director_id FROM directors WHERE name='Paul Verhoeven')), + ('The Matrix', 'SciFi', 1999, 8, (SELECT director_id FROM directors WHERE name='Lana Wachowski')), + ('The Matrix Resurrections','SciFi', 2021, 5, (SELECT director_id FROM directors WHERE name='Lana Wachowski')), + ('The Matrix Reloaded', 'SciFi', 2003, 6, (SELECT director_id FROM directors WHERE name='Lana Wachowski')), + ('The Hunt for Red October','Thriller',1990, 7, (SELECT director_id FROM directors WHERE name='John McTiernan')), + ('Misery', 'Thriller',1990, 7, (SELECT director_id FROM directors WHERE name='Rob Reiner')), + ('The Power Of The Dog', 'Western', 2021, 6, (SELECT director_id FROM directors WHERE name='Jane Campion')), + ('Hell or High Water', 'Western', 2016, 8, (SELECT director_id FROM directors WHERE name='David Mackenzie')), + ('The Good the Bad and the Ugly','Western',1966,9,(SELECT director_id FROM directors WHERE name='Sergio Leone')), + ('Unforgiven', 'Western', 1992, 7, (SELECT director_id FROM directors WHERE name='Clint Eastwood')); diff --git a/sql/extension/extension2/select.sql b/sql/extension/extension2/select.sql new file mode 100644 index 0000000..c07d36c --- /dev/null +++ b/sql/extension/extension2/select.sql @@ -0,0 +1,4 @@ +SELECT f.title, f.genre, f.release_year, f.score, d.name AS director +FROM films f + JOIN directors d ON d.director_id = f.director_id +ORDER BY f.release_year, f.title; \ No newline at end of file diff --git a/sql/extension/extension3/select.sql b/sql/extension/extension3/select.sql new file mode 100644 index 0000000..8ce0fb8 --- /dev/null +++ b/sql/extension/extension3/select.sql @@ -0,0 +1,5 @@ +SELECT d.name AS director, COUNT(f.film_id) AS film_count +FROM directors d + LEFT JOIN films f ON f.director_id = d.director_id +GROUP BY d.director_id, d.name +ORDER BY film_count DESC, director; \ No newline at end of file diff --git a/sql/films/create-films.sql b/sql/films/create-films.sql new file mode 100644 index 0000000..9f3d96f --- /dev/null +++ b/sql/films/create-films.sql @@ -0,0 +1,7 @@ +create table films ( + film_id SERIAL primary key, + title varchar(255) not null unique, + genre varchar(255) not null, + release_year INTEGER not null, + score integer not null check (score between 0 and 10) +); \ No newline at end of file diff --git a/sql/films/insert-films.sql b/sql/films/insert-films.sql new file mode 100644 index 0000000..6b6d25d --- /dev/null +++ b/sql/films/insert-films.sql @@ -0,0 +1,15 @@ +INSERT INTO films (title, genre, release_year, score) VALUES + ('The Shawshank Redemption', 'Drama', 1994, 9), + ('The Godfather', 'Crime', 1972, 9), + ('The Dark Knight', 'Action', 2008, 9), + ('Alien', 'SciFi', 1979, 9), + ('Total Recall', 'SciFi', 1990, 8), + ('The Matrix', 'SciFi', 1999, 8), + ('The Matrix Resurrections', 'SciFi', 2021, 5), + ('The Matrix Reloaded', 'SciFi', 2003, 6), + ('The Hunt for Red October', 'Thriller',1990, 7), + ('Misery', 'Thriller',1990, 7), + ('The Power Of The Dog', 'Western', 2021, 6), + ('Hell or High Water', 'Western', 2016, 8), + ('The Good the Bad and the Ugly','Western',1966, 9), + ('Unforgiven', 'Western', 1992, 7); \ No newline at end of file diff --git a/sql/films/selects-films.sql b/sql/films/selects-films.sql new file mode 100644 index 0000000..002e093 --- /dev/null +++ b/sql/films/selects-films.sql @@ -0,0 +1,27 @@ +select * from films; + +select * from films order by score desc, title; + +select * from films order by release_year asc, title + +select * from films where score >= 8 + +select * from films where score <= 7 + +select * from films where release_year = 1990 + +select * from films where release_year < 2000 + +SELECT * FROM films WHERE release_year > 1990; + +SELECT * FROM films WHERE release_year BETWEEN 1990 AND 1999; + +SELECT * FROM films WHERE genre = 'SciFi'; + +SELECT * FROM films WHERE genre = 'Western' or genre = 'SciFi' + +SELECT * FROM films WHERE genre <> 'SciFi'; + +SELECT * FROM films WHERE genre = 'Western' AND release_year < 2000; + +SELECT * FROM films WHERE title ILIKE '%Matrix%';