Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SQL
SQL
0/12 completed
Beginner
What is a database?SELECT: reading dataFiltering with WHERESorting and limiting results
Intermediate
Aggregates and GROUP BYJOINs: combining tablesINSERT, UPDATE and DELETECreating tables and constraints
Advanced
Subqueries and CTEsWindow functions
Pro
Indexes and transactionsDatabase design and normalization
Tracks / SQL / Pro

Database design and normalization

Writing queries is half the job. Designing the schema (which tables exist, what columns they have and how they relate) decides whether your app stays simple or turns into a mess of duplicated, inconsistent data.

The problem: one big table

Imagine storing everything in one spreadsheet-like table:

SQL · editable

Problems appear immediately:

  • Update anomaly: Asha's email is stored three times, and one copy is already wrong.
  • Insert anomaly: you can't record a new product until someone orders it.
  • Delete anomaly: delete Asha's orders and you lose the fact that she exists.

Normalization

Normalization means organising data so that each fact is stored exactly once. The normal forms are a checklist:

1NF (First Normal Form): each cell holds a single value, and there are no repeating groups. ✗ phones = '98765, 91234' → ✓ a separate phones table with one row per number.

2NF: every non-key column depends on the whole key. In a table keyed by (order_id, product_id), product_name depends only on product_id, so it belongs in products.

3NF: non-key columns depend only on the key, not on other non-key columns. If zip_code → city, then city belongs in a zip-code table, not in customers.

Memory trick: every column must describe "the key, the whole key, and nothing but the key."

The sample database in this track is already normalized: customers, products, and orders linking them.

Relationship types

Relationship Example How to model
One-to-one User ↔ Profile FK + UNIQUE on one side
One-to-many Customer → Orders FK on the "many" side (orders.customer_id)
Many-to-many Students ↔ Courses A junction table enrollments(student_id, course_id)
SQL · editable

Denormalization: breaking the rules on purpose

At scale, joins can become expensive. Pros sometimes denormalize deliberately: they store a computed order_total on the order, or copy price_at_purchase into the order line. The second one is actually required: when a product's price changes later, past orders must keep the price the customer actually paid.

The rule: normalize first, denormalize with a reason, and document it.

Keys: natural vs surrogate

  • Natural key: a real-world value like an email or ISBN. It's meaningful, but it can change.
  • Surrogate key: an artificial id (an auto-increment integer or a UUID). It's stable, so it's the usual choice.

Use a surrogate primary key, and add UNIQUE constraints on natural keys.

A design process you can follow

  1. List the nouns in the requirements → candidate tables (User, Course, Lesson, Enrollment).
  2. List each table's attributes → columns with types.
  3. Find the relationships and their cardinality → foreign keys / junction tables.
  4. Add constraints: NOT NULL, UNIQUE, CHECK, FKs.
  5. Write the queries the app needs and add indexes for them.
  6. Draw an ER diagram (dbdiagram.io and drawSQL are great free tools).

Naming conventions

  • Plural or singular table names: pick one and be consistent (customers or customer).
  • snake_case columns; id as PK; <table>_id for FKs (customer_id).
  • Timestamps: created_at, updated_at; booleans: is_active, has_paid.

Capstone practice

Design a schema for this learning website: users, tracks, lessons (in order, within levels), lesson completions, and quiz attempts with scores. Create the tables with proper keys and constraints, insert sample data, then write a query showing each user's completion percentage per track.

SQL · editable
✎ Check your understanding

1.Students can take many courses and courses have many students. How do you model it?

2.Why store price_at_purchase on an order line even though products has a price?

← Indexes and transactions