diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.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/products/core.sql b/sql/products/core.sql new file mode 100644 index 0000000..2a5944d --- /dev/null +++ b/sql/products/core.sql @@ -0,0 +1,51 @@ +CREATE TABLE films ( + id SERIAL PRIMARY KEY, + title VARCHAR(50) UNIQUE NOT NULL, + genre VARCHAR(50) NOT NULL, + release_year INT NOT NULL, + score INT CHECK (score BETWEEN 0 AND 10) +); + +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); + +SELECT * FROM films; + +SELECT * FROM films ORDER BY SCORE DESC + +SELECT * FROM films ORDER BY release_year ASC + +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 > 1990 and release_year < 1999 + +SELECT * FROM films WHERE genre = 'SciFi' + +SELECT * FROM films WHERE genre = 'SciFi' OR genre = 'Western' + +SELECT * FROM films WHERE genre <> 'SciFi' + +SELECT * FROM films WHERE genre = 'Western' AND release_year < 2000 + +SELECT * FROM films WHERE title LIKE '%Matrix%' \ No newline at end of file diff --git a/sql/products/extension1.sql b/sql/products/extension1.sql new file mode 100644 index 0000000..769ff7a --- /dev/null +++ b/sql/products/extension1.sql @@ -0,0 +1,5 @@ +SELECT AVG(score) FROM films + +SELECT COUNT(*) FROM films + +SELECT AVG(score), genre FROM films GROUP BY genre \ No newline at end of file diff --git a/sql/products/extension2.sql b/sql/products/extension2.sql new file mode 100644 index 0000000..6ece84a --- /dev/null +++ b/sql/products/extension2.sql @@ -0,0 +1,42 @@ +CREATE TABLE IF NOT EXISTS directors( + id SERIAL PRIMARY KEY, + name TEXT NOT NULL +); + +INSERT INTO directors (name) VALUES +('Steven Spielberg'), +('Christopher Nolan'), +('Martin Scorsese'), +('Quentin Tarantino'), +('Jon Favreau'), +('Woody Allen'), +('Peter Jackson'); + +DROP TABLE films + +CREATE TABLE films ( + id SERIAL PRIMARY KEY, + title VARCHAR(50) UNIQUE NOT NULL, + genre VARCHAR(50) NOT NULL, + release_year INT NOT NULL, + score INT CHECK (score BETWEEN 0 AND 10), + director_id INT +); + +INSERT INTO films (title, genre, release_year, score, director_id) VALUES +('The Shawshank Redemption', 'Drama', 1994, 9, 1), +('The Godfather', 'Crime', 1972, 9, 2), +('The Dark Knight', 'Action', 2008, 9, 3), +('Alien', 'SciFi', 1979, 9, 4), +('Total Recall', 'SciFi', 1990, 8, 5), +('The Matrix', 'SciFi', 1999, 8, 6), +('The Matrix Resurrections', 'SciFi', 2021, 5, 7), +('The Matrix Reloaded', 'SciFi', 2003, 6,1), +('The Hunt for Red October', 'Thriller', 1990, 7,2), +('Misery', 'Thriller', 1990, 7,3), +('The Power Of The Dog', 'Western', 2021, 6,4), +('Hell or High Water', 'Western', 2016, 8,5), +('The Good the Bad and the Ugly', 'Western', 1966, 9,6), +('Unforgiven', 'Western', 1992, 7,7); + +SELECT * FROM films INNER JOIN directors ON films.director_id = directors.id; \ No newline at end of file diff --git a/sql/products/extension3.sql b/sql/products/extension3.sql new file mode 100644 index 0000000..7fda7a4 --- /dev/null +++ b/sql/products/extension3.sql @@ -0,0 +1 @@ +SELECT name, COUNT(films.id) FROM directors LEFT JOIN films ON films.director_id = directors.id GROUP BY name \ No newline at end of file