diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dfe955f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +join_exercises.sql diff --git a/join_lab.sql b/join_lab.sql new file mode 100644 index 0000000..f829e14 --- /dev/null +++ b/join_lab.sql @@ -0,0 +1,62 @@ +USE sakila; + +# 7 Determine if "Academy Dinosaur" can be rented from Store 1. +# 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." + +# 1 List the number of films per category. +SELECT COUNT(FM.film_id), FM.category_id, C.name +FROM film_category AS FM +JOIN category AS C +USING(category_id) +GROUP BY category_id; + +# 2 Retrieve the store ID, city, and country for each store. +SELECT S.store_id, CI.city, CO.country +FROM store AS S +JOIN address AS A +USING (address_id) +JOIN city AS CI +USING (city_id) +JOIN country AS CO +USING (country_id); + +# 3 Calculate the total revenue generated by each store in dollars +SELECT S.store_id, SUM(P.amount) AS total_revenue +FROM staff AS S +JOIN payment AS P +USING(staff_id) +GROUP BY store_id; + +# 4 Determine the average running time of films for each category +SELECT FC.category_id AS "cat_id", AVG(F.length) AS 'avg_running_time' +FROM film AS F +JOIN film_category AS FC +USING (film_id) +GROUP BY cat_id; + +############################################### Bonus: ################################################################################# +# 5 Identify the film categories with the longest average running time. +SELECT C.category_id, C.name, AVG(F.length) AS Average_length +FROM category AS C +JOIN film_category AS FC +USING (category_id) +JOIN film AS F +USING (film_id) +GROUP BY C.category_id, C.name +ORDER BY AVG(length) DESC; + +# 6 Display the top 10 most frequently rented movies in descending order. + +SELECT F.film_id, F.title, COUNT(rental_id) AS 'Frequency' +FROM film AS F +JOIN inventory AS I +USING (film_id) +JOIN rental AS R +USING (inventory_id) +GROUP BY F.film_id +ORDER BY COUNT(rental_id) DESC +LIMIT 10; + +