Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 104/ 160
06. Database Design, Normalization & Semi-Structured SQL

Second Normal Form (2NF): Eliminating Partial Dependencies

Remove partial dependencies on composite keys to reach 2NF.

IntermediatePartial Dependency · Composite Key3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

If a column only depends on part of a combined key, move it to the table where that part is the whole key.

Think of it like…

A class attendance sheet keyed by (student, date) that also lists each student's home address on every line. The address depends only on the student, not on the date, so it belongs on the student's record, written once.

Where you’ve seen it

enrolments(student_id, course_id, grade, course_title, instructor): course_title and instructor depend only on course_id. Renaming a course means updating every enrolment row. Splitting off courses(course_id, title, instructor) stores each course's details once.

Words you’ll hear

Composite key
A primary key made of two or more columns.
Partial dependency
A non-key column determined by only part of the composite key.
Full dependency
A non-key column that needs every key column to be determined, such as quantity.
2NF vs 3NF
2NF removes dependencies on part of the key; 3NF removes dependencies on non-key columns.

Visual diagram

Loading interactive diagram

Theory & example queries

Spotting partial dependencies

For each non-key column in a table with a composite key, ask: "Could I determine this from just one of the key columns?" In order_items, quantity needs both the order and the product. product_name needs only the product, so it's a partial dependency.

A table whose primary key is a single column can't have a partial dependency, so if it's in 1NF it's automatically in 2NF. That makes 2NF mostly a concern for junction tables, enrolment and line-item tables.

The splitQuery example
1Input dataStart from the source rows or state that the statement sees.
order_items
3 rows
order_items sample rows
idorder_idproduct_idqtyprice
110142249.5
2101841150
310242149.5
products
3 rows
products sample rows
idnamepricecategorystock
42SQL Notebook49.5books18
84Mechanical Keyboard150hardware5
11Query Mug12.99gifts33
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1CREATE TABLE products (2  id bigint PRIMARY KEY,3  name text NOT NULL4);56CREATE TABLE order_items (7  order_id bigint REFERENCES orders (id),8  product_id bigint REFERENCES products (id),9  quantity int NOT NULL,10  PRIMARY KEY (order_id, product_id)11);
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Choose the target

Identify the table or database object the statement will modify.

1/11
3OutputInspect the representative rows, affected-row count, plan, or command status.
Successful schema creation
CREATE TABLE CREATE TABLE

Note: Command tags are representative of PostgreSQL; exact client output varies by dialect.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Key takeaway: Ask of each column: does it need the whole key?

A subtle case: current vs historical values

The product's current price depends only on product_id and belongs in products. But the price the customer actually paid on that order is a fact about the order line, and must not change when the catalogue price changes later. That's a full dependency, so unit_price legitimately stays on order_items.

So normalising isn't just mechanically moving columns. Decide whether a column is the current attribute of the thing (move it) or a snapshot recorded at the time of the event (keep it).

Keeping the price paid

order_items (order_id, product_id, quantity, unit_price_paid) with products (id, name, current_price): the two prices mean different things.

Key takeaway: Move current attributes; keep historical snapshots on the event row.
Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Review

Confirm 1NF and find composite keys

Only tables with multi-column primary keys can violate 2NF, so start with junction and line-item tables.

Focus at this step

PRIMARY KEY (order_id, product_id)

What to watch at this step
Single-column PK
already 2NF
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

A table is in Second Normal Form (2NF) when it's in 1NF and every non-key column depends on the *whole* primary key, not just part of it. It only matters for tables with a composite key: in order_items(order_id, product_id, quantity, product_name), product_name depends only on product_id, which is a partial dependency, so it belongs in the products table.

Why it matters: Partial dependencies copy the same fact (a product's name or price) onto every row that mentions it, so updates must touch many rows and can leave them disagreeing. It's a standard step in normalisation questions, usually asked right after 1NF.

When to reach for it

  • Line-item, enrolment and junction tables
  • Explaining normalisation step by step
  • Removing duplicated product or course details

When to avoid or be careful

  • Copying descriptive columns of one part of the key onto every row
  • Moving historical snapshot values that must not change
  • Worrying about 2NF on tables with a single-column key

1NF vs 2NF vs 3NF

1NF
Best for: One value per cell, no repeating groups

Strengths: Data can be queried and indexed properly.

Watch out: Facts can still be duplicated across rows.

2NF
Best for: Tables with composite keys

Strengths: No column depends on only part of the key.

Watch out: Dependencies between non-key columns can remain.

3NF
Best for: The usual target for transactional schemas

Strengths: Every non-key column depends on the key and nothing else.

Watch out: More tables and joins.

Interview signals & numbers to know

When this topic comes up

  • “What is 2NF?”
  • “What's a partial dependency?”
  • Composite primary keys
  • Product names repeated on order lines
  • Normalise this schema

Benchmarks & rules of thumb

2+ -
key columns needed for a partial dependency
1NF -
prerequisite for 2NF
3NF -
the next step: no transitive dependencies

Common mistakes & follow-ups

Common interview mistakes

  • Confusing a partial dependency (on part of the key) with a transitive dependency (on another non-key column).
  • Moving unit_price_paid to products and rewriting history whenever the price changes.
  • Saying a table with a single-column key violates 2NF.

Likely interviewer follow-ups

  • Why can't a table with a single-column primary key violate 2NF?
  • What's the difference between a partial and a transitive dependency?
  • Should an order line store the product price? Why?
Interview tip: Use order_items as the example, move product_name, then make the point about keeping the price paid. That last nuance shows you understand the data, not just the rule.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Second Normal Form (2NF): Eliminating Partial Dependencies with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
First Normal Form (1NF): Atomic Values & Repeating Groups
Next topic
Third Normal Form (3NF): Eliminating Transitive Dependencies