diff --git a/solutions.sql b/solutions.sql index d0eddcc..bc16474 100644 --- a/solutions.sql +++ b/solutions.sql @@ -1 +1,83 @@ -- Add you solution queries below: + +USE sakila; + +-- 1. How many copies of the film _Hunchback Impossible_ exist in the inventory system? +SELECT COUNT(*) AS num_hunch +FROM inventory +JOIN film ON inventory.film_id = film.film_id +WHERE title = "Hunchback Impossible"; + +-- 2. List all films whose length is longer than the average of all the films. +SELECT title, length +FROM film +WHERE length > ( + SELECT AVG(length) + FROM film + ); + +-- 3. Use subqueries to display all actors who appear in the film _Alone Trip_. + +SELECT first_name, last_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' + ) +); + +-- 4. Sales have been lagging among young families, and you wish to target all family movies for a promotion. Identify all movies categorized as family films. +SELECT f.title +FROM film f +JOIN film_category fc ON f.film_id = fc.film_id +JOIN category c ON fc.category_id = c.category_id +WHERE c.name = 'Family'; + +-- 5. Get name and email from customers from Canada using subqueries. Do the same with joins. Note that to create a join, you will have to identify the correct tables with their primary keys and foreign keys, that will help you get the relevant information. +SELECT first_name, last_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' + ) + ) +); + + +-- 6. Which are films starred by the most prolific actor? Most prolific actor is defined as the actor that has acted in the most number of films. First you will have to find the most prolific actor and then use that actor_id to find the different films that he/she starred. +SELECT actor_id, COUNT(film_id) AS film_count +FROM film_actor +GROUP BY actor_id +ORDER BY film_count DESC +LIMIT 1; + +-- 7. Films rented by most profitable customer. You can use the customer table and payment table to find the most profitable customer ie the customer that has made the largest sum of payments +SELECT customer_id +FROM payment +GROUP BY customer_id +ORDER BY SUM(amount) DESC +LIMIT 1; + +-- 8. Get 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. +SELECT customer_id AS client_id, SUM(amount) AS total_amount_spent +FROM payment +GROUP BY customer_id +HAVING SUM(amount) > ( + SELECT AVG(total_per_customer) + FROM ( + SELECT SUM(amount) AS total_per_customer + FROM payment + GROUP BY customer_id + ) AS sub +);