← Back to SQL, page 25

sql-0490 · Medium mid

Design the database for a restaurant menu where dishes have optional modifiers, such as size, extra toppings and spice level, with price changes.

Model dishes, modifier groups (size, toppings) attached to dishes, and options within each group, with a price delta and rules such as minimum and maximum selections. An order line stores the dish and a child table stores the chosen options, copying prices at order time so later menu changes do not alter past orders. Availability and prices by outlet go in separate tables keyed by outlet.

Full answer

Illustrative design (example SQL, not run here):

sql
CREATE TABLE dishes (id bigserial PRIMARY KEY, name text NOT NULL, base_price numeric(8,2) NOT NULL);
CREATE TABLE modifier_groups (
  id bigserial PRIMARY KEY, name text NOT NULL,
  min_select int NOT NULL DEFAULT 0, max_select int NOT NULL DEFAULT 1
);
CREATE TABLE modifier_options (
  id bigserial PRIMARY KEY, group_id bigint NOT NULL REFERENCES modifier_groups(id),
  name text NOT NULL, price_delta numeric(8,2) NOT NULL DEFAULT 0
);
CREATE TABLE dish_modifier_groups (
  dish_id bigint REFERENCES dishes(id), group_id bigint REFERENCES modifier_groups(id),
  PRIMARY KEY (dish_id, group_id)
);
CREATE TABLE order_lines (id bigserial PRIMARY KEY, order_id bigint NOT NULL, dish_id bigint NOT NULL,
                          qty int NOT NULL, unit_price numeric(8,2) NOT NULL);
CREATE TABLE order_line_options (
  order_line_id bigint REFERENCES order_lines(id), option_id bigint REFERENCES modifier_options(id),
  price_delta numeric(8,2) NOT NULL,
  PRIMARY KEY (order_line_id, option_id)
);

Points that interviewers look for:

  • Groups are reusable: a 'Size' group can attach to many dishes through the link table.
  • min_select and max_select express rules such as 'choose exactly one size' (1 and 1) or 'up to three toppings' (0 and 3). The database cannot easily check these per order, so the order service validates them, ideally inside the same transaction that inserts the lines.
  • Prices are copied into order_lines.unit_price and order_line_options.price_delta, because menu prices change and old invoices must not.
  • Per-outlet prices and availability: outlet_dish(outlet_id, dish_id, price, available), with the base price as a fallback.
  • Price history for the menu itself uses validity dates (valid_from, valid_to) when future price changes are scheduled.

Key points

  • Reusable modifier groups linked to dishes
  • Options carry price deltas
  • Min and max selections per group
  • Copy prices onto order lines
  • Per-outlet prices and availability

Common mistakes

  • Referencing live menu prices from old orders
  • Storing chosen modifiers as free text

Follow-ups

  • How would you report the most popular topping per dish?
  • How would you schedule a price rise for next month?

Tips

  • Mention price copying on orders