Skip to content

[lab-sql-self-cross-join]Giuliana #22

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
30 changes: 30 additions & 0 deletions [lab-sql-self-cross-join]Giuliana.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- Get all pairs of actors that worked together.
select f1.actor_id as Actor_1, f2.actor_id as Actor_2, f1.film_id
from sakila.film_actor f1
inner join sakila.film_actor f2
on f1.film_id = f2.film_id
and f2.actor_id <> f1.actor_id
order by f1.film_id;

-- Get all pairs of customers that have rented the same film more than 3 times.
select distinct c.customer_id as Customer_1, c2.customer_id as Customer_2, count(*) as same_films
from sakila.customer c
inner join sakila.rental r
on c.customer_id = r.customer_id
inner join sakila.inventory i
on r.inventory_id = i.inventory_id
inner join sakila.inventory i2
on i.film_id = i2.film_id
inner join sakila.rental r2
on r2.inventory_id = i2.inventory_id
inner join sakila.customer c2
on c2.customer_id = r2.customer_id
and c.customer_id <> c2.customer_id
group by 1,2
having same_films > 3
order by same_films desc;


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