Skip to content
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
20 changes: 20 additions & 0 deletions Lab SQL Self and cross join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- 1. Get all pairs of actors that worked together.
SELECT FA1.ACTOR_ID AS ACTOR_ID_FIRST, FA2.ACTOR_ID AS ACTOR_ID_SECOND, FA1.FILM_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;

-- Get all pairs of customers that have rented the same film more than 3 times.
SELECT R1.CUSTOMER_ID AS CUSTOMER1, R2.CUSTOMER_ID AS CUSTOMER2, COUNT(*) AS RENTAL_COUNT
FROM SAKILA.RENTAL R1
JOIN SAKILA.RENTAL R2
ON R1.INVENTORY_ID = R2.INVENTORY_ID
AND R1.CUSTOMER_ID <> R2.CUSTOMER_ID
GROUP BY CUSTOMER1, CUSTOMER2
HAVING COUNT(*) > 3;

-- Get all possible pairs of actors and films.
SELECT Film_actor.ACTOR_ID, Film.FILM_ID, Film.TITLE
FROM SAKILA.FILM_ACTOR Film_actor
CROSS JOIN SAKILA.FILM Film;