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-Solved lab #228

Open
wants to merge 3 commits 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
69 changes: 69 additions & 0 deletions lab-sql-joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
USE sakila;
#Write SQL queries to perform the following tasks using the Sakila database:
#List the number of films per category.

SELECT COUNT(title) AS number_of_films, name
FROM film
INNER JOIN film_category
USING (film_id)
INNER JOIN category
USING (category_id)
GROUP BY name
ORDER BY number_of_films DESC;

#Retrieve the store ID, city, and country for each store.
SELECT store_id, city, country
FROM store
INNER JOIN address
USING (address_id)
INNER JOIN city
USING (city_id)
INNER JOIN country
USING (country_id);

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

#Determine the average running time of films for each category.
SELECT AVG(length) AS average_running_time, name
FROM film
INNER JOIN film_category
USING (film_id)
INNER JOIN category
USING (category_id)
GROUP BY name
ORDER BY average_running_time DESC;

# 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."
SELECT DISTINCT(title), inventory_id,
CASE
WHEN ISNULL(return_date) THEN 'Not Available'
ELSE 'Available'
END AS availability
FROM film
INNER JOIN inventory
USING (film_id)
INNER JOIN rental
USING (inventory_id)
WHERE return_date IS NULL;


SELECT title, inventory_id,
CASE
WHEN ISNULL(inventory_id) THEN 'Not Available'
ELSE 'Exists'
END AS status
FROM film
LEFT JOIN inventory
USING(film_id)
WHERE inventory_id IS NULL;