From b7c8cb7ea1381de4828c640242dc5d1a3a6f481b Mon Sep 17 00:00:00 2001 From: PaulaSotoG Date: Mon, 9 Dec 2024 16:31:28 +0100 Subject: [PATCH] Results --- lab-sql-joins.sql | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 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..81d0a7a --- /dev/null +++ b/lab-sql-joins.sql @@ -0,0 +1,88 @@ + +USE sakila; + +-- Write SQL queries to perform the following tasks using the Sakila database: +-- List the number of films per category. + +SELECT c.name AS category_name, COUNT(f.film_id) AS film_count +FROM sakila.category AS c +JOIN sakila.film_category AS fc ON c.category_id = fc.category_id +JOIN sakila.film AS f ON fc.film_id = f.film_id +GROUP BY c.name +ORDER BY film_count DESC; + +-- Retrieve the store ID, city, and country for each store. + +SELECT s.store_id, ci.city, co.country +FROM sakila.store AS s +JOIN sakila.address AS a ON s.address_id = a.address_id +JOIN sakila.city AS ci ON a.city_id = ci.city_id +JOIN sakila.country AS co ON ci.country_id = co.country_id; + + +-- Calculate the total revenue generated by each store in dollars. + +SELECT s.store_id, SUM(p.amount) AS total_revenue +FROM sakila.payment AS p +JOIN sakila.customer AS c ON p.customer_id = c.customer_id +JOIN sakila.store AS s ON c.store_id = s.store_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 avg_running_time +FROM sakila.film AS f +JOIN sakila.film_category AS fc ON f.film_id = fc.film_id +JOIN sakila.category AS c ON fc.category_id = c.category_id +GROUP BY c.name +ORDER BY avg_running_time DESC; + +-- Bonus: + +-- Identify the film categories with the longest average running time. + +SELECT c.name AS category_name, + ROUND(AVG(f.length), 2) AS avg_running_time +FROM sakila.film AS f +JOIN sakila.film_category AS fc ON f.film_id = fc.film_id +JOIN sakila.category AS c ON fc.category_id = c.category_id +GROUP BY c.name +ORDER BY avg_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 sakila.rental AS r +JOIN sakila.inventory AS i ON r.inventory_id = i.inventory_id +JOIN sakila.film AS f ON i.film_id = f.film_id +GROUP BY f.title +ORDER BY rental_count DESC +LIMIT 10; + +-- Determine if "Academy Dinosaur" can be rented from Store 1. + +SELECT i.inventory_id, f.title, i.store_id, + CASE + WHEN r.rental_id IS NULL OR r.return_date IS NOT NULL THEN 'Available' + ELSE 'Rented' + END AS availability +FROM sakila.inventory AS i +JOIN sakila.film AS f ON i.film_id = f.film_id +LEFT JOIN sakila.rental AS r ON i.inventory_id = r.inventory_id AND r.return_date IS NULL +WHERE f.title = 'Academy Dinosaur' +AND i.store_id = 1; + + +-- 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(i.inventory_id, 0) = 0 THEN 'NOT available' + ELSE 'Available' + END AS availability +FROM sakila.film AS f +LEFT JOIN sakila.inventory AS i ON f.film_id = i.film_id +GROUP BY f.title +ORDER BY availability DESC, f.title;