From 0c1251a6dae391faf0e786527cdeeeef6d81b734 Mon Sep 17 00:00:00 2001 From: JulyBeiner Date: Sun, 1 Dec 2024 21:31:14 +0000 Subject: [PATCH] Add files via upload --- lab-sql-joins.sql | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lab-sql-joins.sql diff --git a/lab-sql-joins.sql b/lab-sql-joins.sql new file mode 100644 index 0000000..2350c3a --- /dev/null +++ b/lab-sql-joins.sql @@ -0,0 +1,54 @@ +USE sakila; +## Challenge - Joining on multiple tables +## Write SQL queries to perform the following tasks using the Sakila database: + +## 1.List the number of films per category. + +SELECT c.name AS category_name, COUNT(f.film_id) AS film_count +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 film_count DESC; + +## 2. Retrieve the store ID, city, and country for each store. + +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; + +## 3 Calculate the total revenue generated by each store in dollars. + +SELECT s.store_id, MAX(p.amount) AS total_revenue +FROM store s +JOIN payment p ON s.store_id = payment_id +GROUP BY store_id; + +## 4 Determine the average running time of films for each category. + +SELECT c.name, AVG(f.length) AS average_time +FROM category c +JOIN film f ON c.category_id = film_id +GROUP BY c.name; + +## Bonus: +## 5. Identify the film categories with the longest average running time. + +SELECT c.name, AVG(f.length) AS avg_length +FROM category c +JOIN film f ON category_id = film_id +GROUP BY c.name +ORDER BY name DESC; + +## 6. Display the top 10 most frequently rented movies in descending order. + +SELECT f.title, COUNT(r.rental_id) AS frecuency_rental +FROM film f +JOIN rental r ON film_id = rental_id +GROUP BY f.title +ORDER BY DESC +LIMIT 10; + +