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
68 changes: 68 additions & 0 deletions sql/products/core-queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
-- All films
select *
from films

-- All films ordered by rating descending
select *
from films
ORDER BY score DESC

--All films ordered by release year ascending
select *
from films
ORDER BY releaseYear ASC

--All films with a rating of 8 or higher
select *
from films
where score >= 8

--All films with a rating of 7 or lower
select *
from films
where score <= 7

--films released in 1990
select *
from films
where releaseYear = 1990

-- films released before 2000
select *
from films
where releaseYear < 2000

-- films released after 1990
select *
from films
where releaseYear > 1990

-- films released between 1990 and 1999
select *
from films
where releaseYear >= 1990 and releaseYear <= 1999

-- films with the genre of "SciFi"
select *
from films
where genre like 'SciFi'

-- films with the genre of "Western" or "SciFi"
select *
from films
where genre like 'SciFi' or genre like 'Western'

-- films with any genre apart from "SciFi"
select *
from films
where genre not like 'SciFi'

-- films with the genre of "Western" released before 2000
select *
from films
where genre like 'Western' and releaseYear < 2000

-- films that have the world "Matrix" in their title
select *
from films
where title like '%Matrix%'
15 changes: 8 additions & 7 deletions sql/products/create-products.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CREATE TABLE products(
id serial primary key,
name varchar(255) not null,
price int not null,
discount boolean,
unique(name)
)

-- film table
CREATE TABLE IF NOT EXISTS films (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title VARCHAR(128) NOT NULL UNIQUE,
genre VARCHAR(64) NOT NULL,
releaseYear integer NOT NULL,
score integer NOT NULL
);
15 changes: 15 additions & 0 deletions sql/products/extension1-queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Return the average film rating
select AVG(score)
from films
as average_score

-- Return the total number of films
select Count(*) as total_films
from films

-- Return the average film rating by genre
select genre, AVG(score)
from films
as average_score
GROUP BY genre

48 changes: 48 additions & 0 deletions sql/products/extension2-queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

-- director table
CREATE TABLE IF NOT EXISTS directors (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(64) NULL UNIQUE,
);

-- film table
CREATE TABLE IF NOT EXISTS films (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
directorsId integer NOT NULL,
title VARCHAR(128) NOT NULL UNIQUE,
genre VARCHAR(64) NOT NULL,
release_year integer NOT NULL,
rating integer NOT NULL,
CONSTRAINT fk_director FOREIGN KEY (directorsId) REFERENCES directors(id)
);


-- Insert sample data into users table
INSERT INTO directors (name)
VALUES
('Steven Spielberg'),
('James Cameron'),
('Michael Scorcese');

-- Insert sample data into users table
INSERT INTO films (directorsId,title, genre, release_year, rating)
VALUES
(1,'The Shawshank Redemption', 'Drama', 1994, 9),
(2,'The Godfather', 'Crime', 1972, 9),
(3,'The Dark Knight', 'Action', 2008, 9),
(3,'Alien', 'SciFi', 1979, 9),
(2,'Total Recall', 'SciFi', 1990, 8),
(3,'The Matrix', 'SciFi', 1999, 8),
(1,'The Matrix Resurrections', 'SciFi', 2021, 5),
(1,'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),
(2,'Hell or High Water', 'Western', 2016, 8),
(2,'The Good the Bad and the Ugly', 'Western', 1966, 9),
(1,'Unforgiven', 'Western', 1992, 7);

select f.title as movie_title, f.directorsId as directors_id, d.name as directors_name
from films f
join directors d
on f.directorsId = d.id
21 changes: 17 additions & 4 deletions sql/products/insert-products.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
insert into products(name, price, discount) VALUES ('apple', 135, false);
insert into products(name, price, discount) VALUES ('orange', 55, true);
insert into products(name, price, discount) VALUES ('kiwi', 165, true);
insert into products(name, price, discount) VALUES ('banana', 035, false);
-- Insert sample data into users table
INSERT INTO films (title, genre, releaseYear, 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);