Calculate Product Final Price - Problem

You're building a smart pricing system for an e-commerce platform! ๐Ÿ›’

Given two database tables - Products and Discounts - you need to calculate the final price of each product after applying category-based discounts.

Your mission:

  • ๐Ÿ” Join products with their category discounts
  • ๐Ÿ’ฐ Calculate final price = original price - (discount% ร— original price)
  • ๐Ÿท๏ธ Handle products with no discounts (price remains unchanged)
  • ๐Ÿ“Š Return results ordered by product_id

Tables Structure:

Products: Contains product_id (unique), category, and original price
Discounts: Contains category (primary key) and discount percentage (0-100)

Think of it like a smart cashier that automatically applies the right discount based on what category each item belongs to!

Input & Output

example_1.sql โ€” Basic Scenario
$ Input: Products: [(1,'Electronics',1000), (2,'Clothing',50), (3,'Electronics',1200), (4,'Home',500)] Discounts: [('Electronics',10), ('Clothing',20)]
โ€บ Output: [(1,900,'Electronics'), (2,40,'Clothing'), (3,1080,'Electronics'), (4,500,'Home')]
๐Ÿ’ก Note: Electronics gets 10% off (1000โ†’900, 1200โ†’1080), Clothing gets 20% off (50โ†’40), Home has no discount (500โ†’500)
example_2.sql โ€” No Discounts Available
$ Input: Products: [(1,'Books',25), (2,'Sports',100)] Discounts: []
โ€บ Output: [(1,25,'Books'), (2,100,'Sports')]
๐Ÿ’ก Note: When no discounts exist, all products retain their original prices
example_3.sql โ€” All Products Have Discounts
$ Input: Products: [(1,'Electronics',2000), (2,'Clothing',80)] Discounts: [('Electronics',25), ('Clothing',15)]
โ€บ Output: [(1,1500,'Electronics'), (2,68,'Clothing')]
๐Ÿ’ก Note: All categories have matching discounts: Electronics 25% off (2000โ†’1500), Clothing 15% off (80โ†’68)

Constraints

  • 1 โ‰ค number of products โ‰ค 104
  • 0 โ‰ค number of discount categories โ‰ค 100
  • 1 โ‰ค product_id โ‰ค 106
  • 1 โ‰ค price โ‰ค 106
  • 0 โ‰ค discount โ‰ค 100 (percentage)
  • Category names are non-empty strings with max length 50
  • Each product_id is unique
  • Each discount category appears at most once

Visualization

Tap to expand
๐Ÿ›’ Shopping Cart๐Ÿ“ฑ Electronics $1000๐Ÿ‘• Clothing $50๐Ÿ’ป Electronics $1200๐Ÿ  Home $500๐Ÿท๏ธ Discount RulesElectronics: 10% OFFClothing: 20% OFF๐Ÿงพ Final ReceiptProduct 1: $900 โœ…Product 2: $40 โœ…Product 3: $1080 โœ…Product 4: $500 โž–Check discountsApply & calculate๐Ÿ’ก LEFT JOIN LogicProductsโœ“ Always includedโœ“ Primary data sourceDiscounts? Optional match? NULL if not foundFinal Price = Price ร— (1 - COALESCE(Discount, 0) / 100)COALESCE handles missing discounts by defaulting to 0%๐ŸŽฏ Smart E-commerce Pricing System
Understanding the Visualization
1
Customer's Cart
Products from different categories arrive at checkout
2
Discount Lookup
System checks if each category has an active discount
3
Price Calculation
Applies discount percentage to original price
4
Final Receipt
Shows final prices ordered by product ID
Key Takeaway
๐ŸŽฏ Key Insight: LEFT JOIN is perfect for optional relationships - every product gets processed, but discounts are applied only when available!
Asked in
Amazon 45 Shopify 38 eBay 32 Walmart 28
42.0K Views
High Frequency
~15 min Avg. Time
1.3K 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