Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 101/ 160
06. Database Design, Normalization & Semi-Structured SQL

Entity-Relationship Diagrams (ERD) & Conceptual Data Modeling

Capture entities, attributes, and relationships before writing CREATE TABLE.

BeginnerERD · Entities3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Draw boxes for the things you store, list what you know about each, and draw lines showing how they connect, before writing any SQL.

Think of it like…

An architect's floor plan: rooms (entities), what's in them (attributes) and the doors between them (relationships) are agreed on paper before anyone pours concrete.

Where you’ve seen it

Designing a food-delivery app: Customer places many Orders; each Order comes from one Restaurant; an Order has many OrderItems, each referring to one MenuItem; a Rider delivers many Orders, and an Order has at most one Rider. The many-to-many between Order and MenuItem becomes the OrderItem table.

Words you’ll hear

Entity
A thing you store data about; usually becomes a table.
Attribute
A property of an entity; usually becomes a column.
Cardinality
How many of one entity relate to another: one-to-one, one-to-many, many-to-many.
Crow's foot
Notation where a three-pronged end means "many" and a circle means "optional".

Visual diagram

Loading interactive diagram

Theory & example queries

Finding entities and relationships

Read the requirements and underline nouns (customer, order, restaurant, rider): they're candidate entities. Things that are just properties of something else (customer's phone, order's total) are attributes. Verbs between nouns (places, contains, delivers) are candidate relationships.

For each relationship, ask how many on each side and whether it's required: can an order exist without a rider (yes, until assigned)? Can a customer have no orders (yes)? Those answers set the cardinality and optionality, which later decide where foreign keys go and whether they're nullable.

Questions that fix cardinality

"How many restaurants can one order come from?" (one) "How many orders can one restaurant receive?" (many) → one-to-many, with restaurant_id on orders, NOT NULL.

Key takeaway: Nouns → entities, verbs → relationships, two questions → cardinality.

From diagram to schema

The logical model adds primary keys to every entity, foreign keys on the many side of one-to-many relationships, and junction tables for many-to-many relationships (often with their own attributes, like quantity on order_items). The physical model then adds data types, constraints and indexes for a specific database.

Keep the ERD in sync with the real schema: many tools (dbdiagram.io, DBeaver, pgAdmin, draw.io) can draw it from DDL or generate DDL from it. In an interview, a quick sketch of 5–8 entities with their relationships and a word on keys is usually what's wanted before any CREATE TABLE.

A junction table with its own attributesQuery example
1Input dataStart from the source rows or state that the statement sees.
order_items
3 rows
order_items sample rows
idorder_idproduct_idqtyprice
110142249.5
2101841150
310242149.5
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1CREATE TABLE order_items (2  order_id bigint REFERENCES orders (id),3  menu_item_id bigint REFERENCES menu_items (id),4  quantity int NOT NULL CHECK (quantity > 0),5  price_paid numeric(10, 2) NOT NULL,6  PRIMARY KEY (order_id, menu_item_id)7);
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/7
3OutputInspect the representative rows, affected-row count, plan, or command status.
Expected output
CREATE TABLE

Note: Representative PostgreSQL status when both referenced tables already exist.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Key takeaway: Conceptual → logical (keys, junctions) → physical (types, indexes).

Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Requirements

List the entities

Pick out the nouns the system must remember, and drop ones that are only attributes of something else.

Focus at this step

Customer · Restaurant · Order · MenuItem · Rider

What to watch at this step
Typical
5–10 entities
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

An Entity-Relationship Diagram (ERD) sketches a data model before any tables exist: entities (things like Customer, Order, Product) as boxes, their attributes (name, amount), and the relationships between them as lines marked with cardinality (one or many, optional or required), usually in crow's-foot notation. It moves from conceptual (what exists) to logical (keys and junction tables) to physical (actual tables and types).

Why it matters: Schema mistakes are expensive to fix once data and code depend on them. An ERD lets you agree the model with others in minutes and catch problems early, and in interviews, "design the database for X" is answered best by sketching entities and relationships first.

When to reach for it

  • Starting any new schema or feature with new tables
  • Schema-design interview questions
  • Documenting an existing database for a team

When to avoid or be careful

  • Writing CREATE TABLE before agreeing the entities and relationships
  • Modelling many-to-many relationships with a single foreign key
  • Letting the diagram drift out of date with the real schema

Conceptual vs logical vs physical models

Conceptual model
Best for: Agreeing what exists with non-technical people

Strengths: Simple boxes and lines; fast to change.

Watch out: No keys or types.

Logical model
Best for: Designing keys, relationships and junction tables

Strengths: Database-independent but precise.

Watch out: Not yet tuned for a specific engine.

Physical model
Best for: The actual schema in a chosen database

Strengths: Types, constraints and indexes ready to create.

Watch out: Harder to discuss with non-engineers.

Interview signals & numbers to know

When this topic comes up

  • “Design the database for an e-commerce / delivery / booking app”
  • “What's an ERD?”
  • “Explain cardinality”
  • Crow's foot notation
  • Data modelling

Benchmarks & rules of thumb

3 -
levels: conceptual, logical, physical
3 -
cardinalities: 1:1, 1:N, M:N
1 -
junction table per many-to-many relationship

Common mistakes & follow-ups

Common interview mistakes

  • Treating attributes (like address) as separate entities without a reason, or entities as attributes.
  • Missing a many-to-many relationship, so an order can contain only one menu item.
  • Skipping optionality, then making a foreign key NOT NULL that should allow "not yet assigned".

Likely interviewer follow-ups

  • Draw an ERD for a library system with members, books and loans.
  • How does a many-to-many relationship appear in the final tables?
  • What's the difference between a conceptual and a physical model?
Interview tip: In a design interview, spend the first minutes sketching entities and relationships with cardinality, out loud. Interviewers judge the model far more than the SQL syntax.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Entity-Relationship Diagrams (ERD) & Conceptual Data Modeling with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Solving Classic FAANG / Senior Data Analyst SQL Interview Problems
Next topic
Functional Dependencies & Candidate Keys