task Join fan-out & cardinality · hard
Compare category audience overlap
For each alphabetically ordered pair of categories sharing at least one delivered-order shopper, return shared_buyers, union_buyers and jaccard = shared / union rounded to three decimals.
what to return
- Columns
category_acategory_bshared_buyersunion_buyersjaccardin this order- Rows
- 6 rows · in the order asked
- Watch for
- A shopper belongs to a category once, regardless of repeat orders or quantities. Include only pairs with shared buyers.
- Use category_a < category_b. Order by category_a,category_b.
- Skill
- Compute Jaccard overlap without inflating membership by line-item or repeat-order counts. Refresher: Multi-grain joins without fan-out →
expected result
Your query should return exactly these columns and values. Run it and compare.
expected result6 rows · in this order
| category_a | category_b | shared_buyers | union_buyers | jaccard |
|---|---|---|---|---|
| Garden | Kitchen | 1 | 10 | 0.1 |
| Garden | Lighting | 1 | 11 | 0.091 |
| Garden | Office | 1 | 9 | 0.111 |
first 3 of 6 rows
data you can query
Sample rows from each table. Each column shows its type underneath; → marks a link to another table.
shoppers5 columns · 17 rows
| idinteger · key | nametext | emailtext | regiontext | signup_datetext |
|---|---|---|---|---|
| 1 | Ana Ruiz | ana@harbor.test | West | 2025-01-03 |
| 2 | Ben Okafor | ben@harbor.test | East | 2025-01-05 |
| 3 | Chloe Park | chloe@harbor.test | West | 2025-01-09 |
| 4 | Dmitri Volkov | dmitri@harbor.test | North | 2025-01-14 |
| 5 | Esi Mensah | NULL | South | 2025-01-20 |
showing 5 of 17 rows
catalog_items4 columns · 13 rows
| idinteger · key | nametext | categorytext | pricereal |
|---|---|---|---|
| 1 | Desk Lamp | Lighting | 39 |
| 2 | Floor lamp | Lighting | 89 |
| 3 | String Lights | Lighting | 19.5 |
| 4 | Chef Knife | Kitchen | 59 |
| 5 | Cast Iron Pan | Kitchen | 45 |
showing 5 of 13 rows
sales_orders5 columns · 33 rows
| idinteger · key | shopper_idinteger → shoppers | order_datetext | statustext | delivered_datetext |
|---|---|---|---|---|
| 101 | 1 | 2025-01-04 | delivered | 2025-01-08 |
| 102 | 2 | 2025-01-06 | delivered | 2025-01-07 |
| 103 | 1 | 2025-01-15 | delivered | 2025-01-20 |
| 104 | 3 | 2025-01-18 | cancelled | NULL |
| 105 | 4 | 2025-01-22 | delivered | 2025-01-31 |
showing 5 of 33 rows
order_lines4 columns · 48 rows
| order_idinteger → sales_orders | item_idinteger → catalog_items | quantityinteger | unit_pricereal |
|---|---|---|---|
| 101 | 1 | 1 | 39 |
| 101 | 3 | 2 | 19.5 |
| 102 | 4 | 1 | 59 |
| 103 | 5 | 1 | 45 |
| 103 | 10 | 3 | 12 |
showing 5 of 48 rows
solution.sql
Hints and the answer are always available. Using them counts the question as solved with help.