Skip to content

[lab-sql-self-cross-join] João #16

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

Open
wants to merge 1 commit into
base: master
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
33 changes: 33 additions & 0 deletions Lab-Sql-Self-Cross-Join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- 1) Getting all pairs of actors that worked together
select distinct a3.actor_id as actor_id_1, a3.first_name as first_name_1, a3.last_name as last_name_1, a4.actor_id as actor_id_2, a4.first_name as first_name_2, a4.last_name as last_name_2
from sakila.film_actor a1
inner join sakila.film_actor a2 on a1.film_id = a2.film_id
left join sakila.actor a3 on a1.actor_id = a3.actor_id
left join sakila.actor a4 on a2.actor_id = a4.actor_id
where a1.actor_id < a2.actor_id
order by a3.actor_id asc, a4.actor_id asc;

-- 2) Get all pairs of customers that have rented the same film more than 3 times
select customer1.first_name as first_name_1, customer1.last_name as last_name_1, customer2.first_name as first_name_2, customer2.last_name as last_name_2, count(*) as rentals_of_the_same_film
from(
select distinct customer_id, film_id
from sakila.rental rental
left join sakila.inventory inventory on rental.inventory_id = inventory.inventory_id
) sub1
left join (
select distinct customer_id, film_id
from sakila.rental rental
left join sakila.inventory inventory on rental.inventory_id = inventory.inventory_id
) sub2 on sub1.film_id = sub2.film_id
left join sakila.customer customer1 on sub1.customer_id = customer1.customer_id
left join sakila.customer customer2 on sub2.customer_id = customer2.customer_id
where sub1.customer_id < sub2.customer_id
group by customer1.first_name, customer1.last_name, customer2.first_name, customer2.last_name
having count(*) > 3
order by count(*) desc;

-- 3) Getting all possible pairs of actors and films
select distinct film.title, actor_name.first_name as actor_first_name, actor_name.last_name as actor_last_name
from sakila.film_actor actor
cross join sakila.film film
left join sakila.actor actor_name on actor.actor_id = actor_name.actor_id;