Skip to content
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
48 changes: 48 additions & 0 deletions lab-sql-joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#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.
Use Sakila;
#Let's join the table category (where we have the movie categories) with film_category(with the multiple id's)
SELECT film_id
FROM film_category;
SELECT
category.name,
COUNT(film_category.film_id) AS films_per_category
FROM category
JOIN film_category
ON category.category_id = film_category.category_id
GROUP BY category.name;
# 2. Retrieve the store ID, city, and country for each store.
SELECT
store.store_id,
city.city,
country.country
FROM store
JOIN address
ON store.address_id = address.address_id
JOIN city
ON address.city_id = city.city_id
JOIN country
ON city.country_id = country.country_id;
# 3. Calculate the total revenue generated by each store in dollars.
#I need SUM() for the total revenue. I need to GROUP BY store. I need to find the column amount.
SELECT
store.store_id,
SUM(payment.amount) AS total_revenue
FROM store
JOIN customer
ON store.store_id = customer.store_id
JOIN payment
ON customer.customer_id = payment.customer_id
GROUP BY store.store_id;
# 4. Determine the average running time of films for each category.
#I need the length from Film. I need to do AVG. I need to join per category.
SELECT
category.name,
AVG(film.length) AS average_running_time
FROM film
JOIN film_category
ON film.film_id = film_category.film_id
JOIN category
ON film_category.category_id = category.category_id
GROUP BY category.name;