Erdmates Logo
Erdmates
Back to Blog
SQLJOINDatabaseQueries

SQL JOINs Explained: INNER, LEFT, RIGHT, FULL OUTER with Real Examples

Understand the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN through concrete examples. See how ERD relationships map directly to JOIN queries.

March 10, 20268 min read

What Is a SQL JOIN?

Relational databases store data across multiple tables. A JOIN is a SQL operation that combines rows from two or more tables based on a related condition — typically a foreign key to primary key relationship — producing a single result set.

The relationship lines you draw in an ERD translate directly into JOIN conditions in your queries. Understanding your ERD means writing JOIN queries naturally.

Sample Tables

CREATE TABLE users (
  id    INT PRIMARY KEY,
  name  VARCHAR(100)
);

CREATE TABLE orders (
  id      INT PRIMARY KEY,
  user_id INT REFERENCES users(id),
  amount  DECIMAL(10,2)
);

-- Sample data
INSERT INTO users VALUES (1,'Alice'), (2,'Bob'), (3,'Carol');
INSERT INTO orders VALUES (1,1,50.00),(2,1,30.00),(3,2,80.00),(4,NULL,20.00);
-- Order 4 has NULL user_id; Carol has no orders

INNER JOIN: Intersection

Returns only rows that have a match in both tables.

SELECT u.name, o.amount
FROM users u
INNER JOIN orders o ON u.id = o.user_id;
nameamount
Alice50.00
Alice30.00
Bob80.00

Carol is excluded (no orders). Order 4 (user_id = NULL) is also excluded.

LEFT JOIN: Keep All Left Rows

Returns all rows from the left table (users), filling with NULL when there is no match in the right table (orders).

SELECT u.name, o.amount
FROM users u
LEFT JOIN orders o ON u.id = o.user_id;
nameamount
Alice50.00
Alice30.00
Bob80.00
CarolNULL

Carol is included with NULL amount. Essential for finding users who have never placed an order:

SELECT u.name
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE o.id IS NULL;

RIGHT JOIN: Keep All Right Rows

The opposite of LEFT JOIN — returns all rows from the right table.

SELECT u.name, o.amount
FROM users u
RIGHT JOIN orders o ON u.id = o.user_id;
nameamount
Alice50.00
Alice30.00
Bob80.00
NULL20.00

Order 4 is included; its user name is NULL. In practice, RIGHT JOIN is often rewritten as LEFT JOIN with tables swapped.

FULL OUTER JOIN: Union

Returns all rows from both tables. Rows without a match get NULLs on the missing side.

SELECT u.name, o.amount
FROM users u
FULL OUTER JOIN orders o ON u.id = o.user_id;
nameamount
Alice50.00
Alice30.00
Bob80.00
CarolNULL
NULL20.00

SELF JOIN: Joining a Table to Itself

Useful for hierarchical data like employee-manager relationships.

SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;

How ERD Relationships Map to JOINs

ERD relationship lines translate directly to JOIN ON conditions:

  • 1:N relationship (users to orders): JOIN orders ON users.id = orders.user_id
  • N:M relationship (students to courses via enrollments): Two JOINs through the junction table
  • Nullable FK: Consider LEFT JOIN when the foreign key allows NULL

Conclusion

JOINs are the backbone of relational database querying. When you visualize table relationships in an ERD, it becomes intuitive which JOIN type to use and how to write the ON clause. Design your schema in Erdmates, then use the AI natural language query feature to generate complex JOIN queries automatically.

Start with Erdmates Today

Start designing databases with our AI-powered ERD editor. Free to use.

Try Free DemoStart for Free

Related Articles

bigint, UUID v4, or UUID v7? How to Choose a Primary Key (with PostgreSQL 18's uuidv7())

Compare bigint, UUID v4, and UUID v7 primary keys, see what PostgreSQL 18's built-in uuidv7() changes, and learn which key to pick for each situation.

Databases Read by AI Agents: Schema Design Principles for the MCP Era

AI agents are increasingly connecting directly to databases through MCP. This post covers how to design names, comments, and constraints so agents don't misinterpret your schema, and how to split permissions safely.

PostgreSQL 19 Preview: Changes That Affect Schema Design and Operations (as of Beta 4)

Ahead of its official release, this post covers the PostgreSQL 19 changes that directly affect table design, migrations, and operations, based on the official release notes (Beta 4). It also looks at features reverted during beta and behavior changes to check before upgrading.

Erdmates Logo
Erdmates

Design databases with natural language and collaborate in real-time with your team using our AI-powered ERD editor.

Service

  • Features
  • Pricing
  • Demo
  • SQL to ERD converter

Resources

  • Blog
  • Help
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
  • Third-Party Provision
Company: Produx|Representative: 최정회
Address: 강원특별자치도 원주시 솔우물2길 4-1 (무실동)
Email: master@erdmates.com
Privacy Officer: 최정회|Hosting Provider: Amazon Web Services

© 2026 Produx. All rights reserved.