CREATE TABLE Films (
id serial PRIMARY KEY,
title VARCHAR(255) NOT NULL UNIQUE,
genre VARCHAR(255) NOT NULL,
released INTEGER NOT NULL CHECK (released BETWEEN 1000 and 9999),
score INTEGER NOT NULL CHECK(score between 1 and 10)
);
ALTER TABLE films
RENAME TO film
INSERT INTO film
(title, genre, released, 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', 1998, 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', 1996, 9),
('Unforgiven', 'Western', 1992, 7)
select * from film
select * from film order by score desc
select * from film order by released
select * from film where (score >= 8)
select * from film where (score <= 7)
select * from film where (released = 1990)
select * from film where (released < 2000)
select * from film where (released > 1990)
select * from film where (released > 1990) and (released < 1999)
select * from film where genre = 'SciFi'
select * from film where genre = 'SciFi' OR genre = 'Western'
select * from film where genre != 'SciFi'
select * from film where genre = 'Western' AND (released < 2000)
select * from film where title like '%Matrix%'
select ROUND(AVG(score), 2) AS ScoreAverage
from film
select COUNT(title) from film
select genre, ROUND(AVG(score), 2) AS ScoreAverage
from film
group by genre
CREATE TABLE Director(
id serial PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
insert into director
(name)
VALUES
('Frank Darabon'),
('Francis Ford Coppola'),
('Cristopher Nolan'),
('Ridley Scott'),
('Paul Verhoven'),
('Lana & Lilly Wachowski'),
('Lana Wachowski'),
('John Mctiernan'),
('Rob Reiner'),
('Jane Campion'),
('David Mackenzie'),
('Sergio Leone'),
('Clint Eastwood')
ALTER TABLE film
ADD directorId INT
update film
set directorid = 1
where film.title = 'The Shawshank Redemption';
update film
set directorid = 2
where film.title = 'The Godfater';
update film
set directorid = 3
where film.title = 'The Dark Knight';
update film
set directorid = 4
where film.title = 'Total Recall';
etc:
select film.title, director."name" AS DirectorName
from film
left join director
on film.directorid = director.id
select director."name" as director, count(film.directorid) AS filmsdirected from director
left join film
on director.id = film.directorid
group by director
order by filmsdirected desc
In this exercise you are going to practice write SQL for CREATE TABLE, INSERT and SELECT statements. Some examples have been provided for you in the sql/products folder.
- Create a database
- Explain that each record should have an identifier known as a primary key
- Use a tool to get visibility on the state of the database
- Use SQL to create and drop a table in the database
- Explain that a table has rows and columns also known as records and fields
- Use SQL to insert and records in a table
Sign up to a cloud Postgres database provider like Neon or Supabase, or install Postgres locally, and create a new database instance.
Write a SQL CREATE TABLE statement that creates a table to store films. The table should have the following columns:
- A unique film id (this should be the primary key, use
SERIALorINTEGER AUTOINCREMENTto make it increment automatically) - A unique title
- A genre
- A release year (make this
integerdata type) - A score out of 10 (make this
integerdata type)
Write SQL INSERT statements to add the following records in to the films table.
title, genre, release_year, score
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
Write SQL SELECT statements to return films matching the below criteria:
- All films
- All films ordered by rating descending
- All films ordered by release year ascending
- All films with a rating of 8 or higher
- All films with a rating of 7 or lower
- films released in 1990
- films released before 2000
- films released after 1990
- films released between 1990 and 1999
- films with the genre of "SciFi"
- films with the genre of "Western" or "SciFi"
- films with any genre apart from "SciFi"
- films with the genre of "Western" released before 2000
- films that have the world "Matrix" in their title
For the last query, you will need to research the LIKE keyword in postgres.
Write a SQL SELECT statements to:
- Return the average film rating
- Return the total number of films
- Return the average film rating by genre
For these, you will need to research COUNT, AVG and GROUP BY keywords in postgres.
Create a new directors table. Each director should have a name and a unique director id. Recreate your films table and add a directorId column. Insert a few director records (the data does not need to be real, you can just make directors up). Re-insert your film data, updating each film ao it will have a directorId. Using an SQL JOIN, write a SELECT statement that returns a list of films with their director.
Write a SQL SELECT statement that returns a lists of directors along with the number of films they have directed.