Products bought together in the same order
The task
Merchandising wants to place related products next to each other. Find pairs of products that customers often put in the same basket.
Rules:
- Look only at
completedorders. - A pair is two different products that appear on the same order. Each pair appears once:
product_ais the product with the lowerproduct_id, andproduct_bthe higher one. Never pair a product with itself. orders_togetheris the number of completed orders that contain both products.- Keep pairs with at least 5 such orders.
- Products are told apart by
product_id. Two products can share a name, and they are still two products.
Return product_a and product_b (the product names) and orders_together. Sort by orders_together descending, then product_a, then product_b.
Expected output
product_aproduct_borders_together- Sorted by orders_together descending, then product_a ascending, then product_b ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Online store · click a name to insert it
customers150 rows in the sample
One row per customer account. Key: customer_id.
| int | Unique id. | |
| text | Name on the account. | |
| text | NULL when the customer never gave one (marketplace buyers). | |
| text | Two-letter country code. | |
| date | Day the account was created. | |
| text | organic, paid_search, social, referral or email. NULL when unknown. |
products36 rows in the sample
The catalog. Key: product_id.
| int | Unique id. | |
| text | Display name. | |
| text | Kitchen, Home, Outdoors, Books, Beauty or Toys. | |
| numeric(10,2) | Current list price in USD. | |
| date | First day on sale. |
orders700 rows in the sample
One row per order. Key: order_id.
| int | Unique id. | |
| int | Who placed it. | |
| timestamp | When it was placed (UTC). | |
| text | completed, cancelled or refunded. | |
| text | web, app or marketplace. | |
| numeric(10,2) | Shipping charged, USD. | |
| text | Coupon used, NULL if none. |
order_items1,266 rows in the sample
One row per product in an order. Key: order_id, product_id.
| int | The order. | |
| int | The product. | |
| int | Units bought. | |
| numeric(10,2) | Price paid per unit, after discounts, USD. |
returns79 rows in the sample
Items sent back. Key: return_id.
| int | Unique id. | |
| int | The original order. | |
| int | The product returned. | |
| date | Day the return arrived. | |
| int | Units returned. | |
| text | damaged, wrong_item, not_as_described or changed_mind. NULL when not given. |
Related questions
- Skip-level managers and how many people they covereasy
- Colleagues hired in the same month and departmenteasy
- Trips between a rider and a driver they already knowmedium
- Employees paid more than their managermedium
- How many drivers in the same city rate higherhard
- Products that sold but were never returnedeasy
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL