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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/api-sql-intro.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions sql/products/core.sql
Original file line number Diff line number Diff line change
@@ -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%'
5 changes: 5 additions & 0 deletions sql/products/extension1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT AVG(score) FROM films

SELECT COUNT(*) FROM films

SELECT AVG(score), genre FROM films GROUP BY genre
42 changes: 42 additions & 0 deletions sql/products/extension2.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions sql/products/extension3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT name, COUNT(films.id) FROM directors LEFT JOIN films ON films.director_id = directors.id GROUP BY name