Last Updated : 23 Jul, 2025
MySQL is a popular and strong system that is extensively employed for handling data in the field of relational databases. A crucial aspect of working with databases is the ability to retrieve and handle data from different tables. SQL joins combine rows from different tables by connecting them based on related columns.
The Outer Join is an important join type as it includes rows with no match in the results, unlike other join types. This article will thoroughly explore MySQL Outer Join, discussing its various types and offering real-life examples.
What is an Outer Join?An Outer Join combines all records from one table and includes any corresponding records from another table. If there is no matching item, the result will be NULL on the incompatible side. This differs from an Inner Join as it only displays rows with a match in both tables.
Types of Outer Joins in MySQLMySQL supports two types of Outer Joins that MySQL.
The LEFT JOIN, also called LEFT OUTER JOIN, is used to show all rows from the left table along with the matching rows from the right table. If no matches are detected, the right table will return a result of NULL.
Syntax:SELECT columnsExample:
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Consider two tables, employees and departments:
employees:employee_id
name
department_id
1
John Doe
101
2
Jane Roe
102
3
Jim Smith
NULL
departments:department_id
department_name
101
HR
102
Finance
103
IT
To retrieve all employees along with their department names, using a LEFT JOIN:
SELECT employees.name, departments.department_nameResult:
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
name
department_name
John Doe
HR
Jane Roe
Finance
Jim Smith
NULL
The query retrieves all employees, including those without a corresponding department, and fills in the department name where available, or NULL if not.
RIGHT JOINThe RIGHT JOIN, also referred to as RIGHT OUTER JOIN, is used to display all entries from the right table along with any corresponding rows from the left table. If there are no matches, the left table will output NULL.
Syntax:SELECT columnsExample
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
Using the same employees and departments tables, to retrieve all departments along with their employees:
SELECT employees.name, departments.department_nameResult:
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
name
department_name
John Doe
HR
Jane Roe
Finance
NULL
IT
The query retrieves all departments, including those without a corresponding employee, and fills in the employee name where available, or NULL if not.
Practical Applications of Outer JoinsOuter Joins come in handy when you need a complete dataset that includes rows without matches in a different table. There are numerous applications in the real world. Some practical applications include:
MySQL Outer Joins, such as LEFT JOIN and RIGHT JOIN, are powerful tools for combining and retrieving data from multiple tables, ensuring that all crucial data is included in your queries. Learning and effectively using these joins can significantly enhance your ability to manage and analyze relational data, leading to better-informed and more thorough data-driven decisions.
By mastering Outer Joins, you will be better equipped to handle complex queries and data sets, improving the durability and efficiency of your database interactions. If you are a database administrator, developer, or data analyst, having knowledge of MySQL Outer Joins will be very beneficial for your daily tasks.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4