From 6f84aa0215ffa5204c344288ad60d35a9e19b2fe Mon Sep 17 00:00:00 2001 From: cristinarosa97 Date: Sun, 22 Dec 2024 21:17:51 +0100 Subject: [PATCH 1/3] w4 lab5 done --- lab5.sql | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 lab5.sql diff --git a/lab5.sql b/lab5.sql new file mode 100644 index 0000000..9e7e93b --- /dev/null +++ b/lab5.sql @@ -0,0 +1,87 @@ +USE sakila; + +#1 Determine the number of copies of the film "Hunchback Impossible" that exist in the inventory system. + +SELECT COUNT(*) +FROM inventory as inv +INNER JOIN (SELECT title, film_id + FROM film + WHERE title = "Hunchback Impossible") as f +ON inv.film_id = f.film_id; + + +#2 List all films whose length is longer than the average length of all the films in the Sakila database. + +SELECT * +FROM film +WHERE length > (SELECT AVG(length) FROM film); + + +#3 Use a subquery to display all actors who appear in the film "Alone Trip". + +SELECT first_name, last_name +FROM actor +INNER JOIN film_actor +ON actor.actor_id = film_actor.actor_id +INNER JOIN (SELECT film_id + FROM film + WHERE title = "Alone Trip") as film +ON film_actor.film_id = film.film_id; + +#4 Sales have been lagging among young families, and you want to target family movies for a promotion. Identify all movies categorized as family films. +SELECT film.film_id, title, category_name +FROM film +INNER JOIN film_category as fc +ON film.film_id = fc.film_id +INNER JOIN (SELECT category_id, name as category_name + FROM category + WHERE name = "Family") as cat +ON fc.category_id = cat.category_id; + +#5 Retrieve the name and email of customers from Canada using both subqueries and joins. To use joins, you will need to identify the relevant tables and their primary +# and foreign keys. + +SELECT c.first_name, c.last_name, c.email, country.country +FROM customer c +JOIN address a +ON c.address_id = a.address_id +JOIN city +ON a.city_id = city.city_id +JOIN (SELECT country_id, country FROM country WHERE country = "Canada") country +ON city.country_id = country.country_id; + +#6 Determine which films were starred by the most prolific actor in the Sakila database. A prolific actor is defined as the actor who has acted in the most number of films. +#First, you will need to find the most prolific actor and then use that actor_id to find the different films that he or she starred in. + +SELECT actor_id, COUNT(*) as number_of_films +FROM film_actor +GROUP BY actor_id +ORDER BY number_of_films DESC; + +SELECT film.film_id, film.title +FROM film +JOIN (SELECT actor_id, film_id from film_actor +WHERE actor_id = 107) a +ON film.film_id = a.film_id; + +#7 Find the films rented by the most profitable customer in the Sakila database. You can use the customer and payment tables to find the most profitable customer, i.e., +#the customer who has made the largest sum of payments. +SELECT p.customer_id, SUM(amount) as total_amount +FROM payment p +GROUP BY customer_id +ORDER BY total_amount DESC; + +SELECT customer_id, title +FROM inventory inv +JOIN (SELECT * FROM rental WHERE customer_id = 526) r +ON r.inventory_id = inv.inventory_id +JOIN film +ON inv.film_id = film.film_id; + +#8 Retrieve the client_id and the total_amount_spent of those clients who spent more than the average of the total_amount spent by each client. +#You can use subqueries to accomplish this. + +SELECT customer_id, SUM(amount) as total_amount_spent +FROM payment +WHERE amount > (SELECT AVG(amount) FROM payment) +GROUP BY customer_id; \ No newline at end of file From b2846016c6cbce961444ca2ea819e3331f54514e Mon Sep 17 00:00:00 2001 From: cristinarosa97 Date: Sun, 22 Dec 2024 21:21:34 +0100 Subject: [PATCH 2/3] w4 lab5 done --- lab5.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab5.sql b/lab5.sql index 9e7e93b..4f48a8d 100644 --- a/lab5.sql +++ b/lab5.sql @@ -83,5 +83,5 @@ ON inv.film_id = film.film_id; SELECT customer_id, SUM(amount) as total_amount_spent FROM payment -WHERE amount > (SELECT AVG(amount) FROM payment) -GROUP BY customer_id; \ No newline at end of file +GROUP BY customer_id +HAVING total_amount_spent > (SELECT AVG(amount) FROM payment); \ No newline at end of file From 931fdaa1447b386dd975d9789e06394829b1b80c Mon Sep 17 00:00:00 2001 From: cristinarosa97 Date: Sun, 22 Dec 2024 21:23:53 +0100 Subject: [PATCH 3/3] w4 lab5 done --- lab5.sql | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/lab5.sql b/lab5.sql index 4f48a8d..5491fcb 100644 --- a/lab5.sql +++ b/lab5.sql @@ -53,30 +53,35 @@ ON city.country_id = country.country_id; #6 Determine which films were starred by the most prolific actor in the Sakila database. A prolific actor is defined as the actor who has acted in the most number of films. #First, you will need to find the most prolific actor and then use that actor_id to find the different films that he or she starred in. -SELECT actor_id, COUNT(*) as number_of_films -FROM film_actor -GROUP BY actor_id -ORDER BY number_of_films DESC; - -SELECT film.film_id, film.title -FROM film -JOIN (SELECT actor_id, film_id from film_actor -WHERE actor_id = 107) a -ON film.film_id = a.film_id; +SELECT film.title +FROM film +JOIN film_actor ON film.film_id = film_actor.film_id +WHERE film_actor.actor_id = ( + SELECT actor_id + FROM ( + SELECT actor_id, COUNT(film_id) AS films + FROM film_actor + GROUP BY actor_id + ORDER BY films DESC + LIMIT 1 + ) AS subquery +); #7 Find the films rented by the most profitable customer in the Sakila database. You can use the customer and payment tables to find the most profitable customer, i.e., #the customer who has made the largest sum of payments. -SELECT p.customer_id, SUM(amount) as total_amount -FROM payment p -GROUP BY customer_id -ORDER BY total_amount DESC; - -SELECT customer_id, title -FROM inventory inv -JOIN (SELECT * FROM rental WHERE customer_id = 526) r -ON r.inventory_id = inv.inventory_id -JOIN film -ON inv.film_id = film.film_id; + +SELECT film.title +FROM film +JOIN inventory ON film.film_id = inventory.film_id +JOIN rental ON inventory.inventory_id = rental.inventory_id +JOIN payment ON rental.rental_id = payment.rental_id +WHERE payment.customer_id = ( + SELECT customer_id + FROM payment + GROUP BY customer_id + ORDER BY SUM(amount) DESC + LIMIT 1 +); #8 Retrieve the client_id and the total_amount_spent of those clients who spent more than the average of the total_amount spent by each client. #You can use subqueries to accomplish this.