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-mysql-subqueries #22

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

Conversation

sarabenitezinglott
Copy link

No description provided.

@sh-ih
Copy link

sh-ih commented Nov 22, 2023

Good job with your subqueries!

Some comments:
Your query in question 4 is from question 3. The query to check family films is

SELECT film.title 
	FROM film
    INNER JOIN film_category ON film.film_id = film_category.film_id
    WHERE film_category.category_id in (SELECT category_id FROM category WHERE name = "Family");

On question 6, you didn’t need the first and last names. First you needed to create a query to locate the most prolific actor id:

SELECT actor_id
    FROM film_actor
    GROUP BY actor_id
    ORDER BY COUNT(*) DESC
    LIMIT 1;

The result is actor_id 107 (Gina Degeneres)

And the use that as a subquery to list their films:

SELECT * FROM film
JOIN film_actor using (film_id)
WHERE actor_id = ( SELECT actor_id
    FROM film_actor
    GROUP BY actor_id
    ORDER BY COUNT(*) DESC
    LIMIT 1);

Query 7 has a similar logic: get the customer id of the customer that paid the most and use it on your WHERE clause:

SELECT title
FROM film
WHERE film_id IN (SELECT film_id
	FROM inventory
	WHERE inventory_id IN (SELECT inventory_id
		FROM rental
		WHERE customer_id IN (SELECT customer_id
		FROM (SELECT customer_id, SUM(amount) as total
			FROM payment
			GROUP BY customer_id
			ORDER BY total DESC
			LIMIT 1) as customer)));

And on question 8, you needed to calculate the total amount paid for each customer, using that info, calculate the average payment and then use that value to filter the rows of customers that paid more than that:

SELECT customer_id, SUM(amount) as total_amount_spent FROM payment
	GROUP BY customer_id
    HAVING total_amount_spent > (SELECT AVG(total_amount_spent) FROM
		(SELECT customer_id, SUM(amount) as total_amount_spent FROM payment GROUP BY customer_id) AS amount)
	ORDER BY total_amount_spent DESC;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants