Skip to content
Open
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
65 changes: 51 additions & 14 deletions Solutions.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
--
-- Create a stored procedure that finds first name, last name, and emails of all the customers who rented `Action movies.
--
USE sakila;

--
-- Update the stored procedure in a such manner that it can take a string argument for the category name and return
-- the results for all customers that rented movie of that category/genre.
--
# First part
DELIMITER //
CREATE PROCEDURE action_customers()
BEGIN
select first_name, last_name, email
from customer
join rental on customer.customer_id = rental.customer_id
join inventory on rental.inventory_id = inventory.inventory_id
join film on film.film_id = inventory.film_id
join film_category on film_category.film_id = film.film_id
join category on category.category_id = film_category.category_id
where category.name = "Action"
group by first_name, last_name, email;
END
//
DELIMITER ;

CALL action_customers();

--
-- Write a query to check the number of movies released in each movie category.
--
# Second part
DELIMITER //
CREATE PROCEDURE r_category(in cat varchar(30))
BEGIN
select first_name, last_name, email
from customer
join rental on customer.customer_id = rental.customer_id
join inventory on rental.inventory_id = inventory.inventory_id
join film on film.film_id = inventory.film_id
join film_category on film_category.film_id = film.film_id
join category on category.category_id = film_category.category_id
where category.name = cat
group by first_name, last_name, email;
END
//
DELIMITER ;

--
-- Convert the query in to a stored procedure to filter only those categories that have movies released greater than a certain number.
-- Pass that number as an argument in the stored procedure.
--
CALL r_category("Action");

# Third part
drop procedure if exists film_cat;
DELIMITER //
CREATE PROCEDURE film_cat(in number int)
BEGIN
select c.name, count(fc.film_id) as n_films
from film_category fc
join category c on fc.category_id = c.category_id
group by c.name
having n_films > number;
END
//
DELIMITER ;

CALL film_cat(40);