Skip to content

lab-sql-self-cross-join[Haissa] #33

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
25 changes: 25 additions & 0 deletions lab-sql-self-cross-join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Get all pairs of actors that worked together.
SELECT A1.ACTOR_ID AS FIRST_ACTOR, A2.ACTOR_ID AS SECOND_ACTOR, A1.FILM_ID
FROM SAKILA.FILM_ACTOR A1
LEFT JOIN SAKILA.FILM_ACTOR A2
ON A1.FILM_ID = A2.FILM_ID
AND A1.ACTOR_ID <> A2.ACTOR_ID;

-- Get all pairs of customers that have rented the same film more than 3 times.
SELECT *
FROM SAKILA.RENTAL;

SELECT R1.CUSTOMER_ID AS CUSTOMER_1, R2.CUSTOMER_ID AS CUSTOMER_2, COUNT(*) AS RENTED
FROM SAKILA.RENTAL R1
JOIN SAKILA.RENTAL R2
ON R1.INVENTORY_ID = R2.INVENTORY_ID
AND R1.CUSTOMER_ID <> R2.CUSTOMER_ID
GROUP BY 1, 2
HAVING COUNT(*) > 3;

-- Get all possible pairs of actors and films.
SELECT FA.ACTOR_ID, F.FILM_ID, F.TITLE
FROM SAKILA.FILM_ACTOR FA
LEFT JOIN SAKILA.FILM F
ON FA.FILM_ID = F.FILM_ID
ORDER BY 1,2;