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 July #292

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
54 changes: 54 additions & 0 deletions lab-sql-joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
USE sakila;
## 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.

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, 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, MAX(p.amount) AS total_revenue
FROM store s
JOIN payment p ON s.store_id = payment_id
GROUP BY store_id;

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

SELECT c.name, AVG(f.length) AS average_time
FROM category c
JOIN film f ON c.category_id = film_id
GROUP BY c.name;

## Bonus:
## 5. Identify the film categories with the longest average running time.

SELECT c.name, AVG(f.length) AS avg_length
FROM category c
JOIN film f ON category_id = film_id
GROUP BY c.name
ORDER BY name DESC;

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

SELECT f.title, COUNT(r.rental_id) AS frecuency_rental
FROM film f
JOIN rental r ON film_id = rental_id
GROUP BY f.title
ORDER BY DESC
LIMIT 10;