SQL Table Relationships Explained

If SQL table relationships feel abstract or confusing, you’re not alone. Most explanations jump straight into diagrams and jargon without explaining the one thing that actually matters:

If you know where the foreign key goes, you understand the relationship.

This page will show you how to think about relationships, not just what they’re called — with plain-English explanations, simple diagrams, and copy-paste SQL examples.


The Only Rules You Need to Remember

Before we dive in, lock these in:

  • One-to-One (1:1) → Foreign key is unique

  • One-to-Many (1:N) → Foreign key goes on the many side

  • Many-to-Many (N:N) → You must use a junction table

If you remember nothing else, remember this.


One-to-One (1:1)

What it means

Each row in Table A relates to one and only one row in Table B.

When it’s used

  • Optional or extended data

  • Sensitive data separation

  • Keeping tables smaller and cleaner

Real-world example

A user has one profile.
A profile belongs to one user.

users (1) ─── profile (1)
profile.user_id → users.id

Where does the foreign key go?

✅ The foreign key is unique
Often it’s also the primary key.

Example tables

users
- id (PK)
- email

profiles
- user_id (PK, FK)
- bio
- avatar

SQL example

CREATE TABLE profiles (
user_id INT PRIMARY KEY,
bio TEXT,
avatar TEXT,
FOREIGN KEY (user_id) REFERENCES users(id)
);

Common mistake

❌ Forgetting to enforce uniqueness, which silently turns this into one-to-many.


One-to-Many (1:N)

What it means

One row in Table A can relate to many rows in Table B — but each row in Table B belongs to only one row in Table A.

This is the most common relationship in SQL.

Real-world example

A user can write many posts.
Each post belongs to one user.

users (1) ─────< posts (many)
posts.user_id → users.id

Where does the foreign key go?

Always on the many side

Example tables

users
- id (PK)
- name

posts
- id (PK)
- user_id (FK)
- title

SQL example

CREATE TABLE posts (
id INT PRIMARY KEY,
user_id INT,
title VARCHAR(255),
FOREIGN KEY (user_id) REFERENCES users(id)
);

Why this works

Each post “knows” which user it belongs to.
The user doesn’t need to track all its posts.

Common mistake

❌ Storing multiple post IDs inside the users table.


Many-to-Many (N:N)

What it means

Rows in Table A can relate to many rows in Table B — and rows in Table B can relate to many rows in Table A.

Important rule

🚨 This cannot exist without a third table.

Real-world example

Students take many courses.
Courses have many students.

students ───< enrollments >─── courses

The junction (pivot) table

The middle table holds both foreign keys.

enrollments
- student_id (FK)
- course_id (FK)

SQL example

CREATE TABLE enrollments (
student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (course_id) REFERENCES courses(id)
);

Why the extra table exists

SQL tables store rows, not lists.
The junction table turns relationships into data.

Common mistake

❌ Trying to store arrays or comma-separated IDs.


Quick Mental Models

  • One-to-One → “Extra details”

  • One-to-Many → “Ownership”

  • Many-to-Many → “Membership / tagging / access”


Cheat Sheet (Save This)

  • 1:1 → FK is unique

  • 1:N → FK on the many table

  • N:N → Junction table with 2 FKs + composite PK

If you can point to the foreign key and explain why it lives there, you understand SQL relationships.

Download Your FREE

Dev Stack Starter Guide

Build, automate, and launch faster—see the automation stack developers and agencies are switching to.

  • ✅ API Templates & Code Snippets
  • ✅ Done-for-You Automation Workflows
  • ✅ Step-by-Step Funnel & CRM Guide
  • ✅ Free for Developers, Freelancers, & SaaS Builders

We Respect Your Privacy