Skip to content
Advertisement

SQL SELECT transactions for items that two people have both purchased at least once

I have a table of transactions where a each transaction has a transaction_id, an item (what item was purchased in the transaction) and a customer_id (person who ordered the item).

How can I pull all transactions only for items that have been purchased by both customer_id 100 and customer_id 101 at least once? In this case that result would only be the transactions with the item Computer.

Advertisement

Answer

You can use exists if you want only the transactions for those customers:

If you want all transactions, you could use window functions:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement