Frequently Bought Together Pairs

SQL practice question · Hard · Joins · Amazon

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.

Table: products

Schema of products
ColumnType
product_idINTEGER
product_nameTEXT
Sample rows of products
product_idproduct_name
1Phone Case
2Screen Guard
3Charger
4Earbuds
5Power Bank

Table: order_lines

Schema of order_lines
ColumnType
order_idTEXT
product_idINTEGER
qtyINTEGER
Sample rows of order_lines
order_idproduct_idqty
O111
O121
O131
O212
O221
O331
O341
O411
Expected output on the sample data
product_1product_2orders_togetherpct_of_orders
Phone CaseScreen Guard350
ChargerEarbuds233.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 ›

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Question Asked in Interview Blog
SQL Interview Practice

Company-wise SQL questions

{{ 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.

🏆 Weekly SQL Contest
{{ totalCount }}
Questions
{{ companyCount }}
Companies
{{ prog.total.solved }}
Solved
Bookmark Question Companies Difficulty Category Status
{{ r.n }} - {{ r.title }} {{ c }} {{ r.difficulty }} {{ r.category }} {{ r.statusIcon }}
No questions match these filters.
Showing {{ rangeLabel }} of {{ filteredCount }}

Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.

← All questions

{{ cur.n }} - {{ cur.title }}

🏆 Contest {{ contestWeek }}
{{ cur.catLabel }} {{ cur.diffLabel }} {{ c }}
Embed this questionPaste on a blog, course page or LMS. Free to use — educator guide
{{ embedCode }}

{{ cur.statement }}

Table: {{ tb.name }}
ColumnType {{ c.name }}{{ c.type }}
{{ h }} {{ v.text }}
Expected output on the sample data:
{{ h }} {{ v.text }}
  • {{ h }}

No submissions yet. Press Evaluate to submit your query.

{{ s.verdict }} {{ s.meta }}
The solution unlocks after you solve this question or after 3 evaluations that didn't pass ({{ failCount }}/3 so far). Trying first is the fastest way to learn.
{{ solutionSql }}
  1. {{ e }}
⏱ {{ timerLabel }}
{{ fnHelpTitle }}
{{ fnHelpText }}
Writing {{ dialectName }} · Ctrl/Cmd + Enter runs the query · Tab indents · your code is saved automatically
Output {{ out.status }}
  • {{ w }}
{{ out.verdict }}
{{ out.detail }}
{{ out.error }}
Expected (sample data)
{{ h }}{{ v.text }}
Your output (sample data)
{{ h }}{{ v.text }}
{{ h }} {{ v.text }}
Run your query to see the result here.
🔒

To Track Your Progress, Log In / Sign Up

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.

Sign Up Free Log In