Pizza Toppings Cost Analysis - Problem
You're tasked with creating a comprehensive analysis for a pizza restaurant's menu planning system. Given a table of available toppings and their costs, you need to calculate the total cost of all possible 3-topping pizza combinations.
Database Schema:
| Column Name | Type |
|---|---|
| topping_name | varchar |
| cost | decimal |
Requirements:
- Generate all unique 3-topping combinations (no repeated toppings)
- List toppings in alphabetical order within each combination
- Round total costs to 2 decimal places
- Sort results by total cost descending, then by combination name ascending
Example: If you have toppings [Pepperoni ($2.50), Mushroom ($1.75), Onion ($1.25), Sausage ($3.00)], one valid combination would be 'Mushroom, Onion, Pepperoni' with total cost $5.50.
Input & Output
example_1.sql โ Basic Case
$
Input:
Toppings:
| topping_name | cost |
|-------------|-------|
| Pepperoni | 2.50 |
| Mushroom | 1.75 |
| Onion | 1.25 |
| Sausage | 3.00 |
โบ
Output:
| pizza | total_cost |
|--------------------------------|-----------|
| Onion, Pepperoni, Sausage | 6.75 |
| Mushroom, Pepperoni, Sausage | 7.25 |
| Mushroom, Onion, Sausage | 6.00 |
| Mushroom, Onion, Pepperoni | 5.50 |
๐ก Note:
All possible 3-topping combinations are generated, with toppings listed alphabetically within each combination, sorted by total cost descending.
example_2.sql โ Equal Costs
$
Input:
Toppings:
| topping_name | cost |
|-------------|-------|
| Cheese | 2.00 |
| Tomato | 2.00 |
| Basil | 2.00 |
โบ
Output:
| pizza | total_cost |
|-------------------------|-----------|
| Basil, Cheese, Tomato | 6.00 |
๐ก Note:
With only 3 toppings available, only one combination is possible. All costs are equal, resulting in a total of $6.00.
example_3.sql โ Decimal Precision
$
Input:
Toppings:
| topping_name | cost |
|-------------|-------|
| Anchovies | 1.33 |
| Olives | 2.67 |
| Peppers | 1.50 |
| Spinach | 0.75 |
โบ
Output:
| pizza | total_cost |
|----------------------------|-----------|
| Olives, Peppers, Spinach | 4.92 |
| Anchovies, Olives, Spinach | 4.75 |
| Anchovies, Peppers, Spinach| 3.58 |
| Anchovies, Olives, Peppers | 4.50 |
๐ก Note:
Costs with decimal places are properly rounded to 2 decimal places in the final result.
Visualization
Tap to expand
Understanding the Visualization
1
Setup Table Aliases
Create three references (t1, t2, t3) to the same Toppings table
2
Apply Ordering Constraints
Use WHERE t1.name < t2.name < t3.name to ensure alphabetical order
3
Generate Combinations
Cross join creates all possible triplets, constraints filter valid combinations
4
Calculate and Format
Sum costs, round to 2 decimals, concatenate names, and sort results
Key Takeaway
๐ฏ Key Insight: Using string comparison in SQL WHERE clauses (t1.name < t2.name < t3.name) elegantly ensures both alphabetical ordering and elimination of duplicate combinations in a single operation.
Time & Space Complexity
Time Complexity
O(nยณ)
Three nested loops through all toppings creates cubic time complexity
โ Quadratic Growth
Space Complexity
O(nยณ)
Result set contains C(n,3) combinations, which is O(nยณ) for large n
โ Quadratic Space
Constraints
- At least 3 toppings must be available in the table
- All topping costs are positive decimal values
- Topping names are unique (primary key constraint)
- Results must be ordered by total_cost DESC, pizza ASC
- Total cost must be rounded to exactly 2 decimal places
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code