Product's Worth Over Invoices - Problem

Imagine you're working as a financial analyst for an e-commerce company that needs to analyze their invoice data across all products. You have access to two important databases:

  • Product Database: Contains all products with their unique IDs and names
  • Invoice Database: Contains detailed financial information for each invoice including amounts that are still owed, paid, canceled, or refunded

Your task is to create a comprehensive financial report that shows, for each product, the total amounts across all four financial categories from all invoices associated with that product.

Goal: Generate a summary report showing each product's name along with the total amounts due (rest), paid (paid), canceled (canceled), and refunded (refunded) across all invoices.

Input: Two tables - Product table with product information and Invoice table with financial transaction details.

Output: A result set with product names and their aggregated financial totals, ordered alphabetically by product name.

Input & Output

basic_example.sql โ€” Basic Case
$ Input: Product table: +------------+--------+ | product_id | name | +------------+--------+ | 0 | laptop | | 1 | mouse | +------------+--------+ Invoice table: +------------+------------+------+------+----------+----------+ | invoice_id | product_id | rest | paid | canceled | refunded | +------------+------------+------+------+----------+----------+ | 23 | 0 | 2 | 1500 | 25 | 0 | | 12 | 0 | 0 | 1000 | 0 | 15 | | 1 | 1 | 5 | 3000 | 0 | 0 | +------------+------------+------+------+----------+----------+
โ€บ Output: +--------+------+------+----------+----------+ | name | rest | paid | canceled | refunded | +--------+------+------+----------+----------+ | laptop | 2 | 2500 | 25 | 15 | | mouse | 5 | 3000 | 0 | 0 | +--------+------+------+----------+----------+
๐Ÿ’ก Note: For laptop: rest=2+0=2, paid=1500+1000=2500, canceled=25+0=25, refunded=0+15=15. For mouse: rest=5, paid=3000, canceled=0, refunded=0. Results are ordered alphabetically by product name.
no_invoices.sql โ€” Product Without Invoices
$ Input: Product table: +------------+--------+ | product_id | name | +------------+--------+ | 0 | laptop | | 1 | mouse | | 2 | screen | +------------+--------+ Invoice table: +------------+------------+------+------+----------+----------+ | invoice_id | product_id | rest | paid | canceled | refunded | +------------+------------+------+------+----------+----------+ | 23 | 0 | 10 | 1000 | 0 | 0 | | 12 | 1 | 0 | 500 | 0 | 0 | +------------+------------+------+------+----------+----------+
โ€บ Output: +--------+------+------+----------+----------+ | name | rest | paid | canceled | refunded | +--------+------+------+----------+----------+ | laptop | 10 | 1000 | 0 | 0 | | mouse | 0 | 500 | 0 | 0 | | screen | 0 | 0 | 0 | 0 | +--------+------+------+----------+----------+
๐Ÿ’ก Note: The screen product has no invoices, so all its financial metrics are 0. The LEFT JOIN ensures all products appear in the result, and COALESCE handles NULL values by converting them to 0.
single_product.sql โ€” Edge Case
$ Input: Product table: +------------+--------+ | product_id | name | +------------+--------+ | 1 | tablet | +------------+--------+ Invoice table: +------------+------------+------+------+----------+----------+ | invoice_id | product_id | rest | paid | canceled | refunded | +------------+------------+------+------+----------+----------+ | 1 | 1 | 100 | 200 | 50 | 25 | +------------+------------+------+------+----------+----------+
โ€บ Output: +--------+------+------+----------+----------+ | name | rest | paid | canceled | refunded | +--------+------+------+----------+----------+ | tablet | 100 | 200 | 50 | 25 | +--------+------+------+----------+----------+
๐Ÿ’ก Note: Simple case with one product and one invoice. All values are taken directly from the single invoice record.

Constraints

  • 1 โ‰ค number of products โ‰ค 104
  • 0 โ‰ค number of invoices โ‰ค 105
  • Product names contain only lowercase English letters
  • All financial amounts are non-negative integers
  • Each product_id in Product table is unique
  • Each invoice_id in Invoice table is unique

Visualization

Tap to expand
Products1. laptop2. mouse3. screenInvoiceslaptop: $100, $50mouse: $200screen: (none)JOIN Resultlaptop + $100laptop + $50mouse + $200screen + NULLFinal Resultlaptop: $150mouse: $200screen: $0LEFT JOINGROUP BYSUMKey: LEFT JOIN preserves all productsKey: GROUP BY aggregates per productKey: COALESCE handles NULLs
Understanding the Visualization
1
Start with Products
Begin with all products in your catalog
2
LEFT JOIN Invoices
Match each product with its invoices (keeping products without invoices)
3
GROUP BY Product
Group all invoice records by product name
4
SUM Financial Metrics
Calculate totals for rest, paid, canceled, and refunded amounts
5
Handle NULLs
Use COALESCE to convert NULL sums to 0 for products without invoices
6
ORDER Results
Sort the final results alphabetically by product name
Key Takeaway
๐ŸŽฏ Key Insight: LEFT JOIN ensures all products appear in results, GROUP BY aggregates invoice data per product, and COALESCE converts NULL sums to 0 for products without invoices - all in one efficient query.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
42.0K Views
High Frequency
~15 min Avg. Time
1.9K 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