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

Up2date #289

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
Binary file added DER.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 100 additions & 0 deletions lab-sql-joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
-- Write SQL queries to perform the following tasks using the Sakila database:

USE sakila;

-- List the number of films per category.
SELECT c.category_id, COUNT(film_id) as num_films
FROM category c
JOIN film_category fc
ON fc.category_id = c.category_id
GROUP BY c.category_id;

-- Retrieve the store ID, city, and country for each store.
SELECT s.store_id, ci.city, co.country
FROM store s
JOIN address a
USING (address_id)
JOIN city ci
USING (city_id)
JOIN country co
USING (country_id);

-- Calculate the total revenue generated by each store in dollars.
-- Objective: ammount
-- payment se asocia con staff (staff_id), staff se asocia con store (store_id)
SELECT s.store_id, COUNT(p.amount)
FROM payment p
JOIN staff st
USING (staff_id)
JOIN store s
USING (store_id)
GROUP BY s.store_id;

-- Determine the average running time of films for each category.
-- Objective: f.length.
-- film se asocia con film_category (film_id), film_category se asocia con category (category_id)
SELECT c.category_id, c.name, AVG(f.length)
FROM film f
JOIN film_category fc
USING (film_id)
JOIN category c
USING (category_id)
GROUP BY c.category_id;

-- Bonus:

-- Identify the film categories with the longest average running time.
-- Objective: f.length in descending order
SELECT c.category_id, c.name, AVG(f.length) AS mean_length
FROM film f
JOIN film_category fc
USING (film_id)
JOIN category c
USING (category_id)
GROUP BY c.category_id
ORDER BY mean_length DESC
LIMIT 5;

-- Display the top 10 most frequently rented movies in descending order.
-- Objective: r.rental_id
-- film se asocia con inventory (film_id), inventory se asocia con rental (inventory_id)
SELECT f.title, COUNT(r.rental_id) AS num_rent
FROM rental r
JOIN inventory i
USING (inventory_id)
JOIN film f
USING (film_id)
GROUP BY f.title
ORDER BY num_rent DESC
LIMIT 10;

-- Determine if "Academy Dinosaur" can be rented from Store 1.
-- Objective: link film_id con store_id
-- film se asocia con inventory (film_id), inventory se asocia con store (store_id)
SELECT f.title
FROM film f
JOIN inventory i
USING (film_id)
WHERE title = "Academy Dinosaur" AND store_id = 1;

-- 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
FROM film f
LEFT JOIN inventory i
USING (film_id)
GROUP BY f.title, film_id;



-- 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:
-- Make sure you understand the relationships between the tables in the database. This will help you determine which tables to join and which columns to use in your joins.
-- Identify a common column for both tables to use in the ON section of the join. If there isn't a common column, you may need to add another table with a common column.
-- Decide which table you want to use as the left table (immediately after FROM) and which will be the right table (immediately after JOIN).
-- Determine which table you want to include all records from. This will help you decide which type of JOIN to use. If you want all records from the first table, use a LEFT JOIN. If you want all records from the second table, use a RIGHT JOIN. If you want records from both tables only where there is a match, use an INNER JOIN.
-- Use table aliases to make your queries easier to read and understand. This is especially important when working with multiple tables.
-- Write the query
-- Tip 2: Break down the problem into smaller, more manageable parts. For example, you might start by writing a query to retrieve data from just two tables before adding additional tables to the join. Test your queries as you go, and check the output carefully to make sure it matches what you expect. This process takes time, so be patient and go step by step to build your query incrementally.
Loading