I have the following tables
customers(custid, name)
orders(orderid, custid)
itemsordered(itemid, orderid)
items(itemid, description)
My objective is to each pair of customers who both ordered an item with the same description, retrieve the name of both customers. Eliminate duplicates, don't pair customers with themselves, and include each pair only once. For each pair, return the names in the pair in alphabetical order.
I understand I need to reference two custid's and compare each of their descriptions to each other, something like:
select nameA
from customers nameA
join orders using (custid)
join itemsordered using (orderid)
join item using (itemid)
where (select nameB
from customers
from customers nameA
join orders using (custid)
join itemsordered using (orderid)
join item using (itemid)
where descriptionA = descriptionB
etc.
but am unsure how to proceed. Here is the correct response:
Christina|Janine
Christina|Max
Christina|Teddy
Christina|David
Christina|Rachel
Rachel|Teddy
David|Janine
David|Rachel
David|Teddy
Janine|Rachel
Janine|Teddy
Janine|Max
Max|Teddy
I need some new examples. Most of my guesses consist of subqueries like the following:
select attribute from table
join anotherTable using (somekey)
where table in (select anotherAttribute
from anotherTable
where....etc.
Any suggestions or direction would be appreciated.
UPDATE: names are unique.