Normalization
Overview
You’ve seen that Bean & Leaf spreads its data over six tables, and that joins bring it back together. Why not keep everything in one big table, like a spreadsheet? Normalization is the answer — a set of design principles, first described by E. F. Codd, for organising tables so that each fact is stored once. It sounds academic, but its purpose is practical: a normalised database can’t contradict itself.
The trouble with one wide table
Let’s build the spreadsheet version of the shop: one row per order line, with the customer and product details copied in:
CREATE TABLE flat_sales AS
SELECT o.id AS order_id, o.ordered_at::date AS day,
c.email AS customer_email, c.first_name || ' ' || c.last_name AS customer, c.city,
p.name AS product, cat.name AS category, oi.quantity, oi.unit_price
FROM orders AS o
JOIN customers AS c ON c.id = o.customer_id
JOIN order_items AS oi ON oi.order_id = o.id
JOIN products AS p ON p.id = oi.product_id
LEFT JOIN categories AS cat ON cat.id = p.category_id;
SELECT order_id, customer, city, product, category, quantity
FROM flat_sales
WHERE customer_email = 'asha.rao@example.com'
ORDER BY order_id, product;SELECT 42
order_id | customer | city | product | category | quantity
----------+----------+------+------------------------+-----------+----------
1 | Asha Rao | Pune | Assam Breakfast | Tea | 1
1 | Asha Rao | Pune | Ethiopian Yirgacheffe | Coffee | 2
3 | Asha Rao | Pune | Darjeeling First Flush | Tea | 1
9 | Asha Rao | Pune | Electric Kettle | Equipment | 1
9 | Asha Rao | Pune | Jasmine Green | Tea | 1
17 | Asha Rao | Pune | Ethiopian Yirgacheffe | Coffee | 3
23 | Asha Rao | Pune | Assam Breakfast | Tea | 2
23 | Asha Rao | Pune | Ethiopian Yirgacheffe | Coffee | 2
(8 rows)Easy to read — and full of repetition. Asha’s name and city appear on eight rows; “Ethiopian Yirgacheffe is a Coffee” appears on every line that sold it. That repetition causes three kinds of problem, called anomalies.
Update anomaly. Asha moves to Mumbai. Someone updates “her” rows — but only the ones for this year:
UPDATE flat_sales
SET city = 'Mumbai'
WHERE customer_email = 'asha.rao@example.com' AND day >= '2026-06-01';
SELECT DISTINCT customer, city
FROM flat_sales
WHERE customer_email = 'asha.rao@example.com'
ORDER BY city;UPDATE 3
customer | city
----------+--------
Asha Rao | Mumbai
Asha Rao | Pune
(2 rows)Now the data contradicts itself: where does Asha live? Every copy of a fact is a chance for the copies to disagree.
Insert anomaly. A new customer signs up but hasn’t ordered yet. There’s no row to put them on — every row is an order line. The same goes for a new product nobody has bought.
Delete anomaly. Delete the only order line for the Decaf House Blend, and the shop forgets the product existed at all:
DELETE FROM flat_sales WHERE product = 'Decaf House Blend';
SELECT count(*) AS decaf_rows FROM flat_sales WHERE product = 'Decaf House Blend';DELETE 1
decaf_rows
------------
0
(1 row)In the real, normalised shop, none of these can happen: Asha’s city is stored once in customers, customers exist without orders, and deleting an order line leaves the product alone.
The normal forms, in plain language
Database theory defines a series of normal forms. The first three cover almost everything you’ll need in practice.
First normal form (1NF): one value per cell
Each column holds a single value — no lists, no repeating groups of columns. These break 1NF:
- a
tagscolumn containing'gift,electric,sale'; - columns
phone1,phone2,phone3.
A list in a cell can’t be indexed, constrained or joined, and every query has to pick it apart:
SELECT name
FROM (VALUES ('Kettle', 'gift,electric'), ('Teapot', 'glass,gift'), ('Grinder', 'electric'))
AS t(name, tags)
WHERE tags LIKE '%gift%'; name
--------
Kettle
Teapot
(2 rows)That works until someone adds a tag called giftcard. The normalised design is a separate table with one row per product–tag pair, which you can query exactly: WHERE tag = 'gift'. (PostgreSQL’s array and jsonb types are a deliberate, well-supported exception — more in Working with JSON.)
Second normal form (2NF): depend on the whole key
This applies to tables with a composite key. Every non-key column must depend on the whole key, not just part of it. If order_items (key: order_id, product_id) also had a product_name column, that name would depend only on product_id — so it would be repeated on every line that sold the product. It belongs in products.
Third normal form (3NF): nothing but the key
Every non-key column must describe the key directly, not via another column. In a customers table with city and country, the country really depends on the city (Pune is always in India), not on the customer. Strictly, that’s a 3NF violation, and a fully normalised design would have a cities table.
The classic summary: every column must depend on the key, the whole key, and nothing but the key.
How far to go
Database designers usually aim for 3NF, then make pragmatic exceptions. Bean & Leaf keeps city and country together on customers, because a separate cities table would add a join to every address query for little benefit, and addresses are often entered free-form anyway. Normalization is a guide for spotting repetition that will hurt, not a rule to follow to the last column.
Normalising: split, then link
Fixing a denormalised table means splitting it: move each repeated group of facts into its own table with its own key, and keep a foreign key where the data used to be. Here’s how you’d pull the products out of flat_sales:
CREATE TABLE sale_products (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL UNIQUE,
category text
);
INSERT INTO sale_products (name, category)
SELECT DISTINCT product, category
FROM flat_sales
ORDER BY product;
SELECT * FROM sale_products ORDER BY id;CREATE TABLE
INSERT 0 10
id | name | category
----+------------------------+-----------
1 | Assam Breakfast | Tea
2 | Burr Grinder | Equipment
3 | Chamomile Calm | Tea
4 | Colombian Supremo | Coffee
5 | Darjeeling First Flush | Tea
6 | Electric Kettle | Equipment
7 | Ethiopian Yirgacheffe | Coffee
8 | Gift Card |
9 | Jasmine Green | Tea
10 | Pour-Over Dripper | Equipment
(10 rows)SELECT DISTINCT collapses the repetition to one row per product, and the UNIQUE constraint guarantees it stays that way. The next step would add a product_id column to the sales table, fill it with UPDATE … FROM, and drop the copied columns — you’ll see ALTER TABLE for that in the next lesson.
Deliberate denormalization
Sometimes you want a copy, on purpose:
- History.
order_items.unit_pricecopies the product’s price at the time of the sale. That’s not repetition — it’s a different fact (“what this customer paid”), and it must not change when today’s price does. - Performance and reporting. Analytical databases and data warehouses often use wide, denormalised tables, because they’re read far more than written and joins over billions of rows are expensive.
- Cached totals. An
orders.items_totalcolumn saves summing the items every time — at the cost of keeping it in sync (with a trigger, or in the same transaction that changes the items).
The difference between good and bad denormalization is intent: you know exactly which copy is the source of truth and how the others are kept up to date.
Tip: a quick smell test for any table design — if you’d have to update more than one row to change one real-world fact, the design is repeating that fact.
Split a spreadsheet into tables
The starter creates workshop_signups, a flat table copied from a spreadsheet: every signup repeats the attendee's email and name. Normalise the attendees out of it. Create a table attendees with id (integer identity primary key), email (text, required, unique) and name (text, required). Fill it with one row per distinct attendee using INSERT … SELECT DISTINCT, ordered by email so the ids are predictable. Then write a query that joins workshop_signups to attendees on email and returns each attendee's id, name and number of signups, ordered by id.
CREATE TABLE workshop_signups (
signup_id integer PRIMARY KEY,
attendee_email text NOT NULL,
attendee_name text NOT NULL,
workshop text NOT NULL
);
INSERT INTO workshop_signups VALUES
(1, 'mira@example.com', 'Mira Shah', 'Latte Art'),
(2, 'leo@example.com', 'Leo Park', 'Latte Art'),
(3, 'mira@example.com', 'Mira Shah', 'Tea Tasting'),
(4, 'ana@example.com', 'Ana Silva', 'Home Roasting'),
(5, 'mira@example.com', 'Mira Shah', 'Home Roasting');
-- 1. the attendees table 2. fill it 3. signups per attendeeCREATE TABLE workshop_signups (
signup_id integer PRIMARY KEY,
attendee_email text NOT NULL,
attendee_name text NOT NULL,
workshop text NOT NULL
);
INSERT INTO workshop_signups VALUES
(1, 'mira@example.com', 'Mira Shah', 'Latte Art'),
(2, 'leo@example.com', 'Leo Park', 'Latte Art'),
(3, 'mira@example.com', 'Mira Shah', 'Tea Tasting'),
(4, 'ana@example.com', 'Ana Silva', 'Home Roasting'),
(5, 'mira@example.com', 'Mira Shah', 'Home Roasting');
CREATE TABLE attendees (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL UNIQUE,
name text NOT NULL
);
INSERT INTO attendees (email, name)
SELECT DISTINCT attendee_email, attendee_name
FROM workshop_signups
ORDER BY attendee_email;
SELECT a.id, a.name, count(*) AS signups
FROM workshop_signups AS w
JOIN attendees AS a ON a.email = w.attendee_email
GROUP BY a.id, a.name
ORDER BY a.id;Summary
- Normalization means organising tables so each fact is stored in exactly one place.
- Repeated facts cause anomalies: updates that miss copies, data you can't insert, and facts lost when rows are deleted.
- 1NF: one value per cell, no lists or repeating column groups. 2NF and 3NF: every column depends on the key, the whole key, and nothing but the key.
- Normalising means splitting a wide table into narrower ones linked by keys; joins put them back together.
- Denormalise deliberately — for history (an order's unit_price), reporting or performance — never by accident.