Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

Product prices change over time and invoices must show the price that applied on any past date. Design the schema and the query for 'price of product 42 on 2026-03-15'.

Store prices as rows of prices(product_id, price, currency, valid_from, valid_to) where valid_to is exclusive and null for the current price, and find the price on a date with WHERE product_id = 42 AND valid_from <= DATE '2026-03-15' AND (valid_to IS NULL OR valid_to > DATE '2026-03-15'). An exclusion constraint over daterange(valid_from, valid_to) prevents overlapping periods in PostgreSQL, an index on (product_id, valid_from) serves the lookup, and the invoice line still stores the price it applied, because the invoice is a legal record.

Why this question is asked

Prices are the textbook case of data whose value depends on when you ask, and getting them wrong shows up directly in invoices, refunds and disputes. Interviewers ask this design question to check that we can model effective dates, write the lookup without off-by-one errors at the boundaries, protect the data from overlapping periods, and know why the invoice should not rely on the price table at all. In this answer, we will design the table, write the 'price on a date' query, prevent overlaps, index the lookup, show how a price change is applied, and explain why invoices keep their own copy.

1Designing the Price Table

Each row says: from valid_from until valid_to, this product cost this much in this currency. We use dates because prices here change per day, and a half-open period, inclusive start and exclusive end, so that one period ends on exactly the day the next begins.

SQL
CREATE TABLE prices (
product_id bigint NOT NULL REFERENCES products(id),
price numeric(12,2) NOT NULL CHECK (price >= 0),
currency char(3) NOT NULL,
valid_from date NOT NULL,
valid_to date, -- exclusive; null = current price
CHECK (valid_to IS NULL OR valid_to > valid_from),
PRIMARY KEY (product_id, currency, valid_from)
);
INSERT INTO prices VALUES
(42, 19.90, 'EUR', DATE '2025-01-01', DATE '2026-02-01'),
(42, 21.50, 'EUR', DATE '2026-02-01', DATE '2026-04-01'),
(42, 22.90, 'EUR', DATE '2026-04-01', NULL);

The row with valid_to null is the current price. The exclusive end means 21.50 applies on 2026-03-31 and 22.90 from 2026-04-01, with no day covered twice and no gap. If a product can be sold in several currencies, currency is part of the key and of every lookup.

2Finding the Price on 2026-03-15

The price that applied on a date is the row whose period contains that date: it started on or before the date, and either has not ended or ends after the date.

Java
SELECT price, currency
FROM prices
WHERE product_id = 42
AND currency = 'EUR'
AND valid_from <= DATE '2026-03-15'
AND (valid_to IS NULL OR valid_to > DATE '2026-03-15');
-- 21.50 | EUR
-- Boundary check: the day a new price starts
-- ... DATE '2026-04-01' -> 22.90 (valid_to > date is false for the 21.50 row)
Which row applies on which date
Pricevalid_fromvalid_toApplies on 2026-03-15?
19.902025-01-012026-02-01No, ended before
21.502026-02-012026-04-01Yes
22.902026-04-01null (current)No, starts later

Notice the two different comparisons: <= for the inclusive start and > for the exclusive end. Mixing them up, for example valid_to >= date, would return two rows on 2026-04-01. An equivalent and slightly more readable form in PostgreSQL is WHERE daterange(valid_from, valid_to) @> DATE '2026-03-15', where @> means 'contains' and a null upper bound means 'no end'.

3Preventing Overlapping Periods

The query assumes at most one row matches. If a bug inserts a second open-ended row, or two periods overlap, the query returns two prices and the invoice code has to guess. PostgreSQL can enforce the rule with an exclusion constraint, which forbids two rows that are equal on some columns and overlap on a range.

SQL
CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE prices ADD CONSTRAINT prices_no_overlap
EXCLUDE USING gist (
product_id WITH =,
currency WITH =,
daterange(valid_from, valid_to, '[)') WITH &&
);
INSERT INTO prices VALUES (42, 25.00, 'EUR', DATE '2026-03-01', NULL);
-- ERROR: conflicting key value violates exclusion constraint "prices_no_overlap"

btree_gist lets the GiST index handle the plain equality on product_id and currency, and && is the range overlap operator. Recent PostgreSQL versions also support declaring a primary key WITHOUT OVERLAPS on a range column, which expresses the same rule (check your version). MySQL has neither, so there we change prices inside one transaction that first locks the product's row with SELECT ... FOR UPDATE, then closes the current period and inserts the new one, and every writer must follow that protocol.

4Indexing the Lookup and Applying a Price Change

