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 #299

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
97 changes: 97 additions & 0 deletions .ipynb_checkpoints/README-checkpoint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png)

# LAB | SQL Joins

<details>
<summary>
<h2>Learning Goals</h2>
</summary>

This lab allows you to practice and apply the concepts and techniques taught in class.

Upon completion of this lab, you will be able to:

- Use SQL joins to combine data from multiple tables, such as inner, outer, left, right or self-joins.

<br>
<hr>

</details>

<details>
<summary>
<h2>Prerequisites</h2>
</summary>

Before this starting this lab, you should have learnt about:

- SELECT, FROM, ORDER BY, LIMIT, WHERE, GROUP BY, and HAVING clauses. DISTINCT, AS keywords.
- Built-in SQL functions such as COUNT, MAX, MIN, AVG, ROUND, DATEDIFF, or DATE_FORMAT.
- Using JOIN to combine data from multiple tables.

<br>
<hr>

</details>


## Introduction

Welcome to the SQL Joins lab!

In this lab, you will be working with the [Sakila](https://dev.mysql.com/doc/sakila/en/) database on movie rentals. Specifically, you will be practicing how to perform joins on multiple tables in SQL. Joining multiple tables is a fundamental concept in SQL, allowing you to combine data from different tables to answer complex queries. Furthermore, you will also practice how to use aggregate functions to calculate summary statistics on your joined data.


## Challenge - Joining on multiple tables

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


1. List the number of films per category.
2. Retrieve the store ID, city, and country for each store.
3. Calculate the total revenue generated by each store in dollars.
4. Determine the average running time of films for each category.


**Bonus**:

5. Identify the film categories with the longest average running time.
6. Display the top 10 most frequently rented movies in descending order.
7. Determine if "Academy Dinosaur" can be rented from Store 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`."

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.

## Requirements

- Fork this repo
- Clone it to your machine


## Getting Started

Complete the challenges in this readme in a `.sql`file.

## Submission

- Upon completion, run the following commands:

```bash
git add .
git commit -m "Solved lab"
git push origin master
```

- Paste the link of your lab in Student Portal.

98 changes: 98 additions & 0 deletions joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
USE sakila

-- 1. List the number of films per category
SELECT c.name AS category_name, COUNT(f.film_id) AS num_films
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;

-- 2. Retrieve the store ID, city, and country for each store
SELECT s.store_id, ci.city, co.country
FROM store s
JOIN address a ON s.address_id = a.address_id
JOIN city ci ON a.city_id = ci.city_id
JOIN country 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 s
JOIN staff st ON s.store_id = st.store_id
JOIN payment 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 c.name AS category_name, AVG(f.length) AS avg_length
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;

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

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

-- 6. Determine if "Academy Dinosaur" can be rented from Store 1
SELECT f.title, i.inventory_id, s.store_id,
CASE
WHEN i.inventory_id IS NOT NULL THEN 'Available'
ELSE 'Not Available'
END AS availability
FROM film f
JOIN inventory i ON f.film_id = i.film_id
JOIN store s ON i.store_id = s.store_id
WHERE f.title = 'Academy Dinosaur' AND s.store_id = 1;

-- 7. Provide a list of all distinct film titles, along with their availability status in the inventory
SELECT 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 ON f.film_id = i.film_id;

-- 8. List the number of films that have been released
SELECT COUNT(f.film_id) AS total_films
FROM film f;

-- 9. List the number of films per rating
SELECT f.rating, COUNT(f.film_id) AS num_films
FROM film f
GROUP BY f.rating;

-- 10. List the number of films per rating, sorted by the number of films in descending order
SELECT f.rating, COUNT(f.film_id) AS num_films
FROM film f
GROUP BY f.rating
ORDER BY num_films DESC;

-- 11. Determine the mean film duration for each rating
SELECT f.rating, ROUND(AVG(f.length), 2) AS avg_duration
FROM film f
GROUP BY f.rating;

-- 12. Identify which ratings have a mean duration of over two hours
SELECT f.rating, ROUND(AVG(f.length), 2) AS avg_duration
FROM film f
GROUP BY f.rating
HAVING AVG(f.length) > 120;

-- Bonus: Determine which last names are not repeated in the actor table
SELECT a.last_name
FROM actor a
GROUP BY a.last_name
HAVING COUNT(a.last_name) = 1;