Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab-sql-joins_done #314

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
104 changes: 104 additions & 0 deletions lab-sql-joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
USE sakila;

-- Write SQL queries to perform the following tasks using the Sakila database:

-- 1. List the number of films per category.
SELECT * FROM film; -- film_id- PK
SELECT * FROM film_category; -- film_id - PK, FK
-- category_id PK, FK
SELECT * FROM category; -- category_id - PK

SELECT
c.name AS category_name, COUNT(f.film_id) AS film_count
FROM
category c
JOIN
film_category fc ON c.category_id = fc.category_id
JOIN
film f ON fc.film_id = f.film_id
GROUP BY c.name
ORDER BY film_count DESC;

-- 2. Retrieve the store ID, city, and country for each store.
SELECT
s.store_id, ci.city, c.country
FROM
store s
JOIN
address a ON s.address_id = a.address_id
JOIN
city ci ON ci.city_id = a.city_id
JOIN
country c ON c.country_id = ci.country_id;

-- 3. Calculate the total revenue generated by each store in dollars.
SELECT
*
FROM
payment;
SELECT
st.store_id, SUM(p.amount) AS total_revenue
FROM
store st
JOIN
staff s ON st.store_id = s.store_id
JOIN
payment p ON s.staff_id = p.staff_id
GROUP BY st.store_id;

-- 4. Determine the average running time of films for each category.
SELECT c.name AS genre,
AVG(f.length) AS avg_movie_duration
FROM category c
JOIN film_category fc ON fc.category_id = c.category_id
JOIN film f ON f.film_id = fc.film_id
GROUP BY c.name
ORDER BY avg_movie_duration ASC;

-- Bonus:

-- Identify the film categories with the longest average running time.
SELECT c.name AS genre,
MAX(f.length) AS longest_movie_duration
FROM category c
JOIN film_category fc ON fc.category_id = c.category_id
JOIN film f ON f.film_id = fc.film_id
GROUP BY c.name
ORDER BY longest_movie_duration ASC;

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

-- Determine if "Academy Dinosaur" can be rented from Store 1.
SELECT f.title, s.store_id,
COUNT(i.inventory_id) AS available_copies
FROM film f
JOIN inventory i ON i.film_id = f.film_id
JOIN store s ON s.store_id = i.store_id
WHERE title = 'Academy Dinosaur' AND s.store_id = 1
GROUP BY f.title;

/* 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 COUNT(i.inventory_id) > 0 THEN 'Available'
ELSE 'NOT available'
END AS availability_status
FROM film f
LEFT JOIN
inventory i ON f.film_id = i.film_id
GROUP BY
f.title
ORDER BY
f.title;