Pizza Toppings Cost Analysis - Problem

You are given a table Toppings that contains information about pizza toppings and their costs.

Table: Toppings

Column NameType
topping_namevarchar
costdecimal

topping_name is the primary key for this table. Each row contains a topping name and its cost.

Problem:

  • Calculate the total cost of all possible 3-topping pizza combinations
  • Round the total cost to 2 decimal places
  • Do not include combinations where a topping is repeated
  • List toppings in alphabetical order within each combination
  • Order results by total cost descending, then by topping combination ascending

Table Schema

Toppings
Column Name Type Description
topping_name PK varchar Name of the pizza topping (primary key)
cost decimal Cost of the topping in dollars
Primary Key: topping_name
Note: Each row represents a unique pizza topping with its associated cost

Input & Output

Example 1 — Basic Pizza Combinations
Input Table:
topping_name cost
Beef 1.2
Chicken 1.3
Cheese 0.9
Pepperoni 1.5
Output:
pizza total_cost
Chicken,Cheese,Pepperoni 3.7
Beef,Chicken,Pepperoni 4
Beef,Cheese,Pepperoni 3.6
Beef,Chicken,Cheese 3.4
💡 Note:

With 4 toppings, we can create C(4,3) = 4 unique combinations. Each combination lists toppings alphabetically and shows the sum of their costs rounded to 2 decimal places. Results are ordered by total cost descending, then by pizza name ascending.

Example 2 — Minimal Case
Input Table:
topping_name cost
Cheese 1
Pepperoni 2
Mushroom 1.5
Output:
pizza total_cost
Cheese,Pepperoni,Mushroom 4.5
💡 Note:

With exactly 3 toppings, there is only one possible combination. The toppings are listed alphabetically (Cheese, Mushroom, Pepperoni becomes Cheese,Pepperoni,Mushroom) with total cost 4.50.

Example 3 — No Valid Combinations
Input Table:
topping_name cost
Cheese 1
Pepperoni 2
Output:
pizza total_cost
💡 Note:

With only 2 toppings available, it's impossible to create any 3-topping combinations. The result is an empty set.

Constraints

  • 1 ≤ number of toppings ≤ 1000
  • topping_name consists of letters and spaces only
  • 0.01 ≤ cost ≤ 100.00
  • cost has at most 2 decimal places

Visualization

Tap to expand
Pizza Toppings Cost Analysis OverviewToppings Tabletopping_namecostBeef1.20Chicken1.30Cheese0.90Pepperoni1.50SELFJOIN3-Topping Combinationspizzatotal_costBeef,Chicken,Pepperoni4.00Beef,Cheese,Pepperoni3.60Beef,Chicken,Cheese3.40Chicken,Cheese,Pepperoni3.704 toppings availableC(4,3) = 4 combinationsKey: t1.topping_name < t2.topping_name < t3.topping_name ensures unique, alphabetical combinations
Understanding the Visualization
1
Input
Toppings table with names and costs
2
Self-Join
Triple self-join with ordering constraints
3
Output
All unique 3-topping combinations with costs
Key Takeaway
🎯 Key Insight: Use self-joins with proper ordering constraints to generate unique combinations systematically
Asked in
Amazon 28 Microsoft 22 Google 19
28.5K Views
Medium Frequency
~18 min Avg. Time
847 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