Normalization

Updated · 9 min read
mediumPostgreSQL 18sqlnormalizationnormal-forms3nfanomaliesinterview

Normalization in SQL is the process of organizing tables so that each fact is stored in exactly one place. It is used to prevent anomalies: data that becomes contradictory when rows are inserted, updated or deleted. The rules are written as a series of normal forms, each fixing a kind of repetition that the one before allows. In this lesson, we will start from a spreadsheet that records fees the way many small programs do, watch it go wrong in three ways, and then normalize it step by step into the Fee Tracker's design.

In One Minute

  • A table that repeats facts suffers anomalies: updates that miss copies, inserts that can't happen, deletes that lose facts.
  • 1NF: each cell holds one value; no lists or repeating column groups.
  • 2NF: every non-key column depends on the whole key, not part of it.
  • 3NF: no non-key column depends on another non-key column.
  • BCNF: every column that determines others is a key. Most well-designed tables are in BCNF.

The Messy Spreadsheet

Here is a fee spreadsheet, one row per payment, as it might be kept before a database:

SQL▶ Try it
CREATE TABLE fee_sheet (
  student_name TEXT, student_city TEXT, student_state TEXT,
  course_title TEXT, course_fee NUMERIC(8,2),
  paid_on DATE, amount_usd NUMERIC(8,2), method TEXT
);
INSERT INTO fee_sheet VALUES
  ('Srihitha Anamaneni', 'San Francisco', 'CA', 'Java Basics',       450.00, '2025-08-28', 450.00, 'card'),
  ('Srihitha Anamaneni', 'San Francisco', 'CA', 'Java OOP',          500.00, '2025-10-30', 250.00, 'card'),
  ('Srihitha Anamaneni', 'San Francisco', 'CA', 'Java OOP',          500.00, '2025-12-03', 250.00, 'check'),
  ('Navika Anamaneni',   'Dallas',        'TX', 'JavaScript Basics', 450.00, '2025-11-14', 225.00, 'card'),
  ('Navika Anamaneni',   'Dallas',        'TX', 'JavaScript Basics', 450.00, '2025-12-19', 225.00, 'check');
 
SELECT * FROM fee_sheet ORDER BY student_name, paid_on;
Output
CREATE TABLE
 
INSERT 0 5
 
    student_name    | student_city  | student_state |   course_title    | course_fee |  paid_on   | amount_usd | method
--------------------+---------------+---------------+-------------------+------------+------------+------------+--------
 Navika Anamaneni   | Dallas        | TX            | JavaScript Basics |     450.00 | 2025-11-14 |     225.00 | card
 Navika Anamaneni   | Dallas        | TX            | JavaScript Basics |     450.00 | 2025-12-19 |     225.00 | check
 Srihitha Anamaneni | San Francisco | CA            | Java Basics       |     450.00 | 2025-08-28 |     450.00 | card
 Srihitha Anamaneni | San Francisco | CA            | Java OOP          |     500.00 | 2025-10-30 |     250.00 | card
 Srihitha Anamaneni | San Francisco | CA            | Java OOP          |     500.00 | 2025-12-03 |     250.00 | check
(5 rows)

Explanation: It looks tidy, but Srihitha's city appears three times, Java OOP's fee twice, and Navika's city twice. Every repeated value is a copy that must be kept in step with the others.

Three Anomalies

An update anomaly: changing a fact in one row leaves its copies unchanged. Navika moves to Austin, and only one of her rows is updated:

SQL▶ Try it
CREATE TABLE fee_sheet (student_name TEXT, student_city TEXT, student_state TEXT, course_title TEXT, course_fee NUMERIC(8,2), paid_on DATE, amount_usd NUMERIC(8,2), method TEXT);
INSERT INTO fee_sheet VALUES ('Srihitha Anamaneni','San Francisco','CA','Java Basics',450.00,'2025-08-28',450.00,'card'), ('Srihitha Anamaneni','San Francisco','CA','Java OOP',500.00,'2025-10-30',250.00,'card'), ('Srihitha Anamaneni','San Francisco','CA','Java OOP',500.00,'2025-12-03',250.00,'check'), ('Navika Anamaneni','Dallas','TX','JavaScript Basics',450.00,'2025-11-14',225.00,'card'), ('Navika Anamaneni','Dallas','TX','JavaScript Basics',450.00,'2025-12-19',225.00,'check');
 
UPDATE fee_sheet SET student_city = 'Austin'
WHERE student_name = 'Navika Anamaneni' AND paid_on = '2025-11-14';
 
SELECT DISTINCT student_name, student_city
FROM fee_sheet
WHERE student_name LIKE 'Navika%'
ORDER BY student_city;
Output
CREATE TABLE
 
INSERT 0 5
 
UPDATE 1
 
   student_name   | student_city
------------------+--------------
 Navika Anamaneni | Austin
 Navika Anamaneni | Dallas
(2 rows)

Explanation: The database now says Navika lives in two cities at once. The same happens to a course fee changed in one row but not the other.

An insert anomaly: a fact can't be recorded without an unrelated one. Git Basics exists and costs $250.00, but nobody has paid for it, so there's no payment row to put its fee in.

A delete anomaly: deleting one fact destroys another. If Srihitha's Java Basics payment is deleted, the spreadsheet no longer records that Java Basics costs $450.00 at all, because that was its only row.

An update that misses a copy, a course that can't be added without a payment, and a delete that loses a course fee

First Normal Form (1NF)

