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
73 changes: 73 additions & 0 deletions lab-sql-joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

-- Challenge - Joining on multiple tables
-- Write SQL queries to perform the following tasks using the Sakila database:
USE sakila;
-- 1. List the number of films per category.
SELECT c.name AS category_name, COUNT(fc.film_id) AS number_of_films
FROM category AS c
LEFT JOIN film_category AS fc
ON c.category_id = fc.category_id
GROUP BY c.name
ORDER BY number_of_films DESC;

-- 2. Retrieve the store ID, city, and country for each store.

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

-- 3. Calculate the total revenue generated by each store in dollars.
SELECT s.store_id, SUM(p.amount) AS total_revenue
FROM store as s
LEFT JOIN staff as st ON s.store_id = st.store_id
LEFT JOIN payment as 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 cat.name, ROUND(AVG(f.length) , 2) AS avg_running_time
FROM film as f
LEFT JOIN film_category as fc ON f.film_id = fc.film_id
LEFT JOIN category as cat ON fc.category_id = cat.category_id
GROUP BY cat.name
ORDER BY avg_running_time DESC;

-- Bonus:
-- 5.Identify the film categories with the longest average running time.
SELECT c.name, AVG(f.length) 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
HAVING AVG(f.length) > (SELECT AVG(length) FROM film)
ORDER BY avg_length DESC;

-- 6. Display the top 10 most frequently rented movies in descending order.
SELECT f.title, COUNT(r.rental_id) AS rental_count
FROM film AS F
LEFT JOIN inventory AS i ON f.film_id = i.film_id
LEFT JOIN rental AS r ON i.inventory_id = r.inventory_id
GROUP BY f.title
ORDER BY rental_count DESC
LIMIT 10;

-- 7. Determine if "Academy Dinosaur" can be rented from Store 1.
SELECT film_id FROM film
WHERE title LIKE '%Academy Dinosaur%';

SELECT * FROM inventory
WHERE film_id = (SELECT film_id FROM film WHERE title= 'Academy Dinosaur')
AND store_id= 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."
SELECT f.title,
CASE
WHEN MAX(i.inventory_id) IS NULL THEN 'NOT available'
ELSE 'Available'
END AS availability_status
FROM film AS f
LEFT JOIN inventory AS i on f.film_id = i.film_id
GROUP BY f.title;