Generate the Invoice - Problem
Generate the Invoice - Find the Most Expensive Invoice

You're working for a billing system that needs to identify the highest-value invoice from purchase records. Given two tables:

Products table contains product information:
product_id: Unique identifier for each product
price: Price per unit of the product

Purchases table contains invoice details:
invoice_id: Invoice identifier
product_id: Product being purchased
quantity: Number of units purchased

Goal: Find the invoice with the highest total value (sum of price × quantity for all products in that invoice). If multiple invoices have the same highest value, return the one with the smallest invoice_id.

Return all purchase details for the winning invoice, including product information and calculated totals.

Input & Output

example_1.sql — Basic Invoice Selection
$ Input: Products: [(1,10), (2,15), (3,20)] Purchases: [(101,1,5), (101,2,2), (102,1,7), (102,3,1)]
Output: invoice_id=101, product_id=1, price=10, quantity=5, total=50 invoice_id=101, product_id=2, price=15, quantity=2, total=30
💡 Note: Invoice 101 total: (10×5 + 15×2) = $80. Invoice 102 total: (10×7 + 20×1) = $90. Invoice 102 has higher total, so return all its details.
example_2.sql — Tie-Breaking by Invoice ID
$ Input: Products: [(1,25), (2,30)] Purchases: [(201,1,2), (202,2,2), (203,1,1)]
Output: invoice_id=201, product_id=1, price=25, quantity=2, total=50
💡 Note: Invoice 201: $50, Invoice 202: $60, Invoice 203: $25. Invoice 202 has the highest total ($60), so return its details.
example_3.sql — Equal Totals Edge Case
$ Input: Products: [(1,10), (2,5)] Purchases: [(301,1,4), (302,2,8), (303,1,2)]
Output: invoice_id=301, product_id=1, price=10, quantity=4, total=40
💡 Note: Invoice 301: $40, Invoice 302: $40, Invoice 303: $20. Both 301 and 302 have equal highest totals ($40), but 301 has smaller ID, so return invoice 301 details.

Constraints

  • 1 ≤ number of products ≤ 104
  • 1 ≤ number of purchases ≤ 105
  • 1 ≤ product_id ≤ 106
  • 1 ≤ price ≤ 103
  • 1 ≤ quantity ≤ 102
  • Each invoice_id will have at least one purchase
  • All product_ids in Purchases table exist in Products table

Visualization

Tap to expand
📋 Raw DataProducts + PurchasesMultiple Tables🧮 CalculatePrice × QuantitySum per Invoice🏆 Find MaxHighest TotalBreak Ties by ID📄 ResultWinner's DetailsAll Line Items💡 Example ProcessingInvoice 101: (10×5) + (15×2) = $80Invoice 102: (10×7) + (20×1) = $90Winner: Invoice 102 ($90 > $80)Return all details for Invoice 102
Understanding the Visualization
1
Collect Receipt Data
Gather all purchase records with product prices from both tables
2
Calculate Totals
For each invoice, sum up all line items (price × quantity)
3
Find the Winner
Identify the invoice with highest total, using invoice_id for tie-breaking
4
Return Details
Fetch and format all line items for the winning invoice
Key Takeaway
🎯 Key Insight: Use CTEs to break down the problem into logical steps - calculate totals, find the maximum, then retrieve details. This approach is both efficient and easy to understand.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
48.5K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen