TalentJR

🗄️ SQL for beginners · Class 12 · Lesson 15 of 18 · Free

Database Normalization in SQL – 1NF, 2NF and 3NF Step by Step

⏱ About 40 minutes · ▶ run real SQL here — nothing to install · no sign-in

In this lesson you will

  • Spot repeated data and lists in a cell, and explain the problems they cause
  • Turn a messy table into 1NF, 2NF and 3NF tables
  • Rebuild the full picture with JOINs from the normalized tables

The school office kept admissions in one big sheet: name, section, class teacher and all subjects in one cell. Then a class teacher changed, a name was misspelt in one row, and a search for 'Maths' found the wrong students. Normalization is the step-by-step way to fix such tables so each fact is stored once.

What is database normalization in SQL?

Normalization means organising tables so that each fact is stored in one place only. It follows a few rules called normal forms: 1NF, 2NF and 3NF. Each one fixes one kind of problem.

Look at this admissions table. The subjects column holds a list. Mrs. Kulkarni's name is typed again for every 12-A student. That design invites mistakes.

The second query looks for Maths students with LIKE (Lesson 2). It also finds Sara, who takes Applied Maths, not Maths. Try it: change '%Maths%' to '%Physics%' and run again.

📋 Tables in this example: admissions_raw — tap to see how they are made
CREATE TABLE admissions_raw (
  roll_no       INTEGER,
  student_name  TEXT,
  section       TEXT,
  class_teacher TEXT,
  subjects      TEXT
);
INSERT INTO admissions_raw VALUES
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Maths, Physics, Computer Science'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'Maths, English'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Physics, Chemistry, Applied Maths'),
 (4, 'Kabir Singh', '12-B', 'Mr. Rao',       'English');
🗄️ SQL — you can edit this query
Result
┌─────────┬──────────────┬─────────┬───────────────┬───────────────────────────────────┐
│ roll_no │ student_name │ section │ class_teacher │ subjects                          │
├─────────┼──────────────┼─────────┼───────────────┼───────────────────────────────────┤
│ 1       │ Riya Sharma  │ 12-A    │ Mrs. Kulkarni │ Maths, Physics, Computer Science  │
│ 2       │ Aman Verma   │ 12-A    │ Mrs. Kulkarni │ Maths, English                    │
│ 3       │ Sara Khan    │ 12-B    │ Mr. Rao       │ Physics, Chemistry, Applied Maths │
│ 4       │ Kabir Singh  │ 12-B    │ Mr. Rao       │ English                           │
└─────────┴──────────────┴─────────┴───────────────┴───────────────────────────────────┘
(4 rows)

┌──────────────┐
│ student_name │
├──────────────┤
│ Riya Sharma  │
│ Aman Verma   │
│ Sara Khan    │
└──────────────┘
(3 rows)

What is First Normal Form (1NF)?

1NF has two rules. Each cell holds one single value, not a list. And each row can be told apart by a key.

So the subjects list becomes one row per student per subject. The key is now (roll_no, subject), as in the marks table from Lesson 5.

Now plain SQL works: WHERE subject = 'Maths' is exact, and GROUP BY (Lesson 8) can count students per subject. Try it: change the first query to subject = 'Applied Maths'.

📋 Tables in this example: student_subjects — tap to see how they are made
CREATE TABLE student_subjects (
  roll_no       INTEGER NOT NULL,
  student_name  TEXT NOT NULL,
  section       TEXT NOT NULL,
  class_teacher TEXT NOT NULL,
  subject       TEXT NOT NULL,
  PRIMARY KEY (roll_no, subject)
);
INSERT INTO student_subjects VALUES
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Maths'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Physics'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Computer Science'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'Maths'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'English'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Physics'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Chemistry'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Applied Maths'),
 (4, 'Kabir Singh', '12-B', 'Mr. Rao',       'English');
🗄️ SQL — you can edit this query
Result
┌──────────────┐
│ student_name │
├──────────────┤
│ Aman Verma   │
│ Riya Sharma  │
└──────────────┘
(2 rows)

┌──────────────────┬──────────┐
│ subject          │ students │
├──────────────────┼──────────┤
│ Applied Maths    │ 1        │
│ Chemistry        │ 1        │
│ Computer Science │ 1        │
│ English          │ 2        │
│ Maths            │ 2        │
│ Physics          │ 2        │
└──────────────────┴──────────┘
(6 rows)

Why is repeated data a problem? (update anomaly)

The 1NF table still repeats each student's name, section and class teacher in every row. If one copy changes and the others don't, the table now disagrees with itself. This is called an update anomaly.

Below, the office fixes Riya's name in her Maths row only. Ask the table for her name, and you get two answers. Which one is right?

Try it: remove the AND subject = 'Maths' part and run again. Now all rows change, but only because you remembered to.

📋 Tables in this example: student_subjects — tap to see how they are made
CREATE TABLE student_subjects (
  roll_no       INTEGER NOT NULL,
  student_name  TEXT NOT NULL,
  section       TEXT NOT NULL,
  class_teacher TEXT NOT NULL,
  subject       TEXT NOT NULL,
  PRIMARY KEY (roll_no, subject)
);
INSERT INTO student_subjects VALUES
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Maths'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Physics'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Computer Science'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'Maths'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'English'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Physics'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Chemistry'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Applied Maths'),
 (4, 'Kabir Singh', '12-B', 'Mr. Rao',       'English');
🗄️ SQL — you can edit this query
Result
1 row updated.

┌────────────────┐
│ student_name   │
├────────────────┤
│ Riya Sharma    │
│ Riya S. Sharma │
└────────────────┘
(2 rows)

What is Second Normal Form (2NF) in SQL?

2NF = 1NF, plus: every non-key column must depend on the WHOLE key. Here the key is (roll_no, subject). But the student's name depends only on roll_no, which is part of the key. That is a partial dependency.

The fix is to split. Facts about a student go into students, keyed by roll_no. The pairs (roll_no, subject) go into enrolments. INSERT … SELECT DISTINCT copies the data across, one row per student.

Now each name is stored once, so the update anomaly cannot happen. Try it: run SELECT * FROM enrolments; at the end too.

📋 Tables in this example: student_subjects — tap to see how they are made
CREATE TABLE student_subjects (
  roll_no       INTEGER NOT NULL,
  student_name  TEXT NOT NULL,
  section       TEXT NOT NULL,
  class_teacher TEXT NOT NULL,
  subject       TEXT NOT NULL,
  PRIMARY KEY (roll_no, subject)
);
INSERT INTO student_subjects VALUES
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Maths'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Physics'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Computer Science'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'Maths'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'English'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Physics'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Chemistry'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Applied Maths'),
 (4, 'Kabir Singh', '12-B', 'Mr. Rao',       'English');
🗄️ SQL — you can edit this query
Result
Table students created.

Table enrolments created.

4 rows inserted.

9 rows inserted.

┌─────────┬──────────────┬─────────┬───────────────┐
│ roll_no │ student_name │ section │ class_teacher │
├─────────┼──────────────┼─────────┼───────────────┤
│ 1       │ Riya Sharma  │ 12-A    │ Mrs. Kulkarni │
│ 2       │ Aman Verma   │ 12-A    │ Mrs. Kulkarni │
│ 3       │ Sara Khan    │ 12-B    │ Mr. Rao       │
│ 4       │ Kabir Singh  │ 12-B    │ Mr. Rao       │
└─────────┴──────────────┴─────────┴───────────────┘
(4 rows)

What is Third Normal Form (3NF) with an example?

3NF = 2NF, plus: a non-key column must not depend on another non-key column. In students, class_teacher depends on section, not on the student. That chain (roll_no → section → class teacher) is a transitive dependency.

So sections get their own table. Students keep only the section, as a foreign key (Lesson 5). Now a new class teacher for 12-B is ONE update, and every 12-B student sees it.

The JOIN (Lesson 7) rebuilds the old full picture whenever you need it. Try it: change 'Ms. Fernandes' to your own name and run again.

📋 Tables in this example: sections, students, enrolments — tap to see how they are made
CREATE TABLE sections (
  section       TEXT PRIMARY KEY,
  class_teacher TEXT NOT NULL
);
CREATE TABLE students (
  roll_no INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  section TEXT NOT NULL REFERENCES sections(section)
);
CREATE TABLE enrolments (
  roll_no INTEGER NOT NULL REFERENCES students(roll_no),
  subject TEXT NOT NULL,
  PRIMARY KEY (roll_no, subject)
);
INSERT INTO sections VALUES ('12-A', 'Mrs. Kulkarni'), ('12-B', 'Mr. Rao');
INSERT INTO students VALUES (1, 'Riya Sharma', '12-A'), (2, 'Aman Verma', '12-A'), (3, 'Sara Khan', '12-B'), (4, 'Kabir Singh', '12-B');
INSERT INTO enrolments VALUES (1, 'Maths'), (1, 'Physics'), (1, 'Computer Science'), (2, 'Maths'), (2, 'English'),
 (3, 'Physics'), (3, 'Chemistry'), (3, 'Applied Maths'), (4, 'English');
🗄️ SQL — you can edit this query
Result
1 row updated.

┌─────────────┬─────────┬───────────────┬──────────────────┐
│ name        │ section │ class_teacher │ subject          │
├─────────────┼─────────┼───────────────┼──────────────────┤
│ Riya Sharma │ 12-A    │ Mrs. Kulkarni │ Computer Science │
│ Riya Sharma │ 12-A    │ Mrs. Kulkarni │ Maths            │
│ Riya Sharma │ 12-A    │ Mrs. Kulkarni │ Physics          │
│ Aman Verma  │ 12-A    │ Mrs. Kulkarni │ English          │
│ Aman Verma  │ 12-A    │ Mrs. Kulkarni │ Maths            │
│ Sara Khan   │ 12-B    │ Ms. Fernandes │ Applied Maths    │
│ Sara Khan   │ 12-B    │ Ms. Fernandes │ Chemistry        │
│ Sara Khan   │ 12-B    │ Ms. Fernandes │ Physics          │
│ Kabir Singh │ 12-B    │ Ms. Fernandes │ English          │
└─────────────┴─────────┴───────────────┴──────────────────┘
(9 rows)

💡 The setup builds the final 3NF tables: sections, students and enrolments.

🤔 Predict the output

Think like SQL: read the code and choose what it prints. You get the reason straight away.

Question 1. The messy table had 4 rows with subject lists. How many rows does the 1NF table have?

📋 The tables (tap to see them)
CREATE TABLE student_subjects (
  roll_no       INTEGER NOT NULL,
  student_name  TEXT NOT NULL,
  section       TEXT NOT NULL,
  class_teacher TEXT NOT NULL,
  subject       TEXT NOT NULL,
  PRIMARY KEY (roll_no, subject)
);
INSERT INTO student_subjects VALUES
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Maths'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Physics'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Computer Science'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'Maths'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'English'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Physics'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Chemistry'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Applied Maths'),
 (4, 'Kabir Singh', '12-B', 'Mr. Rao',       'English');
SELECT COUNT(*) FROM student_subjects;

Question 2. How many different class teachers does 12-B have now, according to this table?

📋 The tables (tap to see them)
CREATE TABLE student_subjects (
  roll_no       INTEGER NOT NULL,
  student_name  TEXT NOT NULL,
  section       TEXT NOT NULL,
  class_teacher TEXT NOT NULL,
  subject       TEXT NOT NULL,
  PRIMARY KEY (roll_no, subject)
);
INSERT INTO student_subjects VALUES
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Maths'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Physics'),
 (1, 'Riya Sharma', '12-A', 'Mrs. Kulkarni', 'Computer Science'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'Maths'),
 (2, 'Aman Verma',  '12-A', 'Mrs. Kulkarni', 'English'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Physics'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Chemistry'),
 (3, 'Sara Khan',   '12-B', 'Mr. Rao',       'Applied Maths'),
 (4, 'Kabir Singh', '12-B', 'Mr. Rao',       'English');
UPDATE student_subjects SET class_teacher = 'Ms. Fernandes'
WHERE roll_no = 3 AND subject = 'Physics';

SELECT COUNT(DISTINCT class_teacher)
FROM student_subjects
WHERE section = '12-B';

Question 3. In the 3NF tables, who has Ms. Fernandes as class teacher now?

📋 The tables (tap to see them)
CREATE TABLE sections (
  section       TEXT PRIMARY KEY,
  class_teacher TEXT NOT NULL
);
CREATE TABLE students (
  roll_no INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  section TEXT NOT NULL REFERENCES sections(section)
);
CREATE TABLE enrolments (
  roll_no INTEGER NOT NULL REFERENCES students(roll_no),
  subject TEXT NOT NULL,
  PRIMARY KEY (roll_no, subject)
);
INSERT INTO sections VALUES ('12-A', 'Mrs. Kulkarni'), ('12-B', 'Mr. Rao');
INSERT INTO students VALUES (1, 'Riya Sharma', '12-A'), (2, 'Aman Verma', '12-A'), (3, 'Sara Khan', '12-B'), (4, 'Kabir Singh', '12-B');
INSERT INTO enrolments VALUES (1, 'Maths'), (1, 'Physics'), (1, 'Computer Science'), (2, 'Maths'), (2, 'English'),
 (3, 'Physics'), (3, 'Chemistry'), (3, 'Applied Maths'), (4, 'English');
UPDATE sections SET class_teacher = 'Ms. Fernandes'
WHERE section = '12-B';

SELECT st.name
FROM students st
JOIN sections sec ON sec.section = st.section
WHERE sec.class_teacher = 'Ms. Fernandes'
ORDER BY st.name;

🧩 Your challenge

The library's loans_raw table repeats member and book details in every loan. Normalize it into members(member_id, name, class_section), books(isbn, title, author) and loans(member_id, isbn), copy the data with INSERT … SELECT DISTINCT, then list who borrowed what. Expected: Dev Patil – Physics Part 1, Meera Iyer – Short Stories for Teens, Riya Sharma – Physics Part 1, Riya Sharma – Python for Beginners.

Expected result :
Table members created.

Table books created.

Table loans created.

3 rows inserted.

3 rows inserted.

4 rows inserted.

┌─────────────┬─────────────────────────┐
│ name        │ title                   │
├─────────────┼─────────────────────────┤
│ Dev Patil   │ Physics Part 1          │
│ Meera Iyer  │ Short Stories for Teens │
│ Riya Sharma │ Physics Part 1          │
│ Riya Sharma │ Python for Beginners    │
└─────────────┴─────────────────────────┘
(4 rows)
📋 Tables in this example: loans_raw — tap to see how they are made
CREATE TABLE loans_raw (
  member_id    INTEGER,
  member_name  TEXT,
  member_class TEXT,
  book_isbn    TEXT,
  book_title   TEXT,
  author       TEXT
);
INSERT INTO loans_raw VALUES
 (101, 'Riya Sharma', '12-A', '978-01', 'Physics Part 1', 'R. Mehta'),
 (101, 'Riya Sharma', '12-A', '978-02', 'Python for Beginners', 'S. Iyer'),
 (102, 'Dev Patil',   '11-A', '978-01', 'Physics Part 1', 'R. Mehta'),
 (103, 'Meera Iyer',  '11-B', '978-03', 'Short Stories for Teens', 'A. Nair');
🗄️ SQL — you can edit this query

⚠️ Common mistakes (and how to fix them)

❌ Wrong

SELECT student_name FROM admissions_raw
WHERE subjects LIKE '%Maths%'
ORDER BY student_name;

Runs, but gives the wrong result: ┌──────────────┐ │ student_name │ ├──────────────┤ │ Aman Verma │ │ Riya Sharma │ │ Sara Khan │ └──────────────┘ (3 rows)

✅ Right

SELECT student_name FROM student_subjects
WHERE subject = 'Maths'
ORDER BY student_name;

Searching inside a list with LIKE also matches 'Applied Maths'. With one subject per row (1NF), = finds exactly the right students.

❌ Wrong

CREATE TABLE students (
  roll_no      INTEGER PRIMARY KEY,
  student_name TEXT NOT NULL
);
INSERT INTO students
SELECT roll_no, student_name FROM student_subjects;

Error in statement 2 (INSERT): UNIQUE constraint failed: students.roll_no

✅ Right

CREATE TABLE students (
  roll_no      INTEGER PRIMARY KEY,
  student_name TEXT NOT NULL
);
INSERT INTO students
SELECT DISTINCT roll_no, student_name FROM student_subjects;

SELECT COUNT(*) FROM students;

The 1NF table has one row per subject, so roll numbers repeat and the primary key refuses the copies. DISTINCT keeps one row per student.

❌ Wrong

INSERT INTO enrolments VALUES (5, 'Maths');
INSERT INTO students VALUES (5, 'Dev Patil', '12-A');

Error in statement 1 (INSERT): FOREIGN KEY constraint failed

✅ Right

INSERT INTO students VALUES (5, 'Dev Patil', '12-A');
INSERT INTO enrolments VALUES (5, 'Maths');

After splitting, enrolments points to students with a foreign key. Insert the student first, then the enrolment.

📌 Remember

  • Normalization stores each fact once, so data cannot disagree with itself.
  • 1NF: one value per cell (no lists) and a key for every row.
  • 2NF: every non-key column depends on the whole key, so split out partial dependencies.
  • 3NF: no non-key column depends on another non-key column, so split out chains like section → teacher.
  • JOINs and views rebuild the full picture from the clean tables.

Finished the quiz and the challenge?

Questions students ask

What is the difference between 2NF and 3NF?

2NF removes columns that depend on only part of a composite key. 3NF removes columns that depend on another non-key column. Both fix repeated data, just of different kinds.

Is there a 4NF and BCNF?

Yes. BCNF (Boyce-Codd Normal Form) is a slightly stricter 3NF, and 4NF and 5NF handle rarer cases. For most school and business apps, reaching 3NF is the usual goal.

What is denormalization?

It means repeating some data on purpose to make reads faster or simpler, for example storing a total in a report table. It is a trade-off: you must then keep the copies in step, often with triggers (Lesson 14).

Does normalization make queries slower?

Reads need more JOINs, which have a small cost, but indexes on keys (Lesson 11) keep them fast. Writes become simpler and safer, because each fact is changed in one place.