diff --git a/Joins_lab.sql b/Joins_lab.sql new file mode 100644 index 0000000..6b38179 --- /dev/null +++ b/Joins_lab.sql @@ -0,0 +1,81 @@ +USE SAKILA; +SHOW tables; +#1 List the number of films per category. +SELECT + c.name AS category, + COUNT(f.film_id) AS film_count +FROM film AS f +JOIN film_category AS fc + ON f.film_id = fc.film_id +JOIN category AS c + ON fc.category_id = c.category_id +GROUP BY c.name; +#2 Retrieve the store ID, city, and country for each store. +SELECT + store.store_id, + city.city, + country.country +FROM store +JOIN address + ON store.address_id = address.address_id +JOIN city + ON address.city_id = city.city_id +JOIN country + ON city.country_id = country.country_id; +#3. Calculate the total revenue generated by each store in dollars. +SELECT store.store_id ,SUM(payment.amount) AS total_revenue +FROM store +JOIN customer + ON store.store_id=customer.store_id +JOIN payment + ON customer.customer_id=payment.customer_id +GROUP BY store.store_id; +# Determine the average running time of films for each category. +SELECT + category.name AS category_name, + ROUND(AVG(film.length), 2) AS avg_running_time +FROM film +JOIN film_category + ON film.film_id = film_category.film_id +JOIN category + ON film_category.category_id = category.category_id +GROUP BY category.name +ORDER BY avg_running_time DESC; +## Display the top 10 most frequently rented movies in descending order. +SELECT +film.title +FROM film +JOIN inventory + ON film.film_id=inventory.film_id +JOIN rental + ON inventory.inventory_id=rental.inventory_id +GROUP BY film.title +ORDER BY title desc +LIMIT 10; +##Determine if "Academy Dinosaur" can be rented from Store 1. +SELECT + film.title, + inventory.store_id, + CASE + WHEN rental.rental_id IS NULL THEN 'Available' + ELSE 'Not Available' + END AS availability_status +FROM film +JOIN inventory + ON film.film_id = inventory.film_id +LEFT JOIN rental + ON inventory.inventory_id = rental.inventory_id + AND rental.return_date IS NULL +WHERE film.title = 'ACADEMY DINOSAUR' +AND inventory.store_id = 1; + +SELECT + film.title, + CASE + WHEN COUNT(inventory.inventory_id) = 0 THEN 'NOT available' + ELSE 'Available' + END AS availability_status +FROM film +LEFT JOIN inventory + ON film.film_id = inventory.film_id +GROUP BY film.title; \ No newline at end of file