From f9170d7e28bc83ce9c2d721e9c0e23e6dfd33f6e Mon Sep 17 00:00:00 2001 From: javierdastas Date: Tue, 17 Dec 2024 15:54:53 -0400 Subject: [PATCH] LAB SQL Subqueries - Week 3 - Day 2 LAB SQL Subqueries. lab-sql-subqueries, Challenge 3, Week 3, Day 2. --- .ipynb_checkpoints/README-checkpoint.md | 20 ++++ .ipynb_checkpoints/solutions-checkpoint.sql | 103 +++++++++++++++++++ solutions.sql | 104 +++++++++++++++++++- 3 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/README-checkpoint.md create mode 100644 .ipynb_checkpoints/solutions-checkpoint.sql diff --git a/.ipynb_checkpoints/README-checkpoint.md b/.ipynb_checkpoints/README-checkpoint.md new file mode 100644 index 0000000..917f34d --- /dev/null +++ b/.ipynb_checkpoints/README-checkpoint.md @@ -0,0 +1,20 @@ +![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) + +# Lab | SQL Subqueries + +In this lab, you will be using the [Sakila](https://dev.mysql.com/doc/sakila/en/) database of movie rentals. Create appropriate joins wherever necessary. + +You should save your solutions in the `solutions.sql` file. + +
+ +## Instructions + +1. How many copies of the film _Hunchback Impossible_ exist in the inventory system? +2. List all films whose length is longer than the average of all the films. +3. Use subqueries to display all actors who appear in the film _Alone Trip_. +4. Sales have been lagging among young families, and you wish to target all family movies for a promotion. Identify all movies categorized as family films. +5. Get name and email from customers from Canada using subqueries. Do the same with joins. Note that to create a join, you will have to identify the correct tables with their primary keys and foreign keys, that will help you get the relevant information. +6. Which are films starred by the most prolific actor? Most prolific actor is defined as the actor that has acted in the most number of films. First you will have to find the most prolific actor and then use that actor_id to find the different films that he/she starred. +7. Films rented by most profitable customer. You can use the customer table and payment table to find the most profitable customer ie the customer that has made the largest sum of payments +8. Get 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. diff --git a/.ipynb_checkpoints/solutions-checkpoint.sql b/.ipynb_checkpoints/solutions-checkpoint.sql new file mode 100644 index 0000000..ab7b382 --- /dev/null +++ b/.ipynb_checkpoints/solutions-checkpoint.sql @@ -0,0 +1,103 @@ +use sakila; + +-- Query 1 +SELECT COUNT(*) AS copies_count +FROM inventory +WHERE film_id = ( + SELECT film_id + FROM film + WHERE title = 'Hunchback Impossible' +); + +-- Query 2 +SELECT title, length +FROM film +WHERE length > ( + SELECT AVG(length) + FROM film +); + +-- Query 3 +SELECT first_name, last_name +FROM actor +WHERE actor_id IN ( + SELECT actor_id + FROM film_actor + WHERE film_id = ( + SELECT film_id + FROM film + WHERE title = 'Alone Trip' + ) +); + +-- Query 4 +SELECT f.title +FROM film f +WHERE f.film_id IN ( + SELECT fc.film_id + FROM film_category fc + JOIN category c ON fc.category_id = c.category_id + WHERE c.name = 'Family' +); + +-- Query 5 +SELECT first_name, last_name, email +FROM customer +WHERE address_id IN ( + SELECT address_id + FROM address + WHERE city_id IN ( + SELECT city_id + FROM city + WHERE country_id = ( + SELECT country_id + FROM country + WHERE country = 'Canada' + ) + ) +); +-- Query 5 - Using Joins +SELECT cu.first_name, cu.last_name, cu.email +FROM customer cu +JOIN address a ON cu.address_id = a.address_id +JOIN city ci ON a.city_id = ci.city_id +JOIN country co ON ci.country_id = co.country_id +WHERE co.country = 'Canada'; + +-- Query 6 +SELECT f.title +FROM film f +JOIN film_actor fa ON f.film_id = fa.film_id +WHERE fa.actor_id = ( + SELECT actor_id + FROM film_actor + GROUP BY actor_id + ORDER BY COUNT(film_id) DESC + LIMIT 1 +); + +-- Query 7 +SELECT DISTINCT f.title +FROM film f +JOIN inventory i ON f.film_id = i.film_id +JOIN rental r ON i.inventory_id = r.inventory_id +WHERE r.customer_id = ( + SELECT customer_id + FROM payment + GROUP BY customer_id + ORDER BY SUM(amount) DESC + LIMIT 1 +); + +-- Query 8 +SELECT customer_id, SUM(amount) AS total_amount_spent +FROM payment +GROUP BY customer_id +HAVING total_amount_spent > ( + SELECT AVG(total_amount) + FROM ( + SELECT SUM(amount) AS total_amount + FROM payment + GROUP BY customer_id + ) AS subquery +); diff --git a/solutions.sql b/solutions.sql index d0eddcc..ab7b382 100644 --- a/solutions.sql +++ b/solutions.sql @@ -1 +1,103 @@ --- Add you solution queries below: +use sakila; + +-- Query 1 +SELECT COUNT(*) AS copies_count +FROM inventory +WHERE film_id = ( + SELECT film_id + FROM film + WHERE title = 'Hunchback Impossible' +); + +-- Query 2 +SELECT title, length +FROM film +WHERE length > ( + SELECT AVG(length) + FROM film +); + +-- Query 3 +SELECT first_name, last_name +FROM actor +WHERE actor_id IN ( + SELECT actor_id + FROM film_actor + WHERE film_id = ( + SELECT film_id + FROM film + WHERE title = 'Alone Trip' + ) +); + +-- Query 4 +SELECT f.title +FROM film f +WHERE f.film_id IN ( + SELECT fc.film_id + FROM film_category fc + JOIN category c ON fc.category_id = c.category_id + WHERE c.name = 'Family' +); + +-- Query 5 +SELECT first_name, last_name, email +FROM customer +WHERE address_id IN ( + SELECT address_id + FROM address + WHERE city_id IN ( + SELECT city_id + FROM city + WHERE country_id = ( + SELECT country_id + FROM country + WHERE country = 'Canada' + ) + ) +); +-- Query 5 - Using Joins +SELECT cu.first_name, cu.last_name, cu.email +FROM customer cu +JOIN address a ON cu.address_id = a.address_id +JOIN city ci ON a.city_id = ci.city_id +JOIN country co ON ci.country_id = co.country_id +WHERE co.country = 'Canada'; + +-- Query 6 +SELECT f.title +FROM film f +JOIN film_actor fa ON f.film_id = fa.film_id +WHERE fa.actor_id = ( + SELECT actor_id + FROM film_actor + GROUP BY actor_id + ORDER BY COUNT(film_id) DESC + LIMIT 1 +); + +-- Query 7 +SELECT DISTINCT f.title +FROM film f +JOIN inventory i ON f.film_id = i.film_id +JOIN rental r ON i.inventory_id = r.inventory_id +WHERE r.customer_id = ( + SELECT customer_id + FROM payment + GROUP BY customer_id + ORDER BY SUM(amount) DESC + LIMIT 1 +); + +-- Query 8 +SELECT customer_id, SUM(amount) AS total_amount_spent +FROM payment +GROUP BY customer_id +HAVING total_amount_spent > ( + SELECT AVG(total_amount) + FROM ( + SELECT SUM(amount) AS total_amount + FROM payment + GROUP BY customer_id + ) AS subquery +);