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
8 changes: 8 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.

8 changes: 8 additions & 0 deletions sql/extension/extension1/select.sql
Original file line number Diff line number Diff line change
@@ -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;
46 changes: 46 additions & 0 deletions sql/extension/extension2/directors.sql
Original file line number Diff line number Diff line change
@@ -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'));
4 changes: 4 additions & 0 deletions sql/extension/extension2/select.sql
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions sql/extension/extension3/select.sql
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions sql/films/create-films.sql
Original file line number Diff line number Diff line change
@@ -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)
);
15 changes: 15 additions & 0 deletions sql/films/insert-films.sql
Original file line number Diff line number Diff line change
@@ -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);
27 changes: 27 additions & 0 deletions sql/films/selects-films.sql
Original file line number Diff line number Diff line change
@@ -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%';