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

Done #231

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Done #231

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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

join_exercises.sql
62 changes: 62 additions & 0 deletions join_lab.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
USE sakila;

# 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."

# 1 List the number of films per category.
SELECT COUNT(FM.film_id), FM.category_id, C.name
FROM film_category AS FM
JOIN category AS C
USING(category_id)
GROUP BY category_id;

# 2 Retrieve the store ID, city, and country for each store.
SELECT S.store_id, CI.city, CO.country
FROM store AS S
JOIN address AS A
USING (address_id)
JOIN city AS CI
USING (city_id)
JOIN country AS CO
USING (country_id);

# 3 Calculate the total revenue generated by each store in dollars
SELECT S.store_id, SUM(P.amount) AS total_revenue
FROM staff AS S
JOIN payment AS P
USING(staff_id)
GROUP BY store_id;

# 4 Determine the average running time of films for each category
SELECT FC.category_id AS "cat_id", AVG(F.length) AS 'avg_running_time'
FROM film AS F
JOIN film_category AS FC
USING (film_id)
GROUP BY cat_id;

############################################### Bonus: #################################################################################
# 5 Identify the film categories with the longest average running time.
SELECT C.category_id, C.name, AVG(F.length) AS Average_length
FROM category AS C
JOIN film_category AS FC
USING (category_id)
JOIN film AS F
USING (film_id)
GROUP BY C.category_id, C.name
ORDER BY AVG(length) DESC;

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

SELECT F.film_id, F.title, COUNT(rental_id) AS 'Frequency'
FROM film AS F
JOIN inventory AS I
USING (film_id)
JOIN rental AS R
USING (inventory_id)
GROUP BY F.film_id
ORDER BY COUNT(rental_id) DESC
LIMIT 10;