A table is in 1NF when every cell holds a single value and there are no repeating groups of columns. A row with courses = 'Java Basics, Java OOP', or columns payment1, payment2, payment3, breaks 1NF. The fee sheet already has one value per cell, so it's in 1NF, but that alone doesn't prevent the anomalies above.

Second Normal Form (2NF)

A table is in 2NF when it's in 1NF and every non-key column depends on the whole primary key, not just part of it. This matters for composite keys. Suppose the key of a row is (student, course, paid on). Then student_city depends only on the student, and course_fee only on the course: each depends on part of the key. That partial dependency is why they repeat.

The fix is to move each fact to a table keyed by what it depends on:

New tableKeyFacts
studentsstudentcity, state
coursescoursefee
paymentspaymentstudent, course, paid on, amount, method

Third Normal Form (3NF)

A table is in 3NF when it's in 2NF and no non-key column depends on another non-key column. In the students table, time_zone would depend on (city, state), not on the student: everyone in San Francisco shares a time zone. Storing it per student repeats it. So the Fee Tracker puts it in a cities table, keyed by (city, state).

A popular summary: every non-key column must depend on the key, the whole key, and nothing but the key. "The key" is 1NF's identity, "the whole key" is 2NF, and "nothing but the key" is 3NF.

Boyce-Codd Normal Form (BCNF)

A table is in BCNF when every column (or set of columns) that determines another column is a candidate key. It's a slightly stricter 3NF, and differs from it only in unusual tables with several overlapping candidate keys. In practice, a design in 3NF with sensible keys is almost always in BCNF too, and the Fee Tracker is.

FormRuleRemoves
1NFOne value per cell, no repeating groupsLists in cells
2NFNon-key columns depend on the whole keyFacts about part of a composite key
3NFNon-key columns depend only on the keyFacts about other non-key columns
BCNFEvery determinant is a candidate keyRare overlapping-key cases
fee_sheet splits into students, cities, courses, enrollments and payments, each fact in one place
Three Steps Up the Ladder

Common Mistakes

Stopping at 1NF. One value per cell doesn't stop repetition; check 2NF and 3NF too.

Storing derived facts. A student's total paid repeated in students is a copy of the payments; compute it, or see Denormalization for doing it on purpose.

Normalizing values that aren't facts. A payment's amount_usd isn't a copy of the course fee; installments differ, so it belongs in payments.

Splitting too far. Separate tables for values that never repeat, like a student's first name, add joins without benefit.

Interview Corner

What is normalization?

Organizing tables so each fact is stored once, to prevent insert, update and delete anomalies. It's described by normal forms: 1NF, 2NF, 3NF and BCNF.

Explain 1NF, 2NF and 3NF.

1NF: atomic values, no repeating groups. 2NF: no non-key column depends on only part of a composite key. 3NF: no non-key column depends on another non-key column.

What is an update anomaly?

When a fact stored in several rows is changed in some but not all of them, leaving the data contradictory, like a student living in two cities.

Practice

Challenge (chapter checkpoint): Normalize the fee sheet into its students and courses. Write two queries over fee_sheet: one listing each distinct student with their city and state, and one listing each distinct course with its fee. Each fact should appear once.

Show solution
SQL▶ Try it
CREATE TABLE fee_sheet (student_name TEXT, student_city TEXT, student_state TEXT, course_title TEXT, course_fee NUMERIC(8,2), paid_on DATE, amount_usd NUMERIC(8,2), method TEXT);
INSERT INTO fee_sheet VALUES ('Srihitha Anamaneni','San Francisco','CA','Java Basics',450.00,'2025-08-28',450.00,'card'), ('Srihitha Anamaneni','San Francisco','CA','Java OOP',500.00,'2025-10-30',250.00,'card'), ('Srihitha Anamaneni','San Francisco','CA','Java OOP',500.00,'2025-12-03',250.00,'check'), ('Navika Anamaneni','Dallas','TX','JavaScript Basics',450.00,'2025-11-14',225.00,'card'), ('Navika Anamaneni','Dallas','TX','JavaScript Basics',450.00,'2025-12-19',225.00,'check');
 
SELECT DISTINCT student_name, student_city, student_state
FROM fee_sheet
ORDER BY student_name;
 
SELECT DISTINCT course_title, course_fee
FROM fee_sheet
ORDER BY course_title;
Output
CREATE TABLE
 
INSERT 0 5
 
    student_name    | student_city  | student_state
--------------------+---------------+---------------
 Navika Anamaneni   | Dallas        | TX
 Srihitha Anamaneni | San Francisco | CA
(2 rows)
 
   course_title    | course_fee
-------------------+------------
 Java Basics       |     450.00
 Java OOP          |     500.00
 JavaScript Basics |     450.00
(3 rows)

Two students instead of five copies, three courses instead of five fee cells. These results become the students and courses tables, each with a primary key; the payments keep only references (student_id, course_id) plus their own facts (date, amount, method). Step by step: 1NF was already met; 2NF moved student and course facts out of the payment rows; 3NF moved time zones into cities. The result is the Fee Tracker's design.

Quick check: Normalization

3 questions
1Navika's city is updated in one of her two rows. Which anomaly is this?
2A table's key is (student, course, paid_on), and it stores the course fee. Which normal form does that break?
3students stores time_zone, which depends on (city, state). Which normal form does that break?

Key Takeaways

Normal formAsk
1NFDoes any cell hold a list?
2NFDoes any column depend on only part of a composite key?
3NFDoes any column depend on another non-key column?
BCNFDoes anything other than a key determine other columns?

Was this tutorial helpful?

Lesson 72 of 115