From e75867541b94ceeacaa5268f7805dd6c2ff68390 Mon Sep 17 00:00:00 2001 From: Valeria Acevedo Date: Sat, 16 May 2026 16:18:19 +0200 Subject: [PATCH] lab done --- lab-sql-subqueries .sql | 102 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 lab-sql-subqueries .sql diff --git a/lab-sql-subqueries .sql b/lab-sql-subqueries .sql new file mode 100644 index 0000000..32d207b --- /dev/null +++ b/lab-sql-subqueries .sql @@ -0,0 +1,102 @@ +# Challenge +# Write SQL queries to perform the following tasks using the Sakila database: + +#Determine the number of copies of the film "Hunchback Impossible" that +# exist in the inventory system. + +SELECT COUNT(*) AS total_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 +); + +#Use a subquery to display all actors who appear in the film "Alone Trip". +SELECT first_name +FROM actor +WHERE actor_id IN ( + SELECT actor_id + FROM film_actor + WHERE 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_id IN ( + SELECT film_id + FROM film_category + WHERE category_id = ( + SELECT category_id + FROM category + WHERE 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 title +FROM film +WHERE film_id IN ( + SELECT film_id + FROM film_category + WHERE category_id = ( + SELECT category_id + FROM category + WHERE name = 'Family' + ) +); + +# 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 first_name, email +FROM customer +WHERE address_id IN ( + SELECT address_id + FROM address + WHERE city_id IN ( + SELECT city_id + FROM city + WHERE country_id = ( + SELECT country_id + FROM country + WHERE country = 'Canada' + ) + ) +); + +# 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 title +FROM film +WHERE film_id IN ( + SELECT film_id + FROM film_actor + WHERE actor_id = ( + SELECT actor_id + FROM film_actor + GROUP BY actor_id + ORDER BY COUNT(film_id) DESC + LIMIT 1 + ) +); \ No newline at end of file