The primary key (product_id, currency, valid_from) already provides a B-tree index that matches the lookup: the database jumps to product 42 in EUR, scans backwards from 2026-03-15 to the last valid_from on or before it, and checks valid_to. If the key were different, we would add an index on (product_id, valid_from) for exactly this query. Because products rarely have more than a few dozen price rows, the lookup reads one or two index entries.

SQL
-- New price 23.90 from 2026-06-01: close the current period, open a new one
BEGIN;
UPDATE prices SET valid_to = DATE '2026-06-01'
WHERE product_id = 42 AND currency = 'EUR' AND valid_to IS NULL; -- UPDATE 1
INSERT INTO prices VALUES (42, 23.90, 'EUR', DATE '2026-06-01', NULL); -- INSERT 0 1
COMMIT;

Doing both steps in one transaction means readers never see a moment with no current price or two of them, and the exclusion constraint checks the result. Future-dated prices work naturally: the new row simply starts after today, and queries for today keep returning the old price until that date arrives.

5Why the Invoice Keeps Its Own Price

It is tempting to compute invoice totals by looking up the price table for the invoice date. But an invoice is a legal record of what was charged, and it must not change if someone later corrects a price row, applies a discount retroactively, or a rounding rule changes. The price on the invoice was also often not the list price: discounts, contract prices and promotions all apply.

Step throughPricing an invoice line
  1. Look up the price on the order datePrice table, the query above
  2. Apply discounts and contract termsBusiness rules
  3. Store unit_price on the invoice lineA snapshot, never recomputed
  4. Later correction of the price table?
    YesInvoice unchanged, issue a credit note if neededNoNothing to do

4 steps in order. Play them, or step through them one at a time.

So the price table answers 'what was the list price on that day', and the invoice line stores 'what we actually charged'. Both are needed, and they are different facts.

6Follow-up questions

What is the difference between valid time and transaction time?

Valid time is when a fact was true in the real world: this price applied from 1 February. Transaction time is when the database recorded the fact: we entered that price on 28 January, or corrected it on 10 February. Our table models valid time. If we also need to answer 'what did we believe the price was, as of 5 February', we add transaction time columns such as recorded_from and recorded_to, which makes the table bitemporal, and corrections then close the old row in transaction time instead of overwriting it.

Why use an exclusive end date instead of an inclusive one?

With an exclusive end, one period's valid_to equals the next period's valid_from, so periods tile without gaps or overlaps and the query uses simple < and <= comparisons. An inclusive end forces us to store the day before the next start, and every change must compute that day, which is where off-by-one bugs come from.

Why not use 'infinity' instead of null for the current price?

PostgreSQL supports date 'infinity', and it lets the query drop the IS NULL branch, which can make it slightly simpler and more index-friendly. Null is more portable across databases and tools. Either is fine if the whole team uses the same convention.

7Conclusion

In this answer, we modeled prices with half-open validity periods, valid_from inclusive and valid_to exclusive or null for the current price, and wrote the lookup for product 42 on 2026-03-15 with <= on the start and > on the end. We prevented overlapping periods with an exclusion constraint over daterange in PostgreSQL or locked transactional updates in MySQL, used the (product_id, valid_from) ordering of the key for the lookup, applied price changes in one transaction, and kept the charged price on the invoice line because the invoice is a legal record.

What to say in the interviewAbout 90 seconds aloud

Open with

I would store prices as rows with product_id, price, currency, valid_from and valid_to, using half-open periods: valid_from is inclusive, valid_to is exclusive, and null means it is the current price.

Then, if they want more

So one period ends on exactly the day the next begins, with no gaps or overlaps. The query for product 42 on 15 March 2026 is: select price from prices where product_id is 42, and the currency if there are several, and valid_from is less than or equal to that date, and valid_to is null or greater than that date.

The comparisons matter: less than or equal on the start, strictly greater on the end, otherwise you get two rows on the day a price changes. To make sure there is only ever one match, I would add an exclusion constraint in PostgreSQL over product_id with equals and daterange of valid_from and valid_to with overlaps, which needs btree_gist.

In MySQL I would lock the product row and close the old period and insert the new one in one transaction. For the lookup, an index on product_id and valid_from, which the primary key can provide. Price changes are always close and insert in one transaction, and future-dated prices just work.

Finally, I would still store the applied unit price on the invoice line, because the invoice is a legal record of what we charged, including discounts, and must not change if the price table is corrected later.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Product prices change over time and invoices must show the price that applied on any...": https://scrivio-interview-library.netlify.app/databases/data-modeling/fab936f902/