From 6439fc36fcfab352d74eec3b5a93e639db74eb60 Mon Sep 17 00:00:00 2001 From: moniblanes Date: Mon, 23 Mar 2026 20:55:54 +0100 Subject: [PATCH 1/2] solved --- solution.sql | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 solution.sql diff --git a/solution.sql b/solution.sql new file mode 100644 index 0000000..0541ecb --- /dev/null +++ b/solution.sql @@ -0,0 +1,45 @@ +USE sakila; + +-- 1. List the number of films per category +SELECT + category.name AS category, + COUNT(film_category.film_id) AS number_of_films +FROM category +JOIN film_category + ON category.category_id = film_category.category_id +GROUP BY category.category_id, category.name; + +-- 2. Retrieve the store ID, city, and country for each store +SELECT + store.store_id, + city.city, + country.country +FROM store +JOIN address + ON store.address_id = address.address_id +JOIN city + ON address.city_id = city.city_id +JOIN country + ON city.country_id = country.country_id; + +-- 3. Calculate the total revenue generated by each store in dollars +SELECT + store.store_id, + SUM(payment.amount) AS total_revenue +FROM store +JOIN staff + ON store.store_id = staff.store_id +JOIN payment + ON staff.staff_id = payment.staff_id +GROUP BY store.store_id; + +-- 4. Determine the average running time of films for each category +SELECT + category.name AS category, + ROUND(AVG(film.length), 2) AS average_running_time +FROM category +JOIN film_category + ON category.category_id = film_category.category_id +JOIN film + ON film_category.film_id = film.film_id +GROUP BY category.category_id, category.name; \ No newline at end of file From b3ac01c509b1374ef39c54b2baba0fd328454ef8 Mon Sep 17 00:00:00 2001 From: moniblanes Date: Mon, 23 Mar 2026 21:15:02 +0100 Subject: [PATCH 2/2] solved --- solution.sql | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/solution.sql b/solution.sql index 0541ecb..c8fc228 100644 --- a/solution.sql +++ b/solution.sql @@ -42,4 +42,60 @@ JOIN film_category ON category.category_id = film_category.category_id JOIN film ON film_category.film_id = film.film_id -GROUP BY category.category_id, category.name; \ No newline at end of file +GROUP BY category.category_id, category.name; + +-- 5. Identify the film categories with the longest average running time +SELECT + category.name AS category, + ROUND(AVG(film.length), 2) AS avg_length +FROM category +JOIN film_category + ON category.category_id = film_category.category_id +JOIN film + ON film_category.film_id = film.film_id +GROUP BY category.name +ORDER BY avg_length DESC +LIMIT 1; + + +-- 6. Display the top 10 most frequently rented movies in descending order +SELECT + film.title, + COUNT(rental.rental_id) AS times_rented +FROM film +JOIN inventory + ON film.film_id = inventory.film_id +JOIN rental + ON inventory.inventory_id = rental.inventory_id +GROUP BY film.title +ORDER BY times_rented DESC +LIMIT 10; + + +-- 7. Determine if "Academy Dinosaur" can be rented from Store 1 +SELECT + film.title, + CASE + WHEN COUNT(inventory.inventory_id) > 0 THEN 'Available' + ELSE 'NOT available' + END AS availability +FROM film +LEFT JOIN inventory + ON film.film_id = inventory.film_id + AND inventory.store_id = 1 +WHERE film.title = 'Academy Dinosaur' +GROUP BY film.title; + + +-- 8. List all film titles with availability status +SELECT + film.title, + CASE + WHEN COUNT(inventory.inventory_id) > 0 THEN 'Available' + ELSE 'NOT available' + END AS availability +FROM film +LEFT JOIN inventory + ON film.film_id = inventory.film_id +GROUP BY film.title +ORDER BY film.title; \ No newline at end of file