SQL joins are a core feature of relational databases, allowing you to combine data from multiple tables. Yet they're often misunderstood. In this visual guide, we'll walk through each join type with practical examples.
Sample Data for Examples
Throughout this guide, we'll use these two tables:
employees | id | name | department_id | |----|------|---------------| | 1 | Alice | 101 | | 2 | Bob | 102 | | 3 | Carol | 101 | | 4 | David | NULL |
departments | id | name | |----|------| | 101 | Engineering | | 102 | Marketing | | 103 | Sales |
INNER JOIN: Only Matching Records
Returns records that have matching values in both tables.
employees departments
┌───────┐ ┌───────┐
│ │ │ │
│ ┌───┼───────┼───┐ │
│ │ │ INNER │ │ │
│ │ │ JOIN │ │ │
│ └───┼───────┼───┘ │
│ │ │ │
└───────┘ └───────┘
Syntax
SELECT e.name AS employee, d.name AS department
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;
Result
| employee | department |
|---|---|
| Alice | Engineering |
| Bob | Marketing |
| Carol | Engineering |
David is excluded (NULL department_id doesn't match any department).
Sales is excluded (no employees have department_id = 103).
When to Use INNER JOIN
- You only want records that exist in both tables
- Common for reporting where incomplete data should be excluded
- Finding customers who have placed orders, products with reviews, etc.
LEFT JOIN (LEFT OUTER JOIN): All Left + Matching Right
Returns all records from the left table, plus matching records from the right table. Non-matches get NULL.
employees departments
┌───────┐ ┌───────┐
│▓▓▓▓▓▓▓│ │ │
│▓▓▓┌───┼───────┼───┐ │
│▓▓▓│▓▓▓│ LEFT │ │ │
│▓▓▓│▓▓▓│ JOIN │ │ │
│▓▓▓└───┼───────┼───┘ │
│▓▓▓▓▓▓▓│ │ │
└───────┘ └───────┘
(all left) (matching only)
Syntax
SELECT e.name AS employee, d.name AS department
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;
Result
| employee | department |
|---|---|
| Alice | Engineering |
| Bob | Marketing |
| Carol | Engineering |
| David | NULL |
David is included with NULL department (no match found).
Sales is still excluded (it's in the right table, not left).
When to Use LEFT JOIN
- List all employees, even those without departments
- Find customers regardless of whether they've ordered
- Show all products, including those never purchased
RIGHT JOIN (RIGHT OUTER JOIN): All Right + Matching Left
Returns all records from the right table, plus matching records from the left table.
employees departments
┌───────┐ ┌───────┐
│ │ │▓▓▓▓▓▓▓│
│ ┌───┼───────┼───┐▓▓▓│
│ │ │ RIGHT │▓▓▓│▓▓▓│
│ │ │ JOIN │▓▓▓│▓▓▓│
│ └───┼───────┼───┘▓▓▓│
│ │ │▓▓▓▓▓▓▓│
└───────┘ └───────┘
(matching only) (all right)
Syntax
SELECT e.name AS employee, d.name AS department
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.id;
Result
| employee | department |
|---|---|
| Alice | Engineering |
| Bob | Marketing |
| Carol | Engineering |
| NULL | Sales |
Sales is now included with NULL employee (no employees in Sales).
David is excluded (he's in the left table, not right).
When to Use RIGHT JOIN
- List all departments, even empty ones
- Most cases can be rewritten as LEFT JOIN by swapping table order
- Less commonly used than LEFT JOIN
FULL OUTER JOIN: All Records from Both Tables
Returns all records when there's a match in either left or right table.
employees departments
┌───────┐ ┌───────┐
│▓▓▓▓▓▓▓│ │▓▓▓▓▓▓▓│
│▓▓▓┌───┼───────┼───┐▓▓▓│
│▓▓▓│▓▓▓│ FULL │▓▓▓│▓▓▓│
│▓▓▓│▓▓▓│ OUTER │▓▓▓│▓▓▓│
│▓▓▓└───┼───────┼───┘▓▓▓│
│▓▓▓▓▓▓▓│ │▓▓▓▓▓▓▓│
└───────┘ └───────┘
(all left) (all right)
Syntax
SELECT e.name AS employee, d.name AS department
FROM employees e
FULL OUTER JOIN departments d ON e.department_id = d.id;
Result
| employee | department |
|---|---|
| Alice | Engineering |
| Bob | Marketing |
| Carol | Engineering |
| David | NULL |
| NULL | Sales |
Everyone is included — both David (no department) and Sales (no employees).
When to Use FULL OUTER JOIN
- Data reconciliation between systems
- Finding orphaned records on both sides
- Complete view of mapping relationships
Note: MySQL doesn't support FULL OUTER JOIN. Use UNION of LEFT and RIGHT:
SELECT e.name, d.name FROM employees e LEFT JOIN departments d ON e.department_id = d.id
UNION
SELECT e.name, d.name FROM employees e RIGHT JOIN departments d ON e.department_id = d.id;
CROSS JOIN: Cartesian Product
Returns every combination of rows from both tables. No ON clause needed.
employees × departments = all combinations
4 employees × 3 departments = 12 rows
Syntax
SELECT e.name AS employee, d.name AS department
FROM employees e
CROSS JOIN departments d;
Result (12 rows)
| employee | department |
|---|---|
| Alice | Engineering |
| Alice | Marketing |
| Alice | Sales |
| Bob | Engineering |
| Bob | Marketing |
| Bob | Sales |
| ... | ... |
When to Use CROSS JOIN
- Generate all possible combinations (e.g., sizes × colors)
- Date ranges (all dates × all products for inventory)
- Price matrices
⚠️ Warning: CROSS JOIN can produce massive result sets. 1,000 × 1,000 = 1,000,000 rows!
SELF JOIN: Joining a Table to Itself
Useful for hierarchical or comparative data within the same table.
employees_with_manager | id | name | manager_id | |----|------|------------| | 1 | Alice | NULL | | 2 | Bob | 1 | | 3 | Carol | 1 | | 4 | David | 2 |
Find Each Employee's Manager
SELECT
e.name AS employee,
m.name AS manager
FROM employees_with_manager e
LEFT JOIN employees_with_manager m ON e.manager_id = m.id;
Result
| employee | manager |
|---|---|
| Alice | NULL |
| Bob | Alice |
| Carol | Alice |
| David | Bob |
When to Use SELF JOIN
- Organizational hierarchies (employee → manager)
- Flight routes (origin → destination from same cities table)
- Finding duplicate records
- Comparing rows within the same table
Quick Reference Comparison
| Join Type | Left Table | Right Table | Result |
|---|---|---|---|
| INNER | Matching only | Matching only | Intersection |
| LEFT | All records | Matching only | Left + matches |
| RIGHT | Matching only | All records | Right + matches |
| FULL OUTER | All records | All records | Complete union |
| CROSS | All records | All records | Every combination |
| SELF | Table | Same table | Internal relationships |
Performance Tips
1. Index Your Join Columns
CREATE INDEX idx_department_id ON employees(department_id);
CREATE INDEX idx_department_pk ON departments(id);
2. Filter Before Joining
-- ❌ Slower: filter after join
SELECT * FROM employees e
JOIN departments d ON e.department_id = d.id
WHERE d.name = 'Engineering';
-- ✅ Faster: filter in subquery
SELECT * FROM employees e
JOIN (SELECT * FROM departments WHERE name = 'Engineering') d
ON e.department_id = d.id;
3. Avoid SELECT *
-- ❌ Fetches unnecessary columns
SELECT * FROM employees e JOIN departments d ...
-- ✅ Fetch only needed columns
SELECT e.name, d.name FROM employees e JOIN departments d ...
4. Use EXPLAIN to Analyze
EXPLAIN SELECT e.name, d.name
FROM employees e
JOIN departments d ON e.department_id = d.id;
Common Interview Questions
Q: What's the difference between JOIN and INNER JOIN?
A: They're identical. JOIN is shorthand for INNER JOIN.
Q: When would LEFT JOIN return the same results as INNER JOIN?
A: When every record in the left table has a matching record in the right table (no NULLs in the result).
Q: How do you find employees WITHOUT a department using JOIN?
SELECT e.name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id
WHERE d.id IS NULL;
Q: Can you have multiple JOIN conditions?
SELECT * FROM orders o
JOIN customers c ON o.customer_id = c.id AND o.region = c.region;
FAQ
Q: Which join is fastest? A: INNER JOIN is typically fastest because it returns the smallest result set. However, the right join depends on your data needs, not just speed.
Q: Can I join more than two tables? A: Yes! Chain as many joins as needed:
SELECT * FROM employees e
JOIN departments d ON e.department_id = d.id
JOIN locations l ON d.location_id = l.id;
Q: Why is my LEFT JOIN returning duplicates?
A: The right table has multiple matching rows. Use DISTINCT or aggregate functions to handle duplicates.
Q: Should I use JOIN or subquery? A: For related data you need in output, use JOIN. For existence checks or filtering, subqueries can be cleaner. Modern optimizers often generate the same execution plan.
Practice SQL Joins
Test your JOIN knowledge with our tools:
- Online SQL Editor — Practice queries in your browser
- SQL Formatter — Clean up complex join queries
- AI Python Compiler — Process query results with pandas
Last updated: December 2025 | Written by Muhammed Ajmal U K