Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions sakila3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
-- Write SQL queries to perform the following tasks using the Sakila database:
USE Sakila;

-- 1. List the number of films per category.
-- checking what the table looks like

SELECT COUNT(fc.film_id) AS number_of_films, c.name AS cat_name
FROM sakila.film_category as fc
join sakila.category as c
ON fc.category_id = c.category_id
GROUP BY cat_name;

-- 2. Retrieve the store ID, city, and country for each store.
-- checking table content
SELECT * FROM sakila.address;

SELECT s.store_id, ci.city, co.country
FROM sakila.store AS s
JOIN sakila.address a
ON s.address_id = a.address_id
JOIN sakila.city ci
ON a.city_id = ci.city_id
JOIN sakila.country co
ON ci.country_id = co.country_id
ORDER BY co.country;

-- 3. Calculate the total revenue generated by each store in dollars.
-- check table
SELECT * FROM sakila.payment;

SELECT s.store_id, SUM(p.amount)
FROM sakila.store AS s
JOIN sakila.staff st
ON s.store_id = st.store_id
JOIN sakila.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 cat_name, ROUND(AVG(f.length),2) as avg_length
FROM sakila.film AS f
Join sakila.film_category fc
ON f.film_id = fc.film_id
join sakila.category c
ON fc.category_id = c.category_id
GROUP BY cat_name;
-- Bonus:
-- 5. Identify the film categories with the longest average running time.
SELECT c.name AS cat_name, ROUND(AVG(f.length),2) as avg_length
FROM sakila.film AS f
Join sakila.film_category fc
ON f.film_id = fc.film_id
join sakila.category c
ON fc.category_id = c.category_id
GROUP BY cat_name
ORDER BY avg_length DESC
LIMIT 3;
-- 6. Display the top 10 most frequently rented movies in descending order.
SELECT * FROM sakila.rental;

SELECT f.title AS film, COUNT(r.inventory_id) AS times_rented
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.film_id
ORDER BY times_rented DESC
LIMIT 10;

-- 7. Determine if "Academy Dinosaur" can be rented from Store 1.

SELECT f.title, i.store_id
FROM sakila.film AS f
JOIN sakila.inventory as i
ON f.film_id = i.film_id
WHERE f.title = 'Academy Dinosaur';
-- 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 * from sakila.inventory;


SELECT DISTINCT(f.title),
CASE
WHEN COUNT(i.inventory_id) = 0 THEN 'Not Available'
ELSE 'available'
END AS RENTAL_STATUS
FROM sakila.film AS f
LEFT JOIN sakila.inventory AS i
ON f.film_id = i.film_id
LEFT JOIN sakila.rental AS r
ON i.inventory_id = r.inventory_id
AND r.return_date is NULL
GROUP BY f.title
ORDER BY RENTAL_STATUS, f.title;