Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 5 OF 6 · 2 MIN READ

Schema Design and Normalization

Good schema design keeps data correct and queries simple. Interviewers ask you to design tables for a feature, explain normal forms, model many-to-many relationships, and say when denormalizing is worth it.

What you will learn

  • Explain 1NF, 2NF and 3NF in plain words
  • Model one-to-many and many-to-many relationships
  • Pick keys: serial, UUID v7 or natural keys
  • Decide when to denormalize

Key concepts

Normalization

1NF: one value per cell, no repeating groups. 2NF: every column depends on the whole key. 3NF: no column depends on another non-key column (store city_id, not city_name and city_pincode in every order).

Junction table

Many-to-many relationships such as students and courses need a third table (enrollments) with both foreign keys and a composite unique constraint.

Denormalization

Copying data on purpose to make reads fast: an order stores the price at purchase time, a post stores comment_count. You must keep the copy in sync or accept it as a snapshot.

Soft delete

A deleted_at column instead of removing rows, so you can restore and audit. Remember to filter it out and to handle unique constraints (partial unique index WHERE deleted_at IS NULL).

Step by step

Design for an e-commerce order

users(id, email UNIQUE, name, created_at)
products(id, sku UNIQUE, name, price, stock)
orders(id, user_id → users, status, total, created_at)
order_items(order_id → orders, product_id → products, qty, unit_price,  PRIMARY KEY(order_id, product_id))
payments(id, order_id → orders, provider, provider_ref UNIQUE, amount, status)
  • unit_price is copied on purpose: product prices change, past orders must not
  • Money as numeric/decimal or integer paise, never float
  • Status as a constrained enum or check constraint

Examples

Many-to-many with a junction table

CREATE TABLE enrollments (
  student_id bigint REFERENCES students(id) ON DELETE CASCADE,
  course_id  bigint REFERENCES courses(id)  ON DELETE CASCADE,
  enrolled_at timestamptz DEFAULT now(),
  PRIMARY KEY (student_id, course_id)
);

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Design the schema for a ride-hailing app: riders, drivers, trips, ratings, payments.
  • Design tags on blog posts and write the query for posts with all of three given tags.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

UUID or auto-increment id?

Auto-increment is small and index-friendly but guessable and hard to generate across shards. Random UUIDv4 is unguessable but fragments B-tree indexes. Time-ordered UUIDv7 or ULID gives uniqueness across services with good index locality.

Why not store money as float?

Floating point cannot represent most decimals exactly (0.1 + 0.2 ≠ 0.3), so totals drift. Use DECIMAL/NUMERIC or integers in the smallest unit (paise, cents).

Summary

Normalize to keep one fact in one place, use junction tables for many-to-many, pick keys deliberately, store money exactly, and denormalize only for measured read needs.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus