Find product pairs that appear together in at least 2 different orders. An order may list the same product on several lines; it counts once and never pairs with itself. product_1 is the name of the product with the smaller product_id, product_2 the other. pct_of_orders = 100 * orders_together / total number of distinct orders, ROUND to 1. Return product_1, product_2, orders_together, pct_of_orders. Order by orders_together DESC, product_1, product_2.
| Column | Type |
|---|---|
| product_id | INTEGER |
| product_name | TEXT |
| product_id | product_name |
|---|---|
| 1 | Phone Case |
| 2 | Screen Guard |
| 3 | Charger |
| 4 | Earbuds |
| 5 | Power Bank |
| Column | Type |
|---|---|
| order_id | TEXT |
| product_id | INTEGER |
| qty | INTEGER |
| order_id | product_id | qty |
|---|---|---|
| O1 | 1 | 1 |
| O1 | 2 | 1 |
| O1 | 3 | 1 |
| O2 | 1 | 2 |
| O2 | 2 | 1 |
| O3 | 3 | 1 |
| O3 | 4 | 1 |
| O4 | 1 | 1 |
| product_1 | product_2 | orders_together | pct_of_orders |
|---|---|---|---|
| Phone Case | Screen Guard | 3 | 50 |
| Charger | Earbuds | 2 | 33.3 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Forward-Fill Missing FX Rates · Daily Cloud Cost Anomalies ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.