diff --git a/LAB SQL JOIN.sql b/LAB SQL JOIN.sql new file mode 100644 index 0000000..74a6ec8 --- /dev/null +++ b/LAB SQL JOIN.sql @@ -0,0 +1,62 @@ +-- CHALLENGE +USE sakila; + +-- Numero de pelis por categoria +SELECT + c.name AS category, + COUNT(fc.film_id) AS number_of_films +FROM category c +JOIN film_category fc ON c.category_id = fc.category_id +GROUP BY c.name; + + +-- store id, ciudad y pais de cada tienda +SELECT + s.store_id, + ci.city, + co.country +FROM store s +JOIN address a ON s.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; + +-- Ingresos total por tienda +SELECT + s.store_id, + SUM(p.amount) AS total_revenue +FROM store s +JOIN staff st ON s.store_id = st.store_id +JOIN payment p ON st.staff_id = p.staff_id +GROUP BY s.store_id; + +-- Duracion promedio de las peliculas por categoria +SELECT + c.name AS category, + ROUND(AVG(f.length), 2) AS average_running_time +FROM category c +JOIN film_category fc ON c.category_id = fc.category_id +JOIN film f ON fc.film_id = f.film_id +GROUP BY c.name; + +-- BONUS +-- Pelis con la mayor duracion promedio por categoria +SELECT + c.name AS category, + ROUND(AVG(f.length), 2) AS avg_running_time +FROM category c +JOIN film_category fc ON c.category_id = fc.category_id +JOIN film f ON fc.film_id = f.film_id +GROUP BY c.name +ORDER BY avg_running_time DESC; + +-- Top 10 peliculas mas alquiladas + +SELECT + f.title, + COUNT(r.rental_id) AS total_rentals +FROM film f +JOIN inventory i ON f.film_id = i.film_id +JOIN rental r ON i.inventory_id = r.inventory_id +GROUP BY f.title +ORDER BY total_rentals DESC +LIMIT 10;