Create and query an e-commerce schema
E-commerce Schema Design and Querying
This task requires you to design and interact with a PostgreSQL database schema for an e-commerce platform. You will create tables, populate them with sample data, and then write several analytical queries and a data manipulation statement.
Part 1: Schema Creation (DDL)
Create the following tables in PostgreSQL, ensuring appropriate data types and constraints are applied. Choose the minimal correct types for each column.
- products:
product_id: Primary Keyname: Not Nullcategory: Not Nullprice: Decimal (10,2), Check (price > 0)
- customers:
customer_id: Primary Keyemail: Unique, Not Nullcreated_at: Date, Not Null
- orders:
order_id: Primary Keycustomer_id: Foreign Key referencingcustomersorder_date: Date, Not Nullstatus: Check (status IN ('paid', 'refunded', 'cancelled')), Not Nullcoupon_code: Text, Nullable
- order_items:
order_id: Foreign Key referencingorders(ON DELETE CASCADE)product_id: Foreign Key referencingproductsqty: Integer, Check (qty > 0), Not Nullunit_price: Decimal (10,2), Check (unit_price > 0), Not Null- Primary Key: Composite key on (
order_id,product_id)
Part 2: Data Insertion (DML)
Insert rows into the created tables to exactly match the provided sample data:
customers:
| customer_id | 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: Query A - Monthly Gross Revenue
Return the per-customer gross revenue from orders that meet the following criteria:
- Order status is 'paid'.
order_dateis between '2025-08-01' and '2025-08-31' (inclusive).
Revenue should be calculated as SUM(qty * unit_price) for each order item. Include all customers, even those with zero revenue for the specified period, displaying their revenue as 0.
Output columns: customer_id, email, revenue_aug2025.
Sort the results by revenue_aug2025 in descending order, then by customer_id in ascending order.
Part 4: Query B - Recent Product Orders
Using '2025-09-01' as "today's" date, list all products that were ordered in the last 7 days (inclusive, i.e., from '2025-08-26' to '2025-09-01'). Exclude orders with status 'refunded' or 'cancelled'.
Output columns: product_id, name, first_order_date_in_window. The first_order_date_in_window should be the earliest order_date for that specific product within the defined 7-day window.
Part 5: Data Manipulation (DML)
Write a single UPDATE statement to modify the orders table:
- Set
coupon_codeto 'NONE'. - Apply this update only to orders where the
statusis 'paid', theorder_dateis in August 2025, and thecoupon_codeis currentlyNULL.
Immediately following the UPDATE statement, write a SELECT query to verify the exact number of rows that were affected by the update.
AI interview prep
Core dilemma
Balancing strict data integrity and correctness with efficient and accurate data retrieval and manipulation using SQL, including handling edge cases and temporal logic precisely.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Create and query an e-commerce schema
E-commerce Schema Design and Querying
This task requires you to design and interact with a PostgreSQL database schema for an e-commerce platform. You will create tables, populate them with sample data, and then write several analytical queries and a data manipulation statement.
Part 1: Schema Creation (DDL)
Create the following tables in PostgreSQL, ensuring appropriate data types and constraints are applied. Choose the minimal correct types for each column.
- products:
product_id: Primary Keyname: Not Nullcategory: Not Nullprice: Decimal (10,2), Check (price > 0)
- customers:
customer_id: Primary Keyemail: Unique, Not Nullcreated_at: Date, Not Null
- orders:
order_id: Primary Keycustomer_id: Foreign Key referencingcustomersorder_date: Date, Not Nullstatus: Check (status IN ('paid', 'refunded', 'cancelled')), Not Nullcoupon_code: Text, Nullable
- order_items:
order_id: Foreign Key referencingorders(ON DELETE CASCADE)product_id: Foreign Key referencingproductsqty: Integer, Check (qty > 0), Not Nullunit_price: Decimal (10,2), Check (unit_price > 0), Not Null- Primary Key: Composite key on (
order_id,product_id)
Part 2: Data Insertion (DML)
Insert rows into the created tables to exactly match the provided sample data:
customers:
| customer_id | 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: Query A - Monthly Gross Revenue
Return the per-customer gross revenue from orders that meet the following criteria:
- Order status is 'paid'.
order_dateis between '2025-08-01' and '2025-08-31' (inclusive).
Revenue should be calculated as SUM(qty * unit_price) for each order item. Include all customers, even those with zero revenue for the specified period, displaying their revenue as 0.
Output columns: customer_id, email, revenue_aug2025.
Sort the results by revenue_aug2025 in descending order, then by customer_id in ascending order.
Part 4: Query B - Recent Product Orders
Using '2025-09-01' as "today's" date, list all products that were ordered in the last 7 days (inclusive, i.e., from '2025-08-26' to '2025-09-01'). Exclude orders with status 'refunded' or 'cancelled'.
Output columns: product_id, name, first_order_date_in_window. The first_order_date_in_window should be the earliest order_date for that specific product within the defined 7-day window.
Part 5: Data Manipulation (DML)
Write a single UPDATE statement to modify the orders table:
- Set
coupon_codeto 'NONE'. - Apply this update only to orders where the
statusis 'paid', theorder_dateis in August 2025, and thecoupon_codeis currentlyNULL.
Immediately following the UPDATE statement, write a SELECT query to verify the exact number of rows that were affected by the update.