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
81 changes: 81 additions & 0 deletions joins_lab.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
USE sakila;

-- JOINING TABLES

-- 1. List the number of films per category.
SELECT * FROM category;
SELECT * FROM film_category;


SELECT
cn.name AS film_category,
COUNT(*) AS number_of_films
FROM category AS cn
JOIN film_category AS fc
ON cn.category_id = fc.category_id
GROUP BY cn.name
ORDER BY film_category;

-- 2. Retrieve the store ID, city, and country for each store.
SELECT
st.store_id,
ci.city,
co.country
FROM store AS st
JOIN address AS ad
ON st.address_id = ad.address_id
JOIN city AS ci
ON ad.city_id = ci.city_id
JOIN country AS co
ON ci.country_id = co.country_id;

-- 3. Calculate the total revenue generated by each store in dollars.
SELECT
st.store_id,
SUM(pa.amount) as total_store_revenue
FROM store AS st
JOIN customer AS cu
ON st.store_id= cu.store_id
JOIN payment AS pa
ON cu.customer_id = pa.customer_id
GROUP BY st.store_id;

SELECT
st.store_id,
ad.address,
SUM(pa.amount) as total_store_revenue
FROM store AS st
JOIN address AS ad
ON st.address_id = ad.address_id
JOIN customer AS cu
ON st.store_id= cu.store_id
JOIN payment AS pa
ON cu.customer_id = pa.customer_id
GROUP BY st.store_id, ad.address;

-- 4. Determine the average running time of films for each category.
SELECT
cn.name as film_category,
SUM(f.length) AS running_time
FROM category AS cn
JOIN film_category AS fc
ON cn.category_id = fc.category_id
JOIN film AS f
ON fc.film_id = f.film_id
GROUP BY cn.name
ORDER BY film_category;

-- BONUS
-- 5. Identify the film categories with the longest average running time.


-- 6. Display the top 10 most frequently rented movies in descending order.


-- 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."