Back to questions
This is the same question as problem #30 in the SQL Chapter of Ace the Data Science Interview!
Assume you are given the following tables on Walmart transactions and products. Find the number of unique product combinations that are bought together (purchased in the same transaction).
For example, if I find two transactions where apples and bananas are bought, and another transaction where bananas and soy milk are bought, my output would be 2 to represent the 2 unique combinations. Your output should be a single number.
There are 4 unique purchase combinations present in the example data.
The dataset you are querying against may have different input & output - this is just an example!
Goal: Find the number of unique combinations purchased in a single transaction.
Let's start small by joining the and tables together based on the related field so that we can see all the product names clearly:
Showing the first 5 rows of output:
We'll create a CTE containing the above query and perform a to fetch products that were purchased together by a single user by joining on .
Note that we want all pairs of the products, but we do not want to overcount the same product. For example, if user A purchased an apple and instant oatmeal in the same transaction, then we only want to count the (apple, instant oatmeal) transaction once, and not also (instant oatmeal, apple).
To avoid double-counting, we can use a condition within the that the of A is less than that of of B.
Note that we used here, but should yield the same result!
Do you see how this technique solves the overcounting problem? To visualize it more clearly, instead of the in the main and you'll see that there are no overcounting issues.