Table: Products
+-------------+------+ | Column Name | Type | +-------------+------+ | product_id | int | | price | int | +-------------+------+ product_id contains unique values. Each row in this table shows the ID of a product and the price of one unit.
Table: Purchases
+-------------+------+ | Column Name | Type | +-------------+------+ | invoice_id | int | | product_id | int | | quantity | int | +-------------+------+ (invoice_id, product_id) is the primary key (combination of columns with unique values) for this table. Each row in this table shows the quantity ordered from one product in an invoice.
Write a solution to show the details of the invoice with the highest price. If two or more invoices have the same price, return the details of the one with the smallest invoice_id.
Return the result table in any order.
The result format is shown in the following example.
Example 1:
Input: Products table: +------------+-------+ | product_id | price | +------------+-------+ | 1 | 100 | | 2 | 200 | +------------+-------+ Purchases table: +------------+------------+----------+ | invoice_id | product_id | quantity | +------------+------------+----------+ | 1 | 1 | 2 | | 3 | 2 | 1 | | 2 | 2 | 3 | | 2 | 1 | 4 | | 4 | 1 | 10 | +------------+------------+----------+ Output: +------------+----------+-------+ | product_id | quantity | price | +------------+----------+-------+ | 2 | 3 | 600 | | 1 | 4 | 400 | +------------+----------+-------+ Explanation: Invoice 1: price = (2 * 100) = $200 Invoice 2: price = (4 * 100) + (3 * 200) = $1000 Invoice 3: price = (1 * 200) = $200 Invoice 4: price = (10 * 100) = $1000 The highest price is $1000, and the invoices with the highest prices are 2 and 4. We return the details of the one with the smallest ID, which is invoice 2.
Problem Overview: You need to generate invoice totals from transactional data stored across multiple tables. Each purchase references a product and quantity, and the final invoice requires computing the total cost by multiplying product price with quantity and aggregating results per invoice.
Approach 1: JOIN + Aggregation (O(n) time, O(1) extra space)
The core idea is to reconstruct each invoice line by joining the purchases table with the products table using product_id. Once the price is available, compute the line cost using price * quantity. Then aggregate the values using SUM() grouped by the invoice identifier. SQL engines handle grouping efficiently, so this approach scans the joined dataset once, making the time complexity O(n) where n is the number of purchase records. Space overhead is constant because the database performs aggregation internally without allocating extra data structures.
This approach relies on relational operations: INNER JOIN to connect product pricing with purchases and GROUP BY to consolidate rows belonging to the same invoice. The correctness comes from computing totals after the join so every purchase line contributes exactly one price calculation. If multiple products exist in the same invoice, their contributions accumulate through the aggregation step.
Approach 2: CTE with Precomputed Line Totals (O(n) time, O(n) intermediate space)
A more structured query uses a Common Table Expression (CTE) to first compute line totals for each purchase. The CTE calculates price * quantity after joining product and purchase data. The outer query then aggregates those intermediate rows per invoice using SUM(). Time complexity remains O(n) because every purchase row is processed once, but intermediate results may be materialized depending on the optimizer, giving an effective O(n) temporary space footprint.
This structure improves readability and debugging when queries grow more complex. If additional filters or invoice-level calculations are needed later (such as ranking or selecting the highest invoice total), the intermediate result from the CTE becomes a convenient base dataset.
Conceptually, this problem revolves around relational data processing using joins and aggregations, common patterns in database and SQL interview questions. Understanding when to compute derived columns (like line totals) before aggregation is a key design detail.
Recommended for interviews: The direct JOIN + GROUP BY aggregation is what interviewers expect. It demonstrates you understand relational joins, derived expressions, and grouped aggregation. Writing the query with a CTE shows stronger SQL structuring skills and becomes useful when extending the query with additional logic.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| JOIN + GROUP BY Aggregation | O(n) | O(1) | Standard SQL solution when calculating totals per invoice directly from joined tables |
| CTE with Precomputed Line Totals | O(n) | O(n) | Useful when query readability or additional transformations are required before aggregation |
LeetCode Hard 2362 “Generate the Invoice" Interview SQL Question Explanation | EDS • Everyday Data Science • 1,951 views views
Practice Generate the Invoice with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor