Understanding Relationships (One-to-One, One-to-Many, Many-to-Many)
Model 1:1, 1:N, and M:N relationships before writing join queries.
In plain words
One thing matches one thing, one thing has many things, or many things match many things. The last kind needs an extra table to list the pairs.
Think of it like…
A country has one capital (one-to-one), a mother can have many children but each child has one birth mother (one-to-many), and authors write many books while books can have many authors (many-to-many, recorded in a separate list of author-book pairs).
Where you’ve seen it
An e-commerce schema: users 1–1 user_settings (UNIQUE user_id), customers 1–N orders (orders.customer_id), and products N–M categories through product_categories(product_id, category_id), because a product can be in several categories and a category holds many products.
Words you’ll hear
- Cardinality
- How many rows on one side can relate to a row on the other: 1:1, 1:N, M:N.
- Foreign key placement
- In 1:N the key goes on the many side; in 1:1 on either side, made UNIQUE.
- Junction table
- A table of (a_id, b_id) pairs that represents a many-to-many relationship.
- Optional vs required
- A nullable foreign key means the relationship is optional.
Visual diagram
Theory & example queries
Modelling each type
One-to-many is the most common. Put the foreign key on the many side (orders.customer_id), because each order has exactly one customer while a customer can't hold a list of order ids in one column. Make it NOT NULL if every child must have a parent.
One-to-one is a one-to-many with a UNIQUE constraint on the foreign key, or two tables sharing the same primary key. It's used to split optional or rarely read columns into their own table. Many-to-many can't be represented with a single foreign key: create a junction table with a foreign key to each side and a composite primary key so a pair can't repeat.
CREATE TABLE enrolments ( student_id bigint REFERENCES students (id), course_id bigint REFERENCES courses (id), enrolled_at date NOT NULL, PRIMARY KEY (student_id, course_id));PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Junction table created
Note: Creation succeeds when referenced students and courses tables already exist and enrolments is unused.
Key takeaway: 1:N → FK on the many side; 1:1 → UNIQUE FK; M:N → junction table.
How relationships affect queries
Joining from the one side to the many side repeats the parent for every child: one customer with 12 orders gives 12 rows. That's expected, but it's why aggregates over joined data need care, and why joining a parent to two different many-side tables multiplies rows (fan-out).
Junction tables often carry facts about the relationship itself, such as enrolled_at or a grade. That's normal: the junction row is the enrolment. Ask what one row of each table means, and the relationship type usually becomes obvious.
SELECT s.nameFROM courses c JOIN enrolments e ON e.course_id = c.id JOIN students s ON s.id = e.student_idWHERE c.code = 'DB101';PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Students enrolled in DB1013 sample rows
| name |
|---|
| Asha Rao |
| Kabir Shah |
| Mei Chen |
Note: Representative enrollment data for course code DB101.
Key takeaway: Joining across the many side repeats rows; junction rows can hold their own facts.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Ask both directions
For entities A and B, ask how many B one A can have, and how many A one B can have. The two answers give the type.
1 customer → many orders · 1 order → 1 customer
When to use it & trade-offs
What it is
Tables relate in three ways. One-to-one: each row matches at most one row on the other side (a user and their profile). One-to-many: one parent has many children (a customer and their orders), with the foreign key on the "many" side. Many-to-many: rows on both sides relate to many on the other (students and courses), which needs a junction table holding pairs of foreign keys.
Why it matters: Getting the relationship type right decides where foreign keys go, which joins duplicate rows, and whether the design can represent reality at all. It's the first step of every schema-design interview question, and most join bugs trace back to misjudging it.
When to reach for it
- Designing any schema before writing queries
- Deciding where a foreign key column belongs
- Explaining why a join returns more rows than expected
When to avoid or be careful
- Putting a list of child ids in the parent row
- Modelling a many-to-many with a foreign key on one side only
- Splitting tables 1:1 without a reason (optional or rarely used columns)
Ways to model a many-to-many relationship
Strengths: Enforced by foreign keys, indexable both ways, can hold relationship facts.
Watch out: An extra table and join.
Strengths: No join for simple reads.
Watch out: No foreign keys, awkward queries from the other side.
Strengths: Easy to write once.
Watch out: Breaks 1NF, can't be indexed or validated.
Interview signals & numbers to know
When this topic comes up
- “Design the schema for …”
- “One-to-many vs many-to-many”
- “Where does the foreign key go?”
- Junction / bridge / association table
- Students and courses, authors and books
Benchmarks & rules of thumb
- 3
- relationship types
- 2
- foreign keys in a junction table
- N rows
- per parent when joining to its N children
Common mistakes & follow-ups
Common interview mistakes
- Adding order_ids to the customers table instead of customer_id to orders.
- Treating a many-to-many as one-to-many, so a product can only belong to one category.
- Forgetting the composite primary key on a junction table, allowing the same pair to be stored twice.
Likely interviewer follow-ups
- How would you model users following other users?
- When would you split a table into two with a one-to-one relationship?
- What extra columns might a junction table need?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Understanding Relationships (One-to-One, One-to-Many, Many-to-Many) with hands-on SQL practice and interview Q&A.