Skip to content

[lab-sql-self-cross-join] Fabiana Ferraz #28

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
76 changes: 76 additions & 0 deletions lab-sql-self-cross-join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
-- Get all pairs of actors that worked together.
SELECT
CONCAT(A.first_name, ' ', A.last_name) AS actor1,
CONCAT(B.first_name, ' ', B.last_name) AS actor2,
A.title
FROM
(SELECT
fa.actor_id,
a.first_name,
a.last_name,
fa.film_id,
f.title
FROM sakila.actor a
INNER JOIN sakila.film_actor fa ON a.actor_id = fa.actor_id
INNER JOIN sakila.film f ON fa.film_id = f.film_id
) AS A
INNER JOIN
(SELECT
fa.actor_id,
a.first_name,
a.last_name,
fa.film_id,
f.title
FROM sakila.actor a
INNER JOIN sakila.film_actor fa ON a.actor_id = fa.actor_id
INNER JOIN sakila.film f ON fa.film_id = f.film_id
) AS B
ON A.film_id = B.film_id
AND A.actor_id < B.actor_id
ORDER BY A.title;

-- Get all pairs of customers that have rented the same film more than 3 times.
SELECT
A.title as film_title,
CONCAT(A.first_name, ' ', A.last_name) AS customer1,
CONCAT(B.first_name, ' ', B.last_name) AS customer2,
COUNT(*) AS rental_count
FROM
(SELECT
c.customer_id,
c.first_name,
c.last_name,
r.inventory_id,
f.title
FROM sakila.customer c
INNER JOIN sakila.rental r ON c.customer_id = r.customer_id
INNER JOIN sakila.inventory inv ON r.inventory_id = inv.inventory_id
INNER JOIN sakila.film f ON inv.film_id = f.film_id
) AS A
INNER JOIN
(SELECT
c.customer_id,
c.first_name,
c.last_name,
r.inventory_id,
f.title
FROM sakila.customer c
INNER JOIN sakila.rental r ON c.customer_id = r.customer_id
INNER JOIN sakila.inventory inv ON r.inventory_id = inv.inventory_id
INNER JOIN sakila.film f ON inv.film_id = f.film_id
) AS B
ON A.title = B.title
AND A.customer_id < B.customer_id
GROUP BY 1,2,3
HAVING rental_count > 3
ORDER BY rental_count;

-- Get all possible pairs of actors and films.
SELECT
a.actor_id AS actor_id,
concat (a.first_name, ' ', a.last_name) AS actor_name,
f.film_id AS film_id,
f.title AS film_title
FROM sakila.actor a
CROSS JOIN sakila.film f
ORDER BY actor_id, film_id;