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

Add files via upload #291

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
42 changes: 42 additions & 0 deletions Lab_sql_joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
USE sakila;
-- Write SQL queries to perform the following tasks using the Sakila database:
-- List the number of films per category.
SELECT COUNT(sfc.film_id), sc.name as category
FROM sakila.film_category as sfc
LEFT JOIN sakila.category as sc
ON sfc.category_id = sc.category_id
GROUP BY category;

-- Retrieve the store ID, city, and country for each store.
SELECT ss.store_id AS store_id, -- selecciono las columnas que quiero de las dos uniones realizadas (8)
sci.city as city,
scou.country as country
FROM sakila.store as ss -- mi primera tabla a unir (1)
join sakila.address as sa -- mi segunda tabla a unir (2)
on ss.address_id = sa.address_id -- columna comun de la primera unión (3)
join sakila.city as sci -- mi tercera tabla unir con el resultado de la unión previa (4)
on sa.city_id = sci.city_id -- columna comun de la segunda unión (5)
join sakila.country as scou -- mi cuarta tabla unir con el resultado de la unión previa (6)
on sci.country_id = scou.country_id; -- columna comun de la segunda unión (7)

-- Calculate the total revenue generated by each store in dollars.
SELECT ss.store_id AS store_id, -- selecciono las columnas que quiero de las dos uniones realizadas (6)
SUM(sp.amount) as revenue
FROM sakila.store as ss -- mi primera tabla a unir (1)
join sakila.staff as st -- mi segunda tabla a unir (2)
on ss.store_id = st.store_id -- columna comun de la primera unión (3)
join sakila.payment as sp -- mi tercera tabla unir con el resultado de la unión previa (4)
on st.staff_id = sp.staff_id -- columna comun de la segunda unión (5)
GROUP BY store_id;

-- Determine the average running time of films for each category.
SELECT sc.name as category_name,
AVG(sf.length) as duration_mean
FROM sakila.film as sf
JOIN sakila.film_category as sfc
on sf.film_id=sfc.film_id
join sakila.category as sc
on sfc.category_id=sc.category_id
GROUP BY category_name;