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 NameType
topping_namevarchar
costdecimal

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
Original Toppings TableMushroom: $1.75Onion: $1.25Pepperoni: $2.50Sausage: $3.00t1 (First)MushroomOnionPepperonit2 (Second)OnionPepperoniSausaget3 (Third)PepperoniSausageSausageFinal Pizza CombinationsOnion, Pepperoni, Sausage - $6.75Mushroom, Pepperoni, Sausage - $7.25Mushroom, Onion, Sausage - $6.00Mushroom, Onion, Pepperoni - $5.50Self-JoinCombineWHERE t1.name < t2.name < t3.nameEnsures alphabetical order and no duplicates
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

n
2n
โš  Quadratic Growth
Space Complexity
O(nยณ)

Result set contains C(n,3) combinations, which is O(nยณ) for large n

n
2n
โš  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
Asked in
DoorDash 35 Uber 28 Grubhub 22 Amazon 18
24.6K Views
Medium Frequency
~15 min Avg. Time
890 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