From 8b2e740078d537e6da5d87eacafae46bff18ff79 Mon Sep 17 00:00:00 2001 From: AnaP2020 Date: Sat, 4 Jul 2026 15:06:06 +0100 Subject: [PATCH] Lab 4 unit 9 done --- Lab 4.sql | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Lab 4.sql diff --git a/Lab 4.sql b/Lab 4.sql new file mode 100644 index 0000000..af5895c --- /dev/null +++ b/Lab 4.sql @@ -0,0 +1,110 @@ +-- List the Number of Films per Category +USE sakila; + +SELECT + c.name AS category_name, + 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; + +-- Retrieving 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; + +-- Calculating the Total Revenue Generated by Each Store + +SELECT + st.store_id, + SUM(p.amount) AS total_revenue +FROM + store st +JOIN + staff stf ON st.store_id = stf.store_id +JOIN + payment p ON stf.staff_id = p.staff_id +GROUP BY + st.store_id; + +-- Determining the Average Running Time of Films for Each Category + +SELECT + c.name AS category_name, + AVG(f.length) 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; + +-- Identifying the Film Categories with the Longest Average Running Time +SELECT + c.name AS category_name, + AVG(f.length) 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 +ORDER BY + average_running_time DESC +LIMIT 1; + +-- Displaying the Top 10 Most Frequently Rented Movies + +SELECT + f.title, + COUNT(*) AS rental_count +FROM + rental r +JOIN + inventory i ON r.inventory_id = i.inventory_id +JOIN + film f ON i.film_id = f.film_id +GROUP BY + f.title +ORDER BY + rental_count DESC +LIMIT 10; + +-- Determining if "Academy Dinosaur" Can Be Rented from Store 1 +SELECT + COUNT(*) AS is_available +FROM + inventory i +JOIN + film f ON i.film_id = f.film_id +JOIN + store s ON i.store_id = s.store_id +WHERE + f.title = 'Academy Dinosaur' AND s.store_id = 1; + +-- Listing all Distinct Film Titles and Their Availability +SELECT + f.title, + CASE WHEN IFNULL(i.inventory_id, 0) = 0 THEN 'NOT Available' ELSE 'Available' END AS availability +FROM + film f +LEFT JOIN + inventory i ON f.film_id = i.film_id +GROUP BY + f.title, i.inventory_id; \ No newline at end of file