fbpx

Relational Division

The next question, despite it’s simple appearance, can be quite a challenge, even for experienced query writers.

The following Sales table represents the sales of a mobile phones store. As you can see, each row represents one purchase, and has two columns: name – stores the customer’s name, and product_name – stores the product that was purchased by this customer.

 

 

Screen Shot 2015-09-07 at 9.01.01 AM

 

Write a query that will retrieve, for a specific customer, all of the other customers that have purchased the exact same products as he has. For example, if we check who bought the same products as John, the result of this query will be Christopher (who is the only customer that has purchased the same products as John has, in addition to another product). If we check who bought the same products as Tim, the result of this query will be Sandra (who is the only customer that has purchased the same products as Tim has, in addition to another product).

UpScale Analytics is one of the largest platforms in the world for learning SQL by doing, consisting over 300 SQL exercises at different levels (including solutions), by topics, across over 100 different datasets. More…