What is the difference between INNER JOIN and OUTER JOIN in SQL? How do LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN differ from each other and from INNER JOIN?
INNER JOIN returns only the rows that have matching values in both tables, while OUTER JOIN includes unmatched rows from one or both tables depending on the type used. LEFT OUTER JOIN returns all records from the left table and matched records from the right table, RIGHT OUTER JOIN returns all records from the right table and matched records from the left table, and FULL OUTER JOIN returns all records from both tables regardless of matching conditions.
Contents
- What is INNER JOIN?
- Understanding OUTER JOIN Types
- LEFT OUTER JOIN Explained
- RIGHT OUTER JOIN Explained
- FULL OUTER JOIN Explained
- Visual Comparison with Venn Diagrams
- Practical Examples and Use Cases
- When to Use Each JOIN Type
What is INNER JOIN?
An INNER JOIN is the most common type of JOIN operation in SQL. It returns only the rows that have matching values in both tables based on the specified join condition. According to the official documentation, INNER JOINs are best used when you only need matching rows from the paired tables.
The key characteristics of INNER JOIN include:
- Strict matching: Only returns rows where the join condition is satisfied in both tables
- Data reduction: Often results in fewer rows than the original tables
- Performance: Generally faster than OUTER JOINs since it processes less data
- Syntax: Can be written with or without the INNER keyword (both
JOIN table2andINNER JOIN table2are equivalent)
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
In this example, only employees who have a valid department_id that exists in the departments table will be returned. If an employee has no matching department or a department has no employees, those records won’t appear in the result set.
Understanding OUTER JOIN Types
OUTER JOIN operations include unmatched rows from one or both tables, making them fundamentally different from INNER JOINs. As GeeksforGeeks explains, OUTER JOINs are used when you need to retain unmatched rows from one or both tables.
The three main types of OUTER JOINs serve different purposes:
- LEFT OUTER JOIN: Preserves all records from the left table
- RIGHT OUTER JOIN: Preserves all records from the right table
- FULL OUTER JOIN: Preserves all records from both tables
Key Insight: The word “OUTER” is optional in SQL syntax. For example,
LEFT JOINandLEFT OUTER JOINmean exactly the same thing in most SQL databases.
OUTER JOINs can retrieve more data than INNER JOINs, making them essential when you need a complete view of your data, even when relationships don’t exist in all records.
LEFT OUTER JOIN Explained
A LEFT OUTER JOIN (or simply LEFT JOIN) returns all records from the left table and the matched records from the right table. When there’s no match in the right table, NULL values are returned for those columns.
As described in the DataCamp tutorial, “The LEFT OUTER JOIN returns all records from the left table and matched records from the right table.”
How LEFT JOIN Works:
- Left table preservation: All rows from the left table are included in the result
- Right table matching: Only rows with matching join conditions from the right table are included
- NULL filling: Unmatched right table columns are filled with NULL values
- Performance: Generally more expensive than INNER JOIN due to the need to process all left table rows
SELECT customers.customer_name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;
Example Scenario:
If you have 100 customers and only 30 have placed orders, a LEFT JOIN will return all 100 customers. The 70 customers without orders will have NULL values in the order_id column.
RIGHT OUTER JOIN Explained
A RIGHT OUTER JOIN (or RIGHT JOIN) is the mirror image of a LEFT JOIN. It returns all records from the right table and the matched records from the left table. According to GeeksforGeeks, “Right Outer Join works by selecting all records from the right table and matching records from the left table.”
How RIGHT JOIN Works:
- Right table preservation: All rows from the right table are included
- Left table matching: Only rows with matching join conditions from the left table are included
- NULL filling: Unmatched left table columns are filled with NULL values
- Usage: Less commonly used than LEFT JOIN in practice
SELECT departments.department_name, employees.employee_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;
Example Scenario:
If you have 5 departments and employees are assigned to only 4 of them, a RIGHT JOIN will return all 5 departments. The department without employees will have NULL values in the employee_name column.
FULL OUTER JOIN Explained
A FULL OUTER JOIN (or FULL JOIN) returns all records from both the left and right tables. It includes matched rows where the join condition is satisfied, and for unmatched rows, NULL values are filled in. As stated in the Software Testing Help article, “In full outer joins, all data are combined wherever possible.”
How FULL JOIN Works:
- Complete preservation: All rows from both tables are included
- Matching logic: Rows with matching join conditions are combined
- NULL filling: Unmatched rows from either table get NULL values for the other table’s columns
- Result: The most comprehensive result set among all JOIN types
SELECT customers.customer_name, orders.order_id
FROM customers
FULL JOIN orders ON customers.customer_id = orders.customer_id;
Example Scenario:
If you have 100 customers and 200 orders, a FULL JOIN will return all 100 customers and all 200 orders. Customers without orders will have NULL order_ids, and orders without customers will have NULL customer_names.
Visual Comparison with Venn Diagrams
Visual representations are excellent for understanding the differences between JOIN types. Venn diagrams provide a clear way to conceptualize how each JOIN operation works.
INNER JOIN Visualization:
Table A Table B
┌─────────┐ ┌─────────┐
│ ●●● │ │ ●●● │
│ ●●●● │◄──►│ ●●●● │
│ ●●●●● │ │ ●●●●● │
│ ●●●● │ │ ●●●● │
│ ●●● │ │ ●●● │
└─────────┘ └─────────┘
▲ ▲
└───────┬───────┘
│
┌───────────────┐
│ ●●●●● │
│ ●●●●●●● │
│ ●●●●●●●●● │
│ ●●●●●●● │
│ ●●●●● │
└───────────────┘
INNER JOIN Result
LEFT JOIN Visualization:
Table A Table B
┌─────────┐ ┌─────────┐
│ ●●● │ │ ●●● │
│ ●●●● │◄──►│ ●●●● │
│ ●●●●● │ │ ●●●●● │
│ ●●●● │ │ ●●●● │
│ ●●● │ │ ●●● │
└─────────┘ └─────────┘
▲ ▲
└───────┬───────┘
│
┌───────────────────┐
│ ●●●●● │
│ ●●●●●●● │
│ ●●●●●●●●● │
│ ●●●●●●● │
│ ●●●●● │
│ ●●●●●●● │
│ ●●●●●●●●● │
│ ●●●●●●● │
│ ●●●●● │
└───────────────────┘
LEFT JOIN Result
Visual SQL Joins provides excellent interactive diagrams that help developers understand these concepts more intuitively. These visual representations make it much easier to see how different JOIN types handle overlapping and non-overlapping data between tables.
Practical Examples and Use Cases
Let’s explore real-world scenarios where each JOIN type excels:
Example Database Schema:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
department_id INT
);
CREATE TABLE departments (
id INT PRIMARY KEY,
name VARCHAR(50)
);
INSERT INTO employees VALUES
(1, 'John Doe', 1),
(2, 'Jane Smith', 2),
(3, 'Mike Johnson', 1);
INSERT INTO departments VALUES
(1, 'Sales'),
(2, 'Marketing'),
(3, 'Engineering');
INNER JOIN Example:
SELECT e.name, d.name AS department
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;
Result:
John Doe | Sales
Jane Smith | Marketing
Mike Johnson | Sales
Use Case: When you only want to see employees who are assigned to existing departments.
LEFT JOIN Example:
SELECT e.name, d.name AS department
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;
Result:
John Doe | Sales
Jane Smith | Marketing
Mike Johnson | Sales
(In this case, same as INNER JOIN because all employees have valid departments)
Use Case: When you need to see all employees, regardless of whether they have a department assigned.
RIGHT JOIN Example:
SELECT e.name, d.name AS department
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.id;
Result:
John Doe | Sales
Jane Smith | Marketing
Mike Johnson | Sales
NULL | Engineering
Use Case: When you need to see all departments, including those without any employees.
FULL JOIN Example:
SELECT e.name, d.name AS department
FROM employees e
FULL JOIN departments d ON e.department_id = d.id;
Result:
John Doe | Sales
Jane Smith | Marketing
Mike Johnson | Sales
NULL | Engineering
(Same as RIGHT JOIN in this case because there are no employees without departments)
Use Case: When you need a complete picture of both tables, showing all possible combinations.
When to Use Each JOIN Type
Choosing the right JOIN type depends on your specific data requirements:
Use INNER JOIN When:
- You only need data that exists in both tables
- You want to filter out incomplete records
- Performance is a critical concern
- You’re working with foreign key relationships where all related records should exist
Use LEFT JOIN When:
- You need all records from the primary table (left table)
- You want to see which records in the primary table don’t have matches in the secondary table
- You’re building reports where missing data is acceptable
- The left table represents “main” entities and the right table represents optional relationships
Use RIGHT JOIN When:
- You need all records from the secondary table (right table)
- The right table represents “main” entities in your query
- You’re working with legacy systems where RIGHT JOIN is more natural
- You want to identify which records in the secondary table don’t have matches in the primary table
Use FULL JOIN When:
- You need a complete view of both tables
- You want to identify all mismatches between tables
- You’re doing data analysis or reconciliation
- You need to see all possible combinations, including unmatched records
As mentioned in the SpringPeople article, “If you need only matching values from the paired tables, you can pick the inner join in SQL. However, if you need both matching and non-matching values, you must use outer join in SQL.”
Conclusion
Understanding the differences between INNER JOIN and OUTER JOIN types is fundamental to SQL database operations. INNER JOIN returns only matching rows from both tables, while OUTER JOINs preserve unmatched rows from one or both tables. LEFT OUTER JOIN keeps all records from the left table, RIGHT OUTER JOIN keeps all records from the right table, and FULL OUTER JOIN keeps all records from both tables.
Key takeaways:
- INNER JOIN is best for strict matching requirements and better performance
- LEFT JOIN is the most commonly used OUTER JOIN for preserving left table data
- RIGHT JOIN serves the opposite purpose of LEFT JOIN but is used less frequently
- FULL JOIN provides the most comprehensive view but can be resource-intensive
- The word “OUTER” is optional in SQL syntax for all OUTER JOIN types
When writing SQL queries, consider your data requirements carefully. If you need to see all customers even those who haven’t made purchases, use LEFT JOIN. If you’re reconciling two datasets and need to find all differences, FULL JOIN might be your best choice. Mastering these JOIN operations will significantly enhance your ability to work with relational databases effectively.
Sources
- DataCamp - SQL INNER JOIN vs. OUTER JOIN: Key Differences
- GeeksforGeeks - Inner Join vs Outer Join
- GeeksforGeeks - Difference between Inner Join and Outer Join in SQL
- Software Testing Help - Inner Join Vs Outer Join: Exact Difference With Examples
- Temok - Inner Join vs Outer Join: Key SQL Differences With Examples
- SpringPeople - INNER vs OUTER Joins in SQL: What’s the Difference and When to Use Each?
- Steve Stedman - Inner Join versus Outer Join
- Medium - SQL JOINs (Inner, Left, Right, Full, Self and Cross)
- Interview Kickstart - Inner Join vs Outer Join: Exploring SQL Table Relationships