diff --git a/joins-lab.sql b/joins-lab.sql new file mode 100644 index 0000000..88b5ab0 --- /dev/null +++ b/joins-lab.sql @@ -0,0 +1,107 @@ +USE sakila; + +-- 1. List the number of films per category +SELECT + c.name AS category, + 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 + s.store_id, + SUM(p.amount) AS total_revenue +FROM store s +JOIN staff st + ON s.store_id = st.store_id +JOIN payment p + ON st.staff_id = p.staff_id +GROUP BY s.store_id; + + +-- 4. Determine the average running time of films for each category +SELECT + c.name AS category, + AVG(f.length) AS avg_running_time +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_running_time DESC; + + +-- BONUS + +-- 5. Identify the film categories with the longest average running time +SELECT + c.name AS category, + AVG(f.length) AS avg_running_time +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_running_time DESC +LIMIT 1; + + +-- 6. Display the top 10 most frequently rented movies +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, + i.store_id, + COUNT(i.inventory_id) AS copies_available +FROM film f +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; + + +-- 8. List all distinct film titles with availability status +SELECT + f.title, + CASE + WHEN COUNT(i.inventory_id) > 0 THEN 'Available' + ELSE 'NOT available' + END AS availability +FROM film f +LEFT JOIN inventory i + ON f.film_id = i.film_id +GROUP BY f.title +ORDER BY f.title; \ No newline at end of file