← Back to SQL, page 11

sql-0204 · Medium mid

How would you store the price of a product so that past orders still show what the customer actually paid?

Copy the unit price onto each order line at purchase time, and keep the current price on the product. Optionally keep a price history table with valid_from and valid_to dates for analysis. Joining old orders to the current product price would rewrite history whenever the price changes.

Full answer
sql
CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price INTEGER);
CREATE TABLE order_lines (order_id INTEGER, product_id INTEGER, qty INTEGER, unit_price INTEGER);
INSERT INTO products VALUES (1, 'Notebook', 4);
INSERT INTO order_lines VALUES (1, 1, 2, 4);
UPDATE products SET price = 5 WHERE id = 1;
SELECT ol.order_id, ol.unit_price AS paid, p.price AS current_price FROM order_lines ol JOIN products p ON p.id = ol.product_id;
order_idpaidcurrent_price
145

The product now costs 5, yet order 1 still shows 4, because the line stored the price it was sold at. A report that joined to products.price would claim the customer paid 5.

A price history table answers "what was the list price on a given date?":

sql
CREATE TABLE price_history (product_id INTEGER, price INTEGER, valid_from TEXT, valid_to TEXT);
INSERT INTO price_history VALUES (1, 4, '2026-01-01', '2026-03-01'), (1, 5, '2026-03-01', NULL);
SELECT price FROM price_history
WHERE product_id = 1 AND valid_from <= '2026-02-15' AND (valid_to IS NULL OR valid_to > '2026-02-15');
price
4

The same pattern applies to tax rates, currency conversion rates, addresses and discounts: anything that can change after the transaction and must be reported as it was.

sql
CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price INTEGER);
CREATE TABLE order_lines (order_id INTEGER, product_id INTEGER, qty INTEGER, unit_price INTEGER);
INSERT INTO products VALUES (1, 'Notebook', 4);
INSERT INTO order_lines VALUES (1, 1, 2, 4);
UPDATE products SET price = 5 WHERE id = 1;
SELECT ol.order_id, ol.unit_price AS paid, p.price AS current_price FROM order_lines ol JOIN products p ON p.id = ol.product_id;

CREATE TABLE price_history (product_id INTEGER, price INTEGER, valid_from TEXT, valid_to TEXT);
INSERT INTO price_history VALUES (1, 4, '2026-01-01', '2026-03-01'), (1, 5, '2026-03-01', NULL);
SELECT price FROM price_history
WHERE product_id = 1 AND valid_from <= '2026-02-15' AND (valid_to IS NULL OR valid_to > '2026-02-15');

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Store the price paid on the order line
  • Keep the current price on the product
  • A history table with validity dates supports as-of queries
  • Applies to taxes, rates and addresses too

Common mistakes

  • Joining historical orders to current prices
  • Overlapping validity ranges in the history table

Follow-ups

  • How would you prevent overlapping price periods?
  • How would you show revenue at list price versus paid price?

Tips

  • Call it a snapshot, not duplication