📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

What is database normalisation and why does it matter? Explain insert, update and delete anomalies, and walk me through first, second and third normal form using an orders example.

Covers the purpose of normalisation, the three data anomalies it removes, and 1NF, 2NF and 3NF applied step by step to an orders table in SQL Server.

Beginner Theory Beginner data-modelling database-design normal-forms normalisation relational-model sql-server

🎯 Key points

  • Normalisation removes redundant facts so the schema cannot contradict itself
  • Insert, update and delete anomalies come from redundant facts
  • 1NF: atomic values, no repeating groups, a key identifies each row
  • 2NF: no non-key column depends on only part of a composite key
  • 3NF: no transitive dependency (non-key depends on another non-key)
  • Orders example: Customers, Products, Orders, OrderLines tables
  • UnitPrice on the order line is the price charged then, a separate historical fact
  • OLTP aims for 3NF; denormalise only for a measured read problem, with a plan to keep copies consistent

⏱ 30-second answer

Normalisation is organising tables so that each fact is stored once, in the table whose key it depends on, so the data cannot contradict itself. Redundant copies cause anomalies: an update anomaly (change a customer's email in one row but not the other 40), an insert anomaly (cannot record a product until someone orders it) and a delete anomaly (deleting the last order loses the customer).

  • 1NF: atomic values, no repeating groups, every row identified by a key.
  • 2NF: every non-key column depends on the whole key, not part of a composite key.
  • 3NF: no transitive dependencies: non-key columns depend on the key, not on other non-key columns.

A price on an order line is not a copy: it is the price charged on that day, a different fact from today's list price. For OLTP I design to 3NF and only denormalise where a measured read cost justifies the extra write and consistency cost.

🗣 Full answer (senior)

How I would answer this in an interview

"Normalisation is the discipline of putting every fact in exactly one place: the table whose key that fact depends on. The reason it matters is that duplicated facts drift. If a customer's email is copied onto every order row, an update that misses one row leaves the database disagreeing with itself. That's the update anomaly. The insert anomaly is when you can't record something, like a new product, without inventing an order for it. The delete anomaly is when removing one thing, like the last order, silently removes another, like the only record of the customer.

The normal forms are a checklist for removing those problems. First normal form says every column holds one atomic value and there are no repeating groups like Product1, Product2, Product3, and every row has a key. Second normal form applies when the key is composite: every non-key column must depend on the whole key. On an order-lines table keyed by OrderId and ProductId, ProductName depends only on ProductId, so it moves to Products. Third normal form removes transitive dependencies: on Orders, CustomerEmail depends on CustomerId, not on OrderId, so it moves to Customers.

The mistake I see is treating every copy as redundancy. The unit price on an order line is not a copy of the product's current price; it's a historical fact about that sale, so it belongs on the line. In a .NET service with EF Core I design the OLTP schema to third normal form by default, let foreign keys and unique constraints enforce it, and only denormalise once I've measured a read that genuinely needs it. Normalise for correctness first; denormalise for performance on evidence."


The anomalies

Anomaly Example in a flat OrdersFlat table Root cause
Update Customer changes email; 3 of 40 rows keep the old one Same fact stored many times
Insert Cannot add a new product until it is ordered Product facts live only on order rows
Delete Cancelling the only order deletes the customer's address Two facts share one row

The normal forms in one table

Form Rule Violation in the orders example Fix
1NF Atomic values, no repeating groups, has a key Products = 'Kettle,Toaster' or Product1..Product3 columns One row per order line
2NF No partial dependency on a composite key OrderLines(OrderId, ProductId, ProductName) Move ProductName to Products
3NF No transitive dependency Orders(OrderId, CustomerId, CustomerEmail) Move CustomerEmail to Customers

Walking the example

Start: OrdersFlat(OrderId, OrderDate, CustomerName, CustomerEmail, Products) where Products holds a comma-separated list.

  1. 1NF: split the list into rows: OrderLines(OrderId, ProductId, ProductName, Quantity, UnitPrice).
  2. 2NF: ProductName depends only on ProductId → Products(ProductId, Name, ListPrice).
  3. 3NF: CustomerEmail depends on CustomerId → Customers(CustomerId, Name, Email); Orders(OrderId, CustomerId, OrderDate).

When to stop

Workload Target Why
OLTP (orders, bookings) 3NF / BCNF Many small writes; correctness matters most
Reporting / analytics Star schema (deliberately denormalised) Few writes, many large reads
Caches, read models Denormalised copies Rebuilt from the normalised source

Production example

On an order system [where you worked], a flat order table copied the customer's delivery address onto every order. A support fix updated the address on the latest order only, and [N] parcels went to the old address in a month. Moving addresses to their own table with an AddressId on the order (plus a snapshot on dispatch) removed the class of bug, and the table shrank by about [30]%.

🔍 What the interviewer is checking

Checks that the candidate understands why normalisation exists (anomalies) and can apply the normal forms to a concrete schema rather than recite definitions.

Green flags

  • Names the three anomalies with concrete examples
  • Explains 2NF as partial dependency on a composite key, 3NF as transitive dependency
  • Distinguishes snapshot facts (price at sale) from redundancy
  • Mentions enforcing the design with keys and constraints

Red flags

  • Recites "1NF, 2NF, 3NF" without being able to normalise an example
  • Thinks normalisation is mainly about saving disk space
  • Claims you should always denormalise for speed

📚 Learn in depth

1. Functional dependencies are the real idea

All the normal forms are defined by functional dependencies: A → B means "if I know A, I know exactly one B". CustomerId → CustomerEmail, ProductId → ProductName, (OrderId, ProductId) → Quantity. Normalising is just making sure every dependency points from a key of the table it lives in.

OrdersFlat
  OrderId ───────────► OrderDate, CustomerId
  CustomerId ────────► CustomerName, CustomerEmail     (transitive: breaks 3NF)
  (OrderId,ProductId)► Quantity, UnitPrice
  ProductId ─────────► ProductName                     (partial: breaks 2NF)

2. First normal form

1NF asks for: each column holds a single value of its type, no repeating groups (Phone1, Phone2, Phone3), and a key so rows are distinguishable. A comma-separated list in a column breaks 1NF: you cannot index an element, enforce a foreign key on it, or update one element without rewriting the string. SQL Server's STRING_SPLIT exists for parsing input, not as a storage design.

3. Second normal form

2NF only matters for composite keys. If the key is (OrderId, ProductId) and ProductName depends only on ProductId, every order line repeats the name. Tables with a single-column surrogate key are automatically in 2NF, which is why surrogate keys sometimes hide a design problem rather than fix it: OrderLines(OrderLineId, OrderId, ProductId, ProductName) is in 2NF technically but still has the redundancy, now as a transitive dependency via ProductId.

4. Third normal form

3NF: no non-key column depends on another non-key column. The classic memory aid is "every non-key attribute depends on the key, the whole key, and nothing but the key". Orders.CustomerEmail depends on Orders.CustomerId, a non-key column, so it moves.

5. Snapshots are not redundancy

OrderLines.UnitPrice looks like a copy of Products.ListPrice, but they are different facts: one is "the price charged on 3 March", the other is "today's price". The dependency is (OrderId, ProductId) → UnitPrice, which is a key dependency. The same goes for a delivery address captured at dispatch, or a tax rate applied to an invoice. Getting this wrong (joining to the current price for an old invoice) is a real bug that legal and finance teams notice.

6. What SQL Server gives you to enforce it

Customers ◄──FK── Orders ◄──FK── OrderLines ──FK──► Products
   PK CustomerId     PK OrderId     PK (OrderId, LineNo)   PK ProductId
   UQ Email                         UQ (OrderId, ProductId)

Primary keys, unique constraints and foreign keys turn the design into rules the engine enforces for every writer: your .NET API, a Python script, a support engineer in SSMS.

7. Costs

More tables mean more joins. Joins on indexed keys are cheap in SQL Server (a nested loop join with an index seek is a few logical reads per row), so for OLTP this is rarely the bottleneck. Reporting queries over millions of rows are where denormalisation (a star schema, an indexed view, a columnstore reporting table) starts to pay.

Try it yourself

  1. Create OrdersFlat with a comma-separated Products column, insert 5 rows, then try to write a query for "how many kettles did we sell?". Note how fragile it is.
  2. Normalise it into Customers, Products, Orders and OrderLines with primary and foreign keys.
  3. Try to delete a customer who has orders and read the foreign key error. That error is the delete anomaly being prevented.
  4. Write the list of functional dependencies for a table at work and check each against 2NF and 3NF.
  5. Map the normalised schema with EF Core and look at the SQL SaveChanges produces for one order with three lines.

💻 Code

A flat table with anomalies, then the same data in 3NF.

-- Before: one wide table. Breaks 1NF (list in a column), 2NF and 3NF.
CREATE TABLE dbo.OrdersFlat
(
    OrderId       int           NOT NULL PRIMARY KEY,
    OrderDate     date          NOT NULL,
    CustomerName  nvarchar(100) NOT NULL,
    CustomerEmail nvarchar(256) NOT NULL,   -- repeated on every order: update anomaly
    Products      nvarchar(400) NOT NULL    -- 'Kettle,Toaster': not atomic
);

-- After: 3NF. Each fact lives with the key it depends on.
CREATE TABLE dbo.Customers
(
    CustomerId int IDENTITY PRIMARY KEY,
    Name       nvarchar(100) NOT NULL,
    Email      nvarchar(256) NOT NULL CONSTRAINT UQ_Customers_Email UNIQUE
);

CREATE TABLE dbo.Products
(
    ProductId int IDENTITY PRIMARY KEY,
    Name      nvarchar(100) NOT NULL,
    ListPrice decimal(19,4) NOT NULL
);

CREATE TABLE dbo.Orders
(
    OrderId    int IDENTITY PRIMARY KEY,
    CustomerId int  NOT NULL REFERENCES dbo.Customers (CustomerId),
    OrderDate  date NOT NULL
);

CREATE TABLE dbo.OrderLines
(
    OrderId   int NOT NULL REFERENCES dbo.Orders (OrderId),
    ProductId int NOT NULL REFERENCES dbo.Products (ProductId),
    Quantity  int NOT NULL CHECK (Quantity > 0),
    UnitPrice decimal(19,4) NOT NULL,  -- the price charged then: a snapshot, not redundancy
    CONSTRAINT PK_OrderLines PRIMARY KEY (OrderId, ProductId)
);

The same model in EF Core, so the database, not just the code, holds the rules.

namespace Shop.Data;

public sealed class Customer
{
    public int CustomerId { get; set; }
    public required string Name { get; set; }
    public required string Email { get; set; }
    public List<Order> Orders { get; } = [];
}

public sealed class Order
{
    public int OrderId { get; set; }
    public int CustomerId { get; set; }
    public DateOnly OrderDate { get; set; }
    public List<OrderLine> Lines { get; } = [];
}

public sealed class OrderLine
{
    public int OrderId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; } // copied from Product.ListPrice at checkout on purpose
}

⚠ Common mistakes

  • Mistake: Storing a comma-separated list in a column. Why it is wrong: breaks 1NF; you cannot index, constrain or join on an element. Instead: a child table with one row per value.
  • Mistake: Treating OrderLines.UnitPrice as redundancy and joining to the current product price. Why it is wrong: historical invoices change when prices change. Instead: store the price charged as a snapshot on the line.
  • Mistake: Thinking a surrogate Id column makes a table normalised. Why it is wrong: it satisfies 2NF trivially but transitive dependencies remain. Instead: list the functional dependencies and check 3NF.
  • Mistake: Normalising in the C# model but not in the database (no FKs, no unique constraints). Why it is wrong: any other writer can create orphans and duplicates. Instead: enforce keys and constraints in SQL Server.
  • Mistake: Repeating columns like Phone1, Phone2, Phone3. Why it is wrong: a repeating group; the fourth phone needs a schema change and queries must check every column. Instead: a CustomerPhones table.
  • Mistake: Denormalising up front "for performance". Why it is wrong: you pay the consistency cost before knowing there is a read problem. Instead: measure, then add an index, an indexed view or a read model.

🏛 Senior insight: trade-offs

Normalisation is about correctness under change: it decides how many places a single update has to touch. A senior answer frames the normal forms as a tool for finding redundant facts, distinguishes snapshots from copies, and knows that a star schema in a reporting database is a deliberate, read-optimised exception, not a failure to normalise.

I also measure rather than assume: before denormalising, I capture the slow query's actual plan, logical reads (SET STATISTICS IO, TIME ON) and its Query Store history, and try an index first. Only if the join itself is the cost do I add a copy, and then I decide who keeps it in sync (a computed column, an indexed view, or an event-driven read model) and how drift is detected.

Option When it fits Cost When not to
3NF / BCNF OLTP: orders, bookings, accounts More joins; more tables to map Large analytical scans
Star schema Reporting, Power BI, data warehouse Duplicated dimension data, ETL to keep in sync Transactional writes
Snapshot columns Facts that must not change later (price, address at dispatch) Extra storage Data that should follow the master record

🔗 Follow-ups the interviewer may ask

  1. When: Candidate finishes 3NF confidently. Why: Checks whether the candidate knows where normalisation goes beyond 3NF and when it stops paying.
    How to answer

    BCNF tightens 3NF: every determinant must be a candidate key. It only bites when a table has overlapping composite candidate keys, e.g. a tutoring table where each tutor teaches one subject. 4NF removes independent multi-valued facts stored together: an employee's skills and languages in one table force a cross product of rows. 5NF removes join dependencies: a table that can be split into three and rejoined without loss, because of a cyclic rule like "if a supplier supplies a part, and a project uses that part, and the supplier works with the project, then the supplier supplies that part to that project".

    In practice, a sensible 3NF design with one table per independent relationship is almost always already in BCNF and 4NF; 5NF cases are rare. The value is spotting the smell, not chasing the label.

  2. When: Candidate mentions denormalising for performance. Why: Checks judgement about trading consistency for read speed.
    How to answer

    I denormalise only when a measured read cost cannot be fixed with an index or query rewrite, and only with a named owner that keeps the copy consistent. SQL Server gives three levels:

    • Persisted computed column: a derived value from the same row (e.g. LineTotal = Quantity * UnitPrice), always consistent and indexable.
    • Indexed view: a pre-joined or pre-aggregated result with a unique clustered index, maintained synchronously on every write, which can create lock hot spots on popular groups.
    • Summary / reporting table or read model: refreshed by a job or events, cheapest to read but can be stale.

    The trade-off: reads get cheaper, but writes get slower and lock more, data flows get more complex, and every copy needs reconciliation.

↩ Usually asked after

More in Database Design

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me