From e938eb53174df27ae045f78f39085032e800089c Mon Sep 17 00:00:00 2001 From: Shilpa Vellore Krishnamurthy Date: Thu, 9 Jul 2026 00:02:25 +0200 Subject: [PATCH] sqllab4 done --- lab 4.sql | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 lab 4.sql diff --git a/lab 4.sql b/lab 4.sql new file mode 100644 index 0000000..6a784b8 --- /dev/null +++ b/lab 4.sql @@ -0,0 +1,114 @@ +use Sakila; + +-- List the number of films per category. + +select count(film_id) as Total_film from film; + +SELECT rating, COUNT(film_id) AS number_of_films +FROM film +GROUP BY rating +order by desc;category + +-- or +use sakila; +select category_id from + +SELECT + c.name AS category_name, + COUNT(fc.film_id) AS number_of_films +FROM category c +INNER JOIN film_category fc ON c.category_id = fc.category_id +GROUP BY c.name; + +-- 2. Retrieve the store ID, city, and country for each store + +SELECT + s.store_id, + ci.city, + co.country +FROM store s +INNER JOIN address a ON s.address_id = a.address_id +INNER JOIN city ci ON a.city_id = ci.city_id +INNER 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, + SUM(p.amount) AS total_revenue_dollars +FROM store s +INNER JOIN staff st ON s.store_id = st.store_id +INNER JOIN payment p ON st.staff_id = p.staff_id +GROUP BY s.store_id; + +-- Determine the average running time of films for each category + +SELECT + c.name AS category_name, + ROUND(AVG(f.length), 2) AS average_running_time +FROM category c +INNER JOIN film_category fc ON c.category_id = fc.category_id +INNER JOIN film f ON fc.film_id = f.film_id +GROUP BY c.name; + +-- Identify the film categories with the longest average running time + +SELECT + c.name AS category_name, + ROUND(AVG(f.length), 2) AS average_running_time +FROM category c +INNER JOIN film_category fc ON c.category_id = fc.category_id +INNER JOIN film f ON fc.film_id = f.film_id +GROUP BY c.name +ORDER BY average_running_time DESC +LIMIT 1; + + +-- Display the top 10 most frequently rented movies in descending order + +SELECT + f.title, + COUNT(r.rental_id) AS rental_count +FROM film f +INNER JOIN inventory i ON f.film_id = i.film_id +INNER JOIN rental r ON i.inventory_id = r.inventory_id +GROUP BY f.title +ORDER BY rental_count DESC +LIMIT 10; + +-- Determine if "Academy Dinosaur" can be rented from Store 1 +SELECT + f.title, + i.store_id, + COUNT(i.inventory_id) AS copies_in_stock +FROM film f +INNER JOIN inventory i ON f.film_id = i.film_id +WHERE f.title = 'Academy Dinosaur' AND i.store_id = 1 +GROUP BY f.title, i.store_id; + +-- Title availability list (Handling the 42 missing items via CASE and IFNULL) + +SELECT + f.title, + CASE + WHEN IFNULL(i.inventory_id, 0) = 0 THEN 'NOT available' + ELSE 'Available' + END AS availability_status +FROM film f +LEFT JOIN inventory i ON f.film_id = i.film_id +GROUP BY f.title, availability_status; + + + + + + + + + + +-- Retrieve the store ID, city, and country for each store. + + + +-- Calculate the total revenue generated by each store in dollars. +-- Determine the average running time of films for each category.