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

Solved Lab v1 #287

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,103 @@ Write SQL queries to perform the following tasks using the Sakila database:


1. List the number of films per category.

SELECT c.name, count(fc.category_id) as num_films
FROM film_category as fc
JOIN category as c
USING(category_id)
GROUP BY c.name
ORDER BY num_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
JOIN address
USING(address_id)
JOIN city as ci
USING(city_id)
JOIN country as co
USING (country_id);

3. Calculate the total revenue generated by each store in dollars.

SELECT s.store_id, c.city, SUM(p.amount) as total_revenue
from customer
JOIN store as s
USING(store_id)
JOIN payment as p
USING(customer_id)
JOIN address as a
ON c.address_id = a.address_id
JOIN city as c
USING(city_id)
GROUP BY s.store_id, c.city
ORDER BY total_revenue DESC;

4. Determine the average running time of films for each category.

SELECT c.name, ROUND(AVG(f.length)) as average_running_time
FROM film as f
JOIN film_category
USING(film_id)
JOIN category as c
USING(category_id)
GROUP BY c.name
ORDER BY average_running_time DESC;

**Bonus**:

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

SELECT c.name, ROUND(AVG(f.length)) as average_running_time
FROM film as f
JOIN film_category
USING(film_id)
JOIN category as c
USING(category_id)
GROUP BY c.name
ORDER BY average_running_time DESC
LIMIT 2;

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

SELECT f.title, COUNT(r.rental_id) as num_rentals
FROM rental as r
JOIN inventory
USING(inventory_id)
JOIN film as f
USING(film_id)
GROUP BY f.title
ORDER BY num_rentals DESC
LIMIT 10;

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

SELECT store_id, title,
CASE
WHEN COUNT(*) > 0 THEN 'YES'
ELSE 'NO'
END AS is_available
FROM store
JOIN inventory
USING(store_id)
JOIN film
USING(film_id)
WHERE store_id = 1 AND 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 DISTINCT(f.title),
CASE
WHEN IFNULL(i.inventory_id, 0) = 0 THEN 'NOT available'
ELSE 'Available'
END AS availability
FROM film f
LEFT JOIN inventory i
USING (film_id)
ORDER BY title;

Here are some tips to help you successfully complete the lab:

***Tip 1***: This lab involves joins with multiple tables, which can be challenging. Take your time and follow the steps we discussed in class:
Expand Down
131 changes: 131 additions & 0 deletions lab-sql-joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
USE sakila;


-- 1. Enumere el número de películas por categoría.

SELECT c.name AS categoria, COUNT(f.film_id) AS num_peliculas
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 num_peliculas DESC;


SELECT c.name, count(fc.category_id) as num_films
FROM film_category as fc
JOIN category as c
USING(category_id)
GROUP BY c.name
ORDER BY num_films DESC;


-- 2. Recupere el ID de la tienda, la ciudad y el país de cada tienda.
SELECT s.store_id, c.city, co.country
FROM store s
JOIN address a
ON s.address_id = a.address_id
JOIN city c
ON a.city_id = c.city_id
JOIN country co
ON c.country_id = co.country_id;


SELECT s.store_id, ci.city, co.country
FROM store as s
JOIN address
USING(address_id)
JOIN city as ci
USING(city_id)
JOIN country as co
USING (country_id);

-- 3. Calcule los ingresos totales generados por cada tienda en dólares.

SELECT s.store_id, c.city, SUM(p.amount) as total_revenue
FROM customer as cu
JOIN store as s
USING(store_id)
JOIN payment as p
USING(customer_id)
JOIN address as a
ON s.address_id = a.address_id
JOIN city as c
USING(city_id)
GROUP BY s.store_id, c.city
ORDER BY total_revenue DESC;


SELECT s.store_id, SUM(p.amount) as total_revenue
FROM customer
JOIN store as s
USING(store_id)
JOIN payment as p
USING(customer_id)
GROUP BY s.store_id
ORDER BY total_revenue DESC;

-- 4. Determinar el tiempo promedio de ejecución de las películas para cada categoría.

SELECT c.name, ROUND(AVG(f.length)) as average_running_time
FROM film as f
JOIN film_category
USING(film_id)
JOIN category as c
USING(category_id)
GROUP BY c.name
ORDER BY average_running_time DESC;

-- 5. Identifique las categorías de películas con el tiempo de ejecución promedio más largo.

SELECT c.name, ROUND(AVG(f.length)) as average_running_time
FROM film as f
JOIN film_category
USING(film_id)
JOIN category as c
USING(category_id)
GROUP BY c.name
ORDER BY average_running_time DESC
LIMIT 2;

-- 6. Muestra las 10 películas más alquiladas en orden descendente.

SELECT f.title, COUNT(r.rental_id) as num_rentals
FROM rental as r
JOIN inventory
USING(inventory_id)
JOIN film as f
USING(film_id)
GROUP BY f.title
ORDER BY num_rentals DESC
LIMIT 10;

-- 7. Determinar si "Academy Dinosaur" se puede alquilar en la Tienda 1.

SELECT store_id, title,
CASE
WHEN COUNT(*) > 0 THEN 'YES'
ELSE 'NO'
END AS is_available
FROM store
JOIN inventory
USING(store_id)
JOIN film
USING(film_id)
WHERE store_id = 1 AND title='Academy Dinosaur';


-- 8. Proporcione una lista de todos los títulos de películas distintos, junto con su estado de disponibilidad en el inventario.
-- Incluya una columna que indique si cada título está "Disponible" o "NO disponible".
-- Tenga en cuenta que hay 42 títulos que no están en el inventario y esta información se puede obtener utilizando una CASEdeclaración combinada con IFNULL".

SELECT DISTINCT(f.title),
CASE
WHEN IFNULL(i.inventory_id, 0) = 0 THEN 'NOT available'
ELSE 'Available'
END AS availability
FROM film f
LEFT JOIN inventory i
USING (film_id)
ORDER BY title;