Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Database Fundamentals

Database Foundations

  • ACID Properties
  • Indexes & Query Performance
  • Transactions & Isolation Levels

Database Design

  • Normalization (1NF–3NF)
  • SQL Joins & Set Operations
  • Window Functions
Chaturmind
← Database Fundamentals

Database Foundations

  • ACID Properties
  • Indexes & Query Performance
  • Transactions & Isolation Levels

Database Design

  • Normalization (1NF–3NF)
  • SQL Joins & Set Operations
  • Window Functions
HomeLearnDatabasesDatabase FundamentalsAdvanced Querying
✓ FreeIntermediate· 12 min read

SQL Joins

Master INNER, LEFT, RIGHT, FULL, and CROSS joins with visual diagrams and query examples.

Published April 5, 2025


SQL Joins

Joins combine rows from two or more tables based on a related column. Mastering joins is essential — they appear in almost every SQL interview.

Setup

-- employees
| id | name    | dept_id |
|----|---------|--------|
| 1  | Alice   | 10     |
| 2  | Bob     | 20     |
| 3  | Charlie | NULL   |

-- departments
| id | name        |
|----|-------------|
| 10 | Engineering |
| 20 | Marketing   |
| 30 | Finance     |

INNER JOIN — only matching rows

SELECT e.name, d.name AS dept
FROM employees e
INNER JOIN departments d ON e.dept_id = d.id;

-- Result:
-- Alice   Engineering
-- Bob     Marketing
-- (Charlie excluded — NULL dept_id; Finance excluded — no employees)

LEFT JOIN — all left rows + matching right

SELECT e.name, d.name AS dept
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.id;

-- Result:
-- Alice   Engineering
-- Bob     Marketing
-- Charlie NULL         ← Charlie kept, no dept match

RIGHT JOIN — all right rows + matching left

SELECT e.name, d.name AS dept
FROM employees e
RIGHT JOIN departments d ON e.dept_id = d.id;

-- Result:
-- Alice   Engineering
-- Bob     Marketing
-- NULL    Finance     ← Finance kept, no employees

FULL OUTER JOIN — all rows from both sides

SELECT e.name, d.name AS dept
FROM employees e
FULL OUTER JOIN departments d ON e.dept_id = d.id;

-- Result:
-- Alice   Engineering
-- Bob     Marketing
-- Charlie NULL
-- NULL    Finance

CROSS JOIN — cartesian product

SELECT e.name, d.name FROM employees e CROSS JOIN departments d;
-- 3 employees × 3 departments = 9 rows

SELF JOIN — join a table to itself

-- employees table has manager_id referencing id in same table
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;

Anti-Join — rows in left with NO match in right

-- Employees without a department
SELECT e.name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.id
WHERE d.id IS NULL;

-- Alternative with NOT EXISTS
SELECT name FROM employees e
WHERE NOT EXISTS (
    SELECT 1 FROM departments d WHERE d.id = e.dept_id
);

Performance Tips

  • Always index the join columns (ON e.dept_id = d.id → index on dept_id)
  • Reduce the result set before joining: filter with WHERE early
  • For large tables, check the query plan with EXPLAIN ANALYZE

Interview Tips

  1. Know that LEFT JOIN is far more common in real apps than RIGHT JOIN (just swap the table order).
  2. Practice writing queries that involve 3+ table joins — interviewers often test this.
  3. Be ready to explain the difference between WHERE and HAVING when aggregating joined results.

Previous

Normalization (1NF–3NF)

Next

Window Functions

AI Tutor

Lesson: SQL Joins

Quick actions

AI responses can be inaccurate. Verify critical information.