← Problems74. Model returns against ordersMediumData Modelling
00:00 / 30:00

Model returns against orders

MediumNot attempted yetAsked at Amazon

Customers return part of an order, sometimes more than once, sometimes months later. Finance needs net revenue per product — sales less returns — and it has to stay right when only two of the three units come back.

What the grader expects

Your DDL is executed, rows are inserted into the tables you create, and queries are run against them, so the names are part of the answer:

dim_product product_key INTEGER product_name VARCHAR fact_order_lines order_id BIGINT line_number INTEGER product_key INTEGER quantity INTEGER amount DECIMAL(18,2) fact_order_returns return_id BIGINT order_id BIGINT line_number INTEGER quantity INTEGER amount DECIMAL(18,2) returned_on DATE

Constraints

  • A return is its own row, at return grain — not a flag or a column on the order line
  • One order line can have several returns; together they never exceed what was sold
  • Relationships are optional, but if you draw them, the returns point at the order line
  • The trap is a returned BOOLEAN on fact_order_lines: it answers "was this returned" and can express neither a partial return nor a second one

Example 1a whole line comes back

Input · dim_product
product_keyproduct_name
1Lamp
Input · fact_order_lines
amountorder_idquantityline_numberproduct_key
20010211
Input · fact_order_returns
amountorder_idquantityreturn_idline_numberreturned_on
20010250012026-02-01
Output
product_namenet_revenue
Lamp0

Example 2only part of the line comes back

Input · dim_product
product_keyproduct_name
1Chair
Input · fact_order_lines
amountorder_idquantityline_numberproduct_key
30011311
Input · fact_order_returns
amountorder_idquantityreturn_idline_numberreturned_on
10011150112026-02-05
Output
soldreturnednet
300100200

Topics

data modelingfact grain

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Click a column, then another table’s column, to link them.

Start with a table. Give it a grain, then the columns that belong at that grain.

Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.