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

-- Determine the number of copies of the film "Hunchback Impossible" that exist in the inventory system.
SELECT COUNT(inventory.inventory_id) AS number_copies
FROM inventory
WHERE film_id = (
SELECT film_id
FROM film
WHERE title = 'Hunchback Impossible'
);


-- List all films whose length is longer than the average length of all the films in the Sakila database.
SELECT title, length
FROM film
WHERE length > (
SELECT AVG(length)
FROM film
)
ORDER BY length DESC;


-- Use a subquery to display all actors who appear in the film "Alone Trip".
SELECT actor.first_name, actor.last_name
FROM actor
WHERE actor.actor_id IN (
SELECT film_actor.actor_id
FROM film_actor
WHERE film_actor.film_id = (
SELECT film_id
FROM film
WHERE title = 'Alone Trip'
)
);

-- Sales have been lagging among young families, and you want to target family movies for a promotion. Identify all movies categorized as family films.
SELECT title
FROM film
WHERE film.film_id IN (
SELECT film_id
FROM film_category
WHERE category_id = (
SELECT category.category_id
FROM category
WHERE category.name = 'Family'
)
);


-- Retrieve the name and email of customers from Canada using both subqueries and joins. To use joins, you will need to identify the relevant tables and their primary and foreign keys.
SELECT customer.first_name, customer.last_name, customer.email
FROM customer
JOIN address ON customer.address_id = address.address_id
JOIN city ON address.city_id = city.city_id
WHERE city.country_id = (
SELECT country_id
FROM country
WHERE country = 'Canada'
);


-- Determine which films were starred by the most prolific actor in the Sakila database. A prolific actor is defined as the actor who has acted in the most number of films. First, you will need to find the most prolific actor and then use that actor_id to find the different films that he or she starred in.
SELECT actor.first_name, actor.last_name, film.title
FROM film
JOIN film_actor ON film.film_id = film_actor.film_id
JOIN actor ON film_actor.actor_id = actor.actor_id
WHERE film_actor.actor_id = (
SELECT actor_id
FROM film_actor
GROUP BY actor_id
ORDER BY COUNT(*) DESC
LIMIT 1
);



-- Find the films rented by the most profitable customer in the Sakila database. You can use the customer and payment tables to find the most profitable customer, i.e., the customer who has made the largest sum of payments.
SELECT customer.first_name,
customer.last_name,
film.title,
COUNT(rental.rental_id) AS times_rented
FROM film
JOIN inventory ON film.film_id = inventory.film_id
JOIN rental ON inventory.inventory_id = rental.inventory_id
JOIN customer ON rental.customer_id = customer.customer_id
WHERE rental.customer_id = (
SELECT customer_id
FROM payment
GROUP BY customer_id
ORDER BY SUM(amount) DESC
LIMIT 1
)
GROUP BY customer.first_name, customer.last_name, film.title
ORDER BY times_rented DESC;


-- Retrieve the client_id and the total_amount_spent of those clients who spent more than the average of the total_amount spent by each client. You can use subqueries to accomplish this.
SELECT payment.customer_id,
SUM(payment.amount) AS total_amount_spent
FROM payment
GROUP BY payment.customer_id
HAVING SUM(payment.amount) > (
SELECT AVG(customer_total)
FROM (
SELECT SUM(amount) AS customer_total
FROM payment
GROUP BY customer_id
) AS per_customer_totals
);