with cte1 as (select customer_id,first_name,last_name,film_id,title
from rental
inner join inventory using ( inventory_id )
inner join film using (film_id)
inner join customer using (customer_id)
group by customer_id,film_id
order by customer_id),
cte2 as (select customer_id,first_name,last_name,film_id,title
from rental
inner join inventory using ( inventory_id )
inner join film using (film_id)
inner join customer using (customer_id)
group by customer_id,film_id
order by customer_id)
select concat(cte1.first_name,' ',cte1.last_name) as customer1,
concat(cte2.first_name,' ',cte2.last_name) as customer2 ,
group_concat(cte1.title order by cte1.title) as films
from cte1
inner join cte2 on (cte1.customer_id<cte2.customer_id
and cte1.film_id=cte2.film_id)
group by cte1.first_name,cte1.last_name,cte2.first_name,cte2.last_name
having count(*)>5
order by cte1.first_name,cte1.last_name,cte2.first_name,cte2.last_name
;
show status like 'Last_query_cost';