Skip to content

[lab-sql-self-cross-join]Tiago #24

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
34 changes: 34 additions & 0 deletions [lab-sql-self-cross-join]Tiago.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- 1. Get all pairs of actors that worked together.
select fa1.film_id, fa2.actor_id
from sakila.film_actor fa1
inner join sakila.film_actor fa2
on fa1.film_id = fa2.film_id
and fa1.actor_id <> fa2.actor_id
;

-- 2. Get all pairs of customers that have rented the same film more than 3 times.
-- (each pair of customers that rented more than 3 films in comun

select c1.customer_id, c2.customer_id, count(*) as count
from sakila.customer c1
inner join sakila.rental r1 on c1.customer_id = r1.customer_id
inner join sakila.inventory i1 on r1.inventory_id = i1.inventory_id
inner join sakila.film f1 on i1.film_id = f1.film_id
inner join sakila.inventory i2 on f1.film_id = i2.film_id
inner join sakila.rental r2 on i2.inventory_id = r2.inventory_id
inner join sakila.customer c2 on c2.customer_id = r2.customer_id
where c1.customer_id <> c2.customer_id and c1.customer_id < c2.customer_id
group by 1,2
having count > 3
order by 3 desc
;





-- 3. Get all possible pairs of actors and films
select concat(a.first_name, ' ', a.last_name) as name, f.title
from sakila.actor a
cross join sakila.film f
;