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
153 changes: 153 additions & 0 deletions lab_sql_joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
-- ==========================================================
-- Lab · SQL Joins (Sakila)
-- Objetivo: rellenar cada apartado con tu query (sin resolver aquí)
-- ==========================================================

USE sakila;

-- ==========================================================
-- CHALLENGE · Joining on multiple tables
-- ==========================================================

-- 1) List the number of films per category.
-- TODO:
--
SELECT
c.name,
COUNT(DISTINCT fc.film_id) as num_films
FROM category as c
LEFT JOIN film_category as fc
ON c.category_id = fc.category_id
GROUP BY c.name
ORDER BY num_films DESC;

-- ==========================================================
-- 2) Retrieve the store ID, city, and country for each store.
-- TODO:

SELECT
store.store_id,
city.city,
country.country
FROM store as store
LEFT JOIN address as address
ON store.address_id = address.address_id
LEFT JOIN city as city
ON city.city_id = address.city_id
LEFT JOIN country as country
ON city.country_id = country.country_id;


-- ==========================================================
-- 3) Calculate the total revenue generated by each store in dollars.
-- TODO:

SELECT
store.store_id,
SUM(payment.amount) AS total_revenue
FROM store AS store
LEFT JOIN staff AS staff
ON store.store_id = staff.store_id
LEFT JOIN payment AS payment
ON staff.staff_id = payment.staff_id
GROUP BY
store.store_id;


-- ==========================================================
-- 4) Determine the average running time of films for each category.
-- TODO:

SELECT
c.name,
ROUND(AVG(f.length), 0) as avg_length
FROM category AS c
LEFT JOIN film_category AS fc
ON c.category_id = fc.category_id
LEFT JOIN film AS f
ON fc.film_id = f.film_id
GROUP BY
c.name
ORDER BY
avg_length DESC;


-- ==========================================================
-- BONUS
-- ==========================================================

-- 5) Identify the film categories with the longest average running time.
-- TODO:

SELECT
c.name,
ROUND(AVG(f.length), 0) as avg_length
FROM category AS c
LEFT JOIN film_category AS fc
ON c.category_id = fc.category_id
LEFT JOIN film AS f
ON fc.film_id = f.film_id
GROUP BY
c.name
ORDER BY
avg_length DESC LIMIT 5;



-- ==========================================================
-- 6) Display the top 10 most frequently rented movies in descending order.
-- TODO:

SELECT
f.title,
COUNT(r.rental_id) as freq_rented
FROM film AS f
JOIN inventory AS i
ON f.film_id = i.film_id
JOIN rental AS r
ON i.inventory_id = r.inventory_id
GROUP BY f.title
ORDER BY freq_rented DESC
LIMIT 10;


-- ==========================================================
-- 7) Determine if "Academy Dinosaur" can be rented from Store 1.
-- TODO:

SELECT
DISTINCT f.title
FROM film AS f
JOIN inventory AS i
ON f.film_id = i.film_id
JOIN store AS s
ON i.store_id = s.store_id
WHERE
f.title = 'Academy Dinosaur' AND s.store_id = 1;


-- ==========================================================
-- 8) Distinct film titles + availability status ('Available' / 'NOT available')
-- Hint: `CASE` + `IFNULL`
-- TODO:
SELECT
f.title,
CASE
WHEN COUNT(i.inventory_id) > 0 THEN 'Available'
ELSE 'NOT available'
END AS availability_status
FROM film AS f
LEFT JOIN inventory AS i
ON f.film_id = i.film_id
GROUP BY
f.film_id,
f.title;