From dc05bfff36bfac10f473c0c8f8a2c7de608bb16a Mon Sep 17 00:00:00 2001 From: aidagarcd Date: Thu, 11 Jan 2024 19:08:19 +0100 Subject: [PATCH 1/2] aida cross joins lab --- cross joins lab.sql | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 cross joins lab.sql diff --git a/cross joins lab.sql b/cross joins lab.sql new file mode 100644 index 0000000..2415caa --- /dev/null +++ b/cross joins lab.sql @@ -0,0 +1,3 @@ +-- 1 Get all pairs of actors that worked together. +-- 2 Get all pairs of customers that have rented the same film more than 3 times. +-- 3 Get all possible pairs of actors and films. \ No newline at end of file From 687897d406152d21eb8f5d90101d8c9b1807e8d3 Mon Sep 17 00:00:00 2001 From: aidagarcd Date: Fri, 12 Jan 2024 21:43:10 +0100 Subject: [PATCH 2/2] cross join lab --- cross joins lab.sql | 55 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/cross joins lab.sql b/cross joins lab.sql index 2415caa..b11f42d 100644 --- a/cross joins lab.sql +++ b/cross joins lab.sql @@ -1,3 +1,56 @@ -- 1 Get all pairs of actors that worked together. -- 2 Get all pairs of customers that have rented the same film more than 3 times. --- 3 Get all possible pairs of actors and films. \ No newline at end of file +-- 3 Get all possible pairs of actors and films. + +use sakila; + +-- 1 +SELECT + CONCAT(a1.first_name, ' ', a1.last_name) AS 'ACTOR 1', + CONCAT(a2.first_name, ' ', a2.last_name) AS 'ACTOR 2' +FROM + sakila.film_actor fa1 +JOIN + sakila.film_actor fa2 ON fa1.film_id = fa2.film_id +JOIN + sakila.actor a1 ON fa1.actor_id = a1.actor_id +JOIN + sakila.actor a2 ON fa2.actor_id = a2.actor_id +WHERE + a1.actor_id < a2.actor_id; + +-- 2 +SELECT + CONCAT(c1.first_name, ' ', c1.last_name) AS 'CUSTOMER 1', + CONCAT(c2.first_name, ' ', c2.last_name) AS 'CUSTOMER 2', + f.title AS 'FILM', + COUNT(*) AS 'RENTAL_COUNT' +FROM + sakila.rental AS r1 +JOIN + sakila.rental AS r2 ON r1.inventory_id = r2.inventory_id AND r1.customer_id < r2.customer_id +JOIN + sakila.customer AS c1 ON r1.customer_id = c1.customer_id +JOIN + sakila.customer AS c2 ON r2.customer_id = c2.customer_id +JOIN + sakila.inventory AS i1 ON r1.inventory_id = i1.inventory_id +JOIN + sakila.inventory AS i2 ON r2.inventory_id = i2.inventory_id +JOIN + sakila.film AS f ON i1.film_id = f.film_id +GROUP BY + 'CUSTOMER 1', 'CUSTOMER 2', 'FILM' +HAVING + COUNT(*) > 3 +LIMIT 0, 1000; + +-- 3 +SELECT + a.first_name AS 'Actor First Name', + a.last_name AS 'Actor Last Name', + f.title AS 'Film Title' +FROM + sakila.actor AS a +CROSS JOIN + sakila.film AS f;