Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»SQL»Normalization in DBMS: 1NF, 2NF and 3NF With SQL Examples
    SQL

    Normalization in DBMS: 1NF, 2NF and 3NF With SQL Examples

    Anirudh KhuranaBy Anirudh Khurana25 November 2026No Comments11 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Normalization in DBMS with Enrollments, Students, Courses and Departments linked by their keys
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Asha and Ravi signed up for the same SQL course. On Monday, Asha’s timetable says “SQL Basics” and Ravi’s still says “SQL”. Both show course C1. You work at the registration desk. Which timetable would you correct, and how would you stop this happening next term?

    Free PDF
    Normalization notes with SQL and practice questions
    7 pages. Read offline or print a copy.
    Download PDF
    Two paper timetables being compared at a college registration desk
    One course can appear on many timetables. Which record should own its title?

    Try the edit that caused the mix up

    The desk keeps a table, much like a spreadsheet. Each row says who enrolled in which course. C1 is a permanent course ID. The college can change its title without changing that ID. Start by saving a new title on Asha’s copy.

    Playground

    You run the registration desk

    Try each request at the desk. Then switch to linked tables and try the same requests again.

    One register
    StudentIDTimetable title
    AshaC1SQL
    AshaC2Python
    RaviC1SQL

    Did you spot the problem before switching designs? In the first register, saving the title only changed Asha’s row. Ravi could show you a different title for exactly the same ID. In the linked version, both timetables read the title from the same course record. That decision about where a fact lives is the point of normalization in DBMS. DBMS stands for database management system. SQLite is the one we will use below.

    Three requests a single sheet handles badly

    At the deskWhat goes wrong in one registerName for the problem
    Rename C1Two copies can disagree.Update anomaly
    Offer a new courseThere is nobody to put in the required student field yet.Insertion anomaly
    Remove the only Python enrollmentThe last record of Python disappears with it.Deletion anomaly

    An anomaly is an unwanted side effect of an ordinary change. Asha dropping Python should change her timetable. It should leave the college free to offer Python to someone else.

    Give each row a meaning before you give it a key

    On the original sign up sheet, Asha wrote “C1 SQL, C2 Python” in one box. How many people took SQL? Counting boxes will not answer that. You first have to open each list and read it.

    Write a separate row for each enrollment. Asha now gets two rows and Ravi gets one. Put the course ID in one column and its title in another. This is first normal form (1NF) in our example: one value per cell and no repeating course group. C1 can appear in two rows because those rows record two different enrollments.

    Guided walkthrough

    Rebuild the register

    Change one part of the design at a time. Watch for the step where the second copy of SQL disappears.

    Try this with the 1NF register. Cover the course column and look at Asha. Can you tell which enrollment you are holding? Now cover the student column and look at C1. Again, there are two possibilities. Uncover both and the pair identifies exactly one row.

    Which facts need the whole key?

    Use S1 for Asha and S2 for Ravi. People can share names, so a name makes a poor identifier. Here each student can enroll in a particular course once. That makes (student_id, course_id) a composite primary key. “Composite” means it has more than one column. A primary key is the identifier the designer chose for a row.

    Fact you wantWhat you need to knowWhere to store it
    A student’s nameS1 aloneStudents
    A course titleC1 aloneCourses
    Does Asha take SQL?S1 and C1 togetherEnrollments

    The rule “course ID determines course title” is a functional dependency. You can write it as C1’s column name, course_id, followed by an arrow to title. It is a rule of this college, even before you collect any rows. Two titles that happen to match today are no proof that they must match tomorrow.

    In the flat register the title depends on only part of the key. This is a partial dependency. Second normal form (2NF) rules that out for columns outside the candidate keys. A candidate key is any set of columns that uniquely identifies a row and loses that ability if you remove any member. Check every candidate key when there is more than one.

    Why the department name needs one more split

    The college asks you to record each student’s department. Asha and Ravi both belong to D1, Computing. Putting department_name in Students would put “Computing” back on two rows. Yet Students already has a primary key of just one column. There is no half of that key to remove.

    Trace the lookup. First, S1 tells you Asha belongs to D1. Then D1 tells you the department name. This indirect route is a transitive dependency. Put D1 and Computing together in Departments. Keep D1 in Students so that you can find Asha’s department again. That reference is a foreign key.

    Our four resulting tables are in third normal form (3NF) under these rules. If Computing becomes Computing Sciences, one edit to Departments is enough. Open the last step of the walkthrough to find that row.

    For an interview: what is the precise 3NF test?

    For every nontrivial dependency X determines A, either X is a superkey or A is part of a candidate key. Nontrivial means A is outside X. A superkey identifies a row and may contain extra columns. In Students before the split, department_id determines department_name, but it cannot identify one student, and department_name belongs to no candidate key.

    Check the split with a pencil before writing SQL

    Build Ravi’s timetable by hand. Start with the enrollment (S2, C1). Look up S2 in Students and read Ravi and D1. Look up C1 in Courses and read SQL. Finally, look up D1 in Departments and read Computing. One enrollment has led you back to one complete row.

    Keeping the matching IDs makes the split lossless: joining the pieces recovers the original facts without adding invented enrollments. Suppose you threw away Enrollments and kept only two student names and two course titles. How would you know whether Ravi took Python? Matching every student to every course would invent a fourth enrollment. The three pairs in Enrollments are data worth keeping.

    Run the register in SQLite

    Start an empty SQLite database and run these four examples in order in the same session. CREATE TABLE describes the columns. INSERT adds rows. SELECT reads them. The last line sorts the result so yours prints in the same order.

    PRAGMA foreign_keys = ON;
    CREATE TABLE register (
      student_id TEXT NOT NULL, name TEXT NOT NULL,
      course_id TEXT NOT NULL, title TEXT NOT NULL,
      dept_id TEXT NOT NULL, dept_name TEXT NOT NULL,
      PRIMARY KEY (student_id, course_id)
    );
    INSERT INTO register VALUES
      ('S1','Asha','C1','SQL','D1','Computing'),
      ('S1','Asha','C2','Python','D1','Computing'),
      ('S2','Ravi','C1','SQL','D1','Computing');
    SELECT name, title FROM register
    ORDER BY student_id, course_id;

    Output:

    name | title
    Asha | SQL
    Asha | Python
    Ravi | SQL

    Move each fact to its own table

    Create the parent tables before copying the enrollments. REFERENCES tells SQLite which IDs are allowed. SELECT DISTINCT takes one copy of each different fact. Our original titles agree. If your own register already has two titles for C1, confirm the right one before copying.

    CREATE TABLE departments (
      dept_id TEXT PRIMARY KEY NOT NULL, dept_name TEXT NOT NULL
    );
    CREATE TABLE students (
      student_id TEXT PRIMARY KEY NOT NULL, name TEXT NOT NULL,
      dept_id TEXT NOT NULL REFERENCES departments(dept_id)
    );
    CREATE TABLE courses (
      course_id TEXT PRIMARY KEY NOT NULL, title TEXT NOT NULL
    );
    CREATE TABLE enrollments (
      student_id TEXT NOT NULL REFERENCES students(student_id),
      course_id TEXT NOT NULL REFERENCES courses(course_id),
      PRIMARY KEY (student_id, course_id)
    );
    INSERT INTO departments SELECT DISTINCT dept_id, dept_name FROM register;
    INSERT INTO students SELECT DISTINCT student_id, name, dept_id FROM register;
    INSERT INTO courses SELECT DISTINCT course_id, title FROM register;
    INSERT INTO enrollments SELECT student_id, course_id FROM register;
    SELECT course_id, title FROM courses ORDER BY course_id;

    Output:

    course_id | title
    C1 | SQL
    C2 | Python

    Join the four tables and count the results

    Start at FROM enrollments. Each JOIN follows one of the ID lookups you just did for Ravi. The short names e, s, c and d stand for the four tables. Count the result: three enrollments went in. Check all six columns against the original inserts.

    SELECT s.student_id, s.name, c.course_id, c.title,
           d.dept_id, d.dept_name
    FROM enrollments AS e
    JOIN students AS s ON s.student_id = e.student_id
    JOIN courses AS c ON c.course_id = e.course_id
    JOIN departments AS d ON d.dept_id = s.dept_id
    ORDER BY s.student_id, c.course_id;

    Output:

    student_id | name | course_id | title | dept_id | dept_name
    S1 | Asha | C1 | SQL | D1 | Computing
    S1 | Asha | C2 | Python | D1 | Computing
    S2 | Ravi | C1 | SQL | D1 | Computing

    Make Monday’s edit again

    UPDATE changes the C1 row in Courses. Predict the two titles the SELECT will return before you read them.

    UPDATE courses SET title = 'SQL Basics'
    WHERE course_id = 'C1';
    SELECT s.name, c.title
    FROM enrollments AS e
    JOIN students AS s ON s.student_id = e.student_id
    JOIN courses AS c ON c.course_id = e.course_id
    WHERE c.course_id = 'C1'
    ORDER BY s.student_id;

    Output:

    name | title
    Asha | SQL Basics
    Ravi | SQL Basics

    Try a different desk: a library loan

    A library keeps loan_id, copy_id, book_title, borrower_id and borrower_name in one Loans table. A borrower can take the same copy again next month. Two copies can have the same title. If borrower B8 changes her name, which rows would you have to edit?

    Check your design

    In the flat table you would edit every loan for B8. Keep Borrowers(borrower_id, borrower_name), Copies(copy_id, book_title) and Loans(loan_id, copy_id, borrower_id) for this simplified exercise. A separate loan_id distinguishes repeat loans. If the library also tracks editions and many copies per edition, give editions their own ID and store the title there.

    Quiz

    Can you repair another register?

    Six questions. Choose an answer to see the reason.

    Questions beginners ask

    What is normalization in DBMS in simple words?

    It is a way of organizing tables so that each fact can be recorded in the right place. In this register, C1 has one title in Courses, even when many students take it.

    Does normalization remove all repeated values?

    Student IDs and course IDs still repeat in Enrollments. Those values record who takes which course. Repeating the title beside each enrollment is the duplication we removed.

    Would the same key work if students could retake a course?

    A student could then have two enrollments with the same student and course IDs. You would need a way to distinguish the attempts, such as a term if only one attempt per term is allowed.

    Keep a copy and practise

    Free PDF
    Normalization notes with SQL and practice questions
    7 pages. Read offline or print a copy.
    Download PDF

    Try the normalization questions in our DBMS interview guide, or practice joining tables with our SQL interview questions.

    For another worked example, read Microsoft’s introduction to database normalization. The SQLite foreign key documentation explains how SQLite checks the references used here.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous Article100 Machine Learning Interview Questions and Answers (Freshers to Experienced)
    Anirudh Khurana
    • Website

    Anirudh Khurana is the founder of Code and Debug, an Indian EdTech platform that helps students and working professionals crack coding interviews and move into data and AI roles. He teaches DSA, Python, SQL, data engineering and machine learning through live bootcamps and his YouTube channel, Code and Debug, which has over 50,000 subscribers. Every guide on this blog is written or reviewed by him and tested with real code.

    Related Posts

    SQL

    100 SQL Interview Questions with Queries and Answers (Freshers to Experienced)

    17 October 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • AI & Machine Learning (8)
    • Data Structures & Algorithms (241)
      • Beginner (82)
      • Expert (52)
      • Intermediate (106)
    • Interview Questions (6)
    • Pandas (1)
    • Python (1)
    • Resume (1)
    • SQL (2)
    Recent Posts

    Normalization in DBMS: 1NF, 2NF and 3NF With SQL Examples

    25 November 2026

    100 Machine Learning Interview Questions and Answers (Freshers to Experienced)

    25 November 2026

    K-Means Clustering in Machine Learning: An Interactive Guide (with Python)

    20 November 2026

    Decision Tree in Machine Learning: An Interactive Guide (with Python)

    18 November 2026

    Support Vector Machine (SVM) in Machine Learning: An Interactive Guide

    16 November 2026
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.