From 443f4a39075cfc6a5cfe0796ea319e387146a2f7 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 29 Mar 2026 15:22:32 +0200 Subject: [PATCH] Solved lab --- lab-sql-joins.sql | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 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..98ab8ed --- /dev/null +++ b/lab-sql-joins.sql @@ -0,0 +1,90 @@ +-- 1. List the number of films per category. +SELECT c.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 +ORDER BY number_of_films 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 st.store_id, + SUM(p.amount) AS total_revenue +FROM payment p +JOIN staff s + ON p.staff_id = s.staff_id +JOIN store st + ON s.store_id = st.store_id +GROUP BY st.store_id +ORDER BY total_revenue DESC; +-- 4. Determine the average running time of films for each category. +SELECT c.name AS category, + ROUND(AVG(f.length),2) AS avg_duration +FROM film f +JOIN film_category fc + ON f.film_id = fc.film_id +JOIN category c + ON fc.category_id = c.category_id +GROUP BY c.name +ORDER BY avg_duration DESC; +-- 5. Identify the film categories with the longest average running time. +SELECT c.name AS category, + AVG(f.length) AS avg_duration +FROM film f +JOIN film_category fc + ON f.film_id = fc.film_id +JOIN category c + ON fc.category_id = c.category_id +GROUP BY c.name +ORDER BY avg_duration DESC +LIMIT 5; +-- 6. Display the top 10 most frequently rented movies in descending order. +SELECT f.title, + COUNT(r.rental_id) 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; +-- 7. Determine if "Academy Dinosaur" can be rented from Store 1. +SELECT f.title, + COUNT(i.inventory_id) AS available_copies +FROM film f +JOIN inventory i + ON f.film_id = i.film_id +LEFT JOIN rental r + ON i.inventory_id = r.inventory_id + AND r.return_date IS NULL +WHERE f.title = "Academy Dinosaur" + AND i.store_id = 1 + AND r.rental_id IS NULL +GROUP BY f.title; +-- 8. Provide a list of all distinct film titles, along with their availability status in the inventory. Include a column indicating whether each title is 'Available' or 'NOT available.' +-- Note that there are 42 titles that are not in the inventory, and this information can be obtained using a CASE statement combined with IFNULL. +SELECT f.title, + CASE + WHEN IFNULL(COUNT(i.inventory_id), 0) = 0 THEN 'NOT available' + WHEN COUNT(r.rental_id) < COUNT(i.inventory_id) THEN 'Available' + ELSE 'NOT available' + END AS availability +FROM film f +LEFT JOIN inventory i + ON f.film_id = i.film_id +LEFT JOIN rental r + ON i.inventory_id = r.inventory_id + AND r.return_date IS NULL +GROUP BY f.film_id, f.title; \ No newline at end of file