Aim: By the end of this page you can explain what a data model is and why it comes before any AWS service: name the entities, attributes and relationships of RegularMart, walk a model from conceptual to logical to physical, tell an OLTP model from an OLAP model, and say which of the 10 data modeling topics comes next, confidently, in a data engineering interview.
Description: Data that lives in one big sheet repeats itself, contradicts itself and lets bad rows in, so every report is a guess. A data model is the blueprint: which things we store, what we know about each, how they connect, and which rules keep them correct. On this page you see the problem, the three building blocks, the three levels of a model, the app model vs the analytics model, the strengths and limits of modeling, when not to model heavily, the main modeling styles, how real tools (Amazon Redshift, Azure Synapse, Google BigQuery, dbt, Apache Iceberg, Delta Lake) show the model, and where the model sits in a real AWS pipeline.
Before you start: basic SQL (SELECT, WHERE, GROUP BY, JOIN). No AWS account is needed for this page: everything runs in duckdb on your laptop.
Install duckdb (a free database that runs on your laptop, no server and no AWS account needed). Use the Python package or the CLI:
pip install duckdb # Python: import duckdb; con = duckdb.connect("regularmart.duckdb")
# or the CLI from duckdb.org, then:
duckdb regularmart.duckdbCreate the RegularMart tables (run once · paste into duckdb, or duckdb regularmart.duckdb < setup_regularmart.sql):
-- setup_regularmart.sql · the RegularMart tables (same rows as the SQL series)
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR NOT NULL,
city VARCHAR
);
INSERT INTO customers VALUES
(1, 'Arjun', 'Hyderabad'), (2, 'Divya', 'Chennai'), (3, 'Farhan', 'Mumbai'),
(4, 'Lakshmi', 'Bengaluru'), (5, 'Rahul', 'Delhi');
-- raw orders, exactly as the app sent them (no foreign key yet: order 5005 points to customer 6)
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER,
product VARCHAR,
amount INTEGER,
order_date DATE
);
INSERT INTO orders VALUES
(5001, 1, 'Mobile', 15000, '2024-01-05'),
(5002, 2, 'Laptop', 55000, '2024-01-07'),
(5003, 1, 'Headphones', 2000, '2024-01-09'),
(5004, 4, 'Watch', 3500, '2024-01-10'),
(5005, 6, 'Shoes', 2500, '2024-01-11');
CREATE TABLE products (
product_id VARCHAR PRIMARY KEY,
product_name VARCHAR NOT NULL,
category VARCHAR,
price INTEGER
);
INSERT INTO products VALUES
('P1', 'Mobile', 'Electronics', 15000), ('P2', 'Laptop', 'Electronics', 55000),
('P3', 'Headphones', 'Accessories', 2000), ('P4', 'Watch', 'Accessories', 3500),
('P5', 'Shoes', 'Fashion', 2500);The same SQL runs in PostgreSQL. Orders are loaded raw on purpose (no foreign key yet), so order 5005 for customer 6 is in the table: finding and blocking it is part of the lab.
These are the three tables the setup script creates, read back from duckdb: the same RegularMart rows you saw in the animation (order 5005 points to customer 6, who does not exist).
customers 5 rows
| customer_id | name | city |
|---|---|---|
| 1 | Arjun | Hyderabad |
| 2 | Divya | Chennai |
| 3 | Farhan | Mumbai |
| 4 | Lakshmi | Bengaluru |
| 5 | Rahul | Delhi |
orders 5 rows
| order_id | customer_id | product | amount | order_date |
|---|---|---|---|---|
| 5001 | 1 | Mobile | 15000 | 2024-01-05 |
| 5002 | 2 | Laptop | 55000 | 2024-01-07 |
| 5003 | 1 | Headphones | 2000 | 2024-01-09 |
| 5004 | 4 | Watch | 3500 | 2024-01-10 |
| 5005 | 6 | Shoes | 2500 | 2024-01-11 |
products 5 rows
| product_id | product_name | category | price |
|---|---|---|---|
| P1 | Mobile | Electronics | 15000 |
| P2 | Laptop | Electronics | 55000 |
| P3 | Headphones | Accessories | 2000 |
| P4 | Watch | Accessories | 3500 |
| P5 | Shoes | Fashion | 2500 |
Task 1 🗂️ Your lead asks: “Which entities does RegularMart store today, and how many rows does each have?”
💭 Hint: One table per entity. Count each table and stack the answers with UNION ALL.
SELECT 'customers' AS entity, COUNT(*) AS row_count FROM customers UNION ALL SELECT 'orders', COUNT(*) FROM orders UNION ALL SELECT 'products', COUNT(*) FROM products;
✅ real output · duckdb on the RegularMart tables · 3 rows
| entity | row_count |
|---|---|
| customers | 5 |
| orders | 5 |
| products | 5 |
Task 2 🏷️ A new analyst wants to know the attributes of the orders entity and their data types before writing a report.
💭 Hint: information_schema.columns lists every column with its data type (DESCRIBE orders works too).
SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = 'orders' ORDER BY ordinal_position;
✅ real output · duckdb on the RegularMart tables · 5 rows
| column_name | data_type | is_nullable |
|---|---|---|
| order_id | INTEGER | NO |
| customer_id | INTEGER | YES |
| product | VARCHAR | YES |
| amount | INTEGER | YES |
| order_date | DATE | YES |
Task 3 📄 The boss still wants “the old Excel view”: one row per order with the customer name and city. Rebuild it from the model without losing order 5005.
💭 Hint: LEFT JOIN customers to orders on customer_id: every order stays, and an order with no matching customer shows NULL.
SELECT o.order_id, c.name, c.city, o.product, o.amount FROM orders o LEFT JOIN customers c ON c.customer_id = o.customer_id ORDER BY o.order_id;
✅ real output · duckdb on the RegularMart tables · 5 rows
| order_id | name | city | product | amount |
|---|---|---|---|---|
| 5001 | Arjun | Hyderabad | Mobile | 15000 |
| 5002 | Divya | Chennai | Laptop | 55000 |
| 5003 | Arjun | Hyderabad | Headphones | 2000 |
| 5004 | Lakshmi | Bengaluru | Watch | 3500 |
| 5005 | NULL | NULL | Shoes | 2500 |
Task 4 🕵️ The finance team says one order “belongs to nobody”. Find every order whose customer does not exist.
💭 Hint: An anti-join: orders LEFT JOIN customers, keep the rows where the customer side is NULL.
SELECT o.order_id, o.customer_id, o.product, o.amount FROM orders o LEFT JOIN customers c ON c.customer_id = o.customer_id WHERE c.customer_id IS NULL;
✅ real output · duckdb on the RegularMart tables · 1 row
| order_id | customer_id | product | amount |
|---|---|---|---|
| 5005 | 6 | Shoes | 2500 |
Task 5 🏙️ Marketing asks for total sales per city, using only orders that belong to a real customer.
💭 Hint: INNER JOIN follows the relationship; then GROUP BY city.
SELECT c.city, COUNT(*) AS orders, SUM(o.amount) AS sales FROM orders o JOIN customers c ON c.customer_id = o.customer_id GROUP BY c.city ORDER BY sales DESC;
✅ real output · duckdb on the RegularMart tables · 3 rows
| city | orders | sales |
|---|---|---|
| Chennai | 1 | 55000 |
| Hyderabad | 2 | 17000 |
| Bengaluru | 1 | 3500 |
Task 6 🙋 Which customers have never placed an order? Sales wants to send them a welcome coupon.
💭 Hint: Start from customers this time and keep the ones with no order (NOT EXISTS).
SELECT c.customer_id, c.name, c.city FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id) ORDER BY c.customer_id;
✅ real output · duckdb on the RegularMart tables · 2 rows
| customer_id | name | city |
|---|---|---|
| 3 | Farhan | Mumbai |
| 5 | Rahul | Delhi |
Task 7 🚚 Arjun moves to Pune. In the old sheet you had to edit two rows. In the model, change it once and show that both of his orders now say Pune.
💭 Hint: UPDATE customers once, then rebuild the sheet view with a JOIN.
UPDATE customers SET city = 'Pune' WHERE customer_id = 1; SELECT o.order_id, c.name, c.city, o.product FROM orders o JOIN customers c ON c.customer_id = o.customer_id WHERE c.customer_id = 1 ORDER BY o.order_id;
✅ real output · duckdb on the RegularMart tables · 2 rows
| order_id | name | city | product |
|---|---|---|---|
| 5001 | Arjun | Pune | Mobile |
| 5003 | Arjun | Pune | Headphones |
Task 8 🔑 Build the clean physical table orders_clean with a FOREIGN KEY to customers, load the four good orders, then try order 5005.
💭 Hint: REFERENCES customers(customer_id) on the column. duckdb enforces it, so the bad insert fails with a constraint error.
CREATE TABLE orders_clean (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
product VARCHAR,
amount INTEGER CHECK (amount > 0),
order_date DATE
);
INSERT INTO orders_clean SELECT * FROM orders WHERE order_id <> 5005;
SELECT COUNT(*) AS loaded FROM orders_clean;CREATE TABLE orders_clean (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
product VARCHAR,
amount INTEGER CHECK (amount > 0),
order_date DATE
);
-- now the bad row
INSERT INTO orders_clean VALUES (5005, 6, 'Shoes', 2500, '2024-01-11');✅ real output · duckdb on the RegularMart tables · 1 row
| loaded |
|---|
| 4 |
✅ real output · duckdb refused the statement (this error is the lesson)
Constraint Error: Violates foreign key constraint because key "customer_id: 6" does not exist in the referenced table
✅ real output · the last query before the error · 0 rows
| Count |
|---|
| Empty set (0 rows) |
Task 9 📦 The category manager wants sales per category. Orders only store the product name, so follow the relationship to products.
💭 Hint: JOIN orders.product to products.product_name, then GROUP BY category.
SELECT p.category, COUNT(*) AS orders, SUM(o.amount) AS sales FROM orders o JOIN products p ON p.product_name = o.product GROUP BY p.category ORDER BY sales DESC;
✅ real output · duckdb on the RegularMart tables · 3 rows
| category | orders | sales |
|---|---|---|
| Electronics | 2 | 70000 |
| Accessories | 2 | 5500 |
| Fashion | 1 | 2500 |
Task 10 ⭐ Build a tiny star schema for the analytics team: dim_customer with a surrogate key, and fact_sales with a date_key like 20240105. Then report sales per city from the star.
💭 Hint: ROW_NUMBER() makes customer_sk; strftime(order_date, '%Y%m%d') makes the date_key. Join the fact to the dimension on customer_sk.
CREATE TABLE dim_customer AS
SELECT ROW_NUMBER() OVER (ORDER BY customer_id) AS customer_sk, customer_id, name, city
FROM customers;
CREATE TABLE fact_sales AS
SELECT o.order_id, d.customer_sk,
CAST(strftime(o.order_date, '%Y%m%d') AS INTEGER) AS date_key,
o.amount
FROM orders o
JOIN dim_customer d ON d.customer_id = o.customer_id;
SELECT d.city, SUM(f.amount) AS sales, MIN(f.date_key) AS first_day
FROM fact_sales f
JOIN dim_customer d ON d.customer_sk = f.customer_sk
GROUP BY d.city
ORDER BY sales DESC;✅ real output · duckdb on the RegularMart tables · 3 rows
| city | sales | first_day |
|---|---|---|
| Chennai | 55000 | 20240107 |
| Hyderabad | 17000 | 20240105 |
| Bengaluru | 3500 | 20240110 |
SELECT table_name, constraint_type, constraint_column_names FROM duckdb_constraints() WHERE constraint_type = 'PRIMARY KEY' ORDER BY table_name;
✅ real output · duckdb on the RegularMart tables · 3 rows
| table_name | constraint_type | constraint_column_names |
|---|---|---|
| customers | PRIMARY KEY | ['customer_id'] |
| orders | PRIMARY KEY | ['order_id'] |
| products | PRIMARY KEY | ['product_id'] |
💡 Say this in the interview: “A data model is the blueprint of the data: entities, attributes, relationships and the rules, like keys. At RegularMart it is customers, orders and products, linked by customer_id.”
CREATE TABLE orders_physical (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
amount INTEGER
);
SELECT column_name, data_type FROM information_schema.columns
WHERE table_name = 'orders_physical' ORDER BY ordinal_position;✅ real output · duckdb on the RegularMart tables · 3 rows
| column_name | data_type |
|---|---|
| order_id | INTEGER |
| customer_id | INTEGER |
| amount | INTEGER |
💡 Say this in the interview: “Conceptual is what the business means, logical adds every attribute and key, physical is the CREATE TABLE for one database. Same model, three levels of detail.”
-- an OLAP-style question on the OLTP tables: one big read across many rows SELECT c.city, SUM(o.amount) AS sales FROM orders o JOIN customers c USING (customer_id) GROUP BY c.city ORDER BY sales DESC;
✅ real output · duckdb on the RegularMart tables · 3 rows
| city | sales |
|---|---|
| Chennai | 55000 |
| Hyderabad | 17000 |
| Bengaluru | 3500 |
💡 Say this in the interview: “OLTP is modeled for many small writes and is normalized; OLAP is modeled for big reads and is usually a star schema. Our pipeline moves data from the first to the second.”
SELECT COUNT(*) AS orphan_orders, SUM(amount) AS amount_at_risk FROM orders o WHERE NOT EXISTS (SELECT 1 FROM customers c WHERE c.customer_id = o.customer_id);
✅ real output · duckdb on the RegularMart tables · 1 row
| orphan_orders | amount_at_risk |
|---|---|
| 1 | 2500 |
💡 Say this in the interview: “The model is the contract every job and dashboard depends on. Designing it first is cheap; changing it after the pipeline is built means rewriting jobs and reloading data.”
CREATE TABLE orders_fk (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers (customer_id)
);
INSERT INTO orders_fk VALUES (5005, 6);✅ real output · duckdb refused the statement (this error is the lesson)
Constraint Error: Violates foreign key constraint because key "customer_id: 6" does not exist in the referenced table
✅ real output · the last query before the error · 0 rows
| Count |
|---|
| Empty set (0 rows) |
💡 Say this in the interview: “In Redshift the keys are informational only, so a bad row loads silently. I always add an orphan check or a dbt relationships test in the pipeline.”
Build the clean model in one script: orders_clean with a foreign key to customers, a quarantine table for rows that break the rule, load every raw order into the right place, then report sales per category from the clean orders only.
💭 Hint: Split the raw orders with EXISTS / NOT EXISTS: good rows go to orders_clean, the rest to orders_quarantine with a reason. Then join orders_clean to products.
CREATE TABLE orders_clean (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
product VARCHAR,
amount INTEGER CHECK (amount > 0),
order_date DATE
);
CREATE TABLE orders_quarantine AS
SELECT *, 'unknown customer' AS reason FROM orders o
WHERE NOT EXISTS (SELECT 1 FROM customers c WHERE c.customer_id = o.customer_id);
INSERT INTO orders_clean
SELECT * FROM orders o
WHERE EXISTS (SELECT 1 FROM customers c WHERE c.customer_id = o.customer_id);
SELECT 'clean' AS place, COUNT(*) AS row_count FROM orders_clean
UNION ALL SELECT 'quarantine', COUNT(*) FROM orders_quarantine;CREATE TABLE orders_clean AS SELECT * FROM orders o WHERE EXISTS (SELECT 1 FROM customers c WHERE c.customer_id = o.customer_id); SELECT p.category, SUM(o.amount) AS sales FROM orders_clean o JOIN products p ON p.product_name = o.product GROUP BY p.category ORDER BY sales DESC;
✅ real output · duckdb on the RegularMart tables · 2 rows
| place | row_count |
|---|---|
| clean | 4 |
| quarantine | 1 |
✅ real output · duckdb on the RegularMart tables · 2 rows
| category | sales |
|---|---|
| Electronics | 70000 |
| Accessories | 5500 |