diff --git a/lab-sql-joins.sql b/lab-sql-joins.sql new file mode 100644 index 0000000..5f0ca3f --- /dev/null +++ b/lab-sql-joins.sql @@ -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; \ No newline at end of file