KatInterview

Design and Query a Retail Order Database

SQL & DataModel answer
MediumModel answerSQL & Datasqlschema-designaggregation

Last updated

Build a PostgreSQL schema for orders, write insert statements, compute revenue aggregates with outer joins, filter recent purchases, and update coupon fields.

You are building a retail order tracking system in PostgreSQL. Complete the following tasks:

Part 1: Schema Definition

Define four tables with appropriate data types and constraints:

  • customers: customer_id (primary key), email (unique, required), created_at (required date)
  • products: product_id (primary key), name (required), category (required), price (two decimal places, must be positive)
  • orders: order_id (primary key), customer_id (foreign key to customers), order_date (required date), status (required, must be 'paid', 'refunded', or 'cancelled'), coupon_code (optional text)
  • order_items: composite primary key on (order_id, product_id), order_id (foreign key to orders with cascade delete), product_id (foreign key to products), qty (required positive integer), unit_price (two decimal places, must be positive)

Choose minimal correct types for each column.

Part 2: Data Loading

Insert the exact rows shown in these tables:

customers

customer_id | email               | created_at
----------- | ------------------- | ----------
1           | [email protected]   | 2025-08-20
2           | [email protected]     | 2025-08-28
3           | [email protected] | 2025-08-30

products

product_id | name   | category | price
---------- | ------ | -------- | ------
10         | Widget | hardware | 25.00
11         | Gizmo  | hardware | 40.00
12         | Course | digital  | 199.00

orders

order_id | customer_id | order_date | status    | coupon_code
-------- | ----------- | ---------- | --------- | -----------
100      | 1           | 2025-08-29 | paid      | NULL
101      | 1           | 2025-09-01 | refunded  | NULL
102      | 2           | 2025-08-31 | paid      | SUMMER10
103      | 3           | 2025-09-01 | cancelled | NULL
104      | 1           | 2025-08-25 | paid      | NULL

order_items

order_id | product_id | qty | unit_price
-------- | ---------- | --- | ----------
100      | 10         | 2   | 25.00
100      | 11         | 1   | 40.00
101      | 12         | 1   | 199.00
102      | 10         | 1   | 25.00
103      | 11         | 2   | 40.00
104      | 10         | 1   | 25.00
104      | 11         | 1   | 40.00

Part 3: Revenue Report

Write a query that calculates total revenue per customer from paid orders placed during August 2025 (inclusive of both 2025-08-01 and 2025-08-31). Revenue equals the sum of (qty × unit_price) across all items in qualifying orders.

Return columns: customer_id, email, revenue_aug2025

Include all customers in the output, showing 0 for those with no qualifying revenue. Sort by revenue descending, then by customer_id ascending.

Part 4: Recent Product Orders

Assume today's date is 2025-09-01. Write a query that finds all products that appear in orders placed within the last 7 days (from 2025-08-26 through 2025-09-01, inclusive). Exclude orders with status 'refunded' or 'cancelled'.

Return columns: product_id, name, first_order_date_in_window

Show the earliest order date in the window for each product. Each product should appear at most once.

Part 5: Coupon Backfill

Write an UPDATE statement that sets coupon_code to 'NONE' for all paid orders from August 2025 that currently have a NULL coupon_code.

Then write a SELECT query that verifies the update by counting how many rows still match the original condition (paid, August 2025, NULL coupon).

Hints

Hint 1

Create tables in dependency order: parent tables before children that reference them.

Hint 2

For the revenue query, use LEFT JOIN to preserve customers with zero revenue. Apply date and status filters in a subquery, not in the outer WHERE clause.

Hint 3

The recent products query needs GROUP BY with MIN to find the first occurrence per product and eliminate duplicates.