Data Modeling · 01

Introduction to Data Modeling

🧩 RegularMart · SQL · duckdb
🗂️

1

Read the notes for this step ↓

🗒️ Step-by-step notes

🎯 Student AIM read this first · what you will learn on this page

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.

🤔 Why learn data modeling?

  • Without a model the same customer is stored twice, with two different cities, and totals go wrong.
  • Keys and relationships stop bad rows (like order 5005 for a customer who does not exist).
  • Every AWS service you will use (AWS Glue, Amazon Redshift, Amazon Athena) needs tables that someone designed.
  • “Design the model for this business” is one of the most common data engineering interview rounds.

🌍 Where is it used?

  • 🛒 Online shops: customers, orders, products.
  • 🏦 Banks: accounts, transactions, branches.
  • 🏥 Hospitals: patients, doctors, visits.
  • 🛵 Food delivery: restaurants, orders, riders.
  • 📊 Data warehouses: fact tables and dimension tables for dashboards.

📘 What you will learn

  • Why data modeling? The one-big-sheet problem, real scenarios, the road map.
  • Without / With: one messy sheet vs a modeled set of tables.
  • What is a data model? The blueprint: entities, attributes, relationships and rules.
  • Conceptual → logical → physical: the same RegularMart model at three levels.
  • OLTP vs OLAP models: the app database vs the analytics warehouse.
  • Your journey: the 10 data modeling topics of this series.
  • Pros / Cons: what a model gives you and what it costs.
  • Not a fit: one-off questions, raw landing zones, fast-changing events.
  • Compare: 3NF vs star schema vs Data Vault vs one big table.
  • Tools & clouds: Amazon Redshift, Azure Synapse, Google BigQuery, dbt, Apache Iceberg, Delta Lake.
  • Architecture: where the model sits in a real pipeline (sources → staging → model → BI).

✅ After this page you can

  • Spot repeated data, update problems and orphan rows in a flat sheet.
  • Name the entities, attributes and relationships of a small business.
  • Write the physical model (CREATE TABLE with keys) and watch it block a bad row.
  • Explain conceptual vs logical vs physical, and OLTP vs OLAP.
  • Answer the top 5 data modeling intro questions below.

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.

🧠 All memory tricks

🧪 Practice Lab real-time scenarios · SQL · duckdb (works the same in PostgreSQL; notes for Amazon Redshift)

Step 1 · Setup

Install duckdb (a free database that runs on your laptop, no server and no AWS account needed). Use the Python package or the CLI:

Terminal
pip install duckdb          # Python: import duckdb; con = duckdb.connect("regularmart.duckdb")
# or the CLI from duckdb.org, then:
duckdb regularmart.duckdb

Create the RegularMart tables (run once · paste into duckdb, or duckdb regularmart.duckdb < setup_regularmart.sql):

SQL · 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.

Step 2 · The data

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_idnamecity
1ArjunHyderabad
2DivyaChennai
3FarhanMumbai
4LakshmiBengaluru
5RahulDelhi

orders 5 rows

order_idcustomer_idproductamountorder_date
50011Mobile150002024-01-05
50022Laptop550002024-01-07
50031Headphones20002024-01-09
50044Watch35002024-01-10
50056Shoes25002024-01-11

products 5 rows

product_idproduct_namecategoryprice
P1MobileElectronics15000
P2LaptopElectronics55000
P3HeadphonesAccessories2000
P4WatchAccessories3500
P5ShoesFashion2500

Step 3 · Real-time scenario tasks easy → hard · try first, then click

EasyEntities = tables

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.

EasyAttributes + types

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).

EasyThe one big sheet

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.

MediumOrphan rows

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.

MediumRelationship: customer → orders

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.

MediumOptional relationship

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).

MediumStored once

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.

HardPhysical model · foreign key

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.

HardRelationship: order → product

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.

HardOLAP model preview

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.

Step 4 · Top 5 interview questions easy → tricky · click to reveal

Step 5 · Mini challenge: 🧩 RegularMart’s first clean model

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.