SQL FULL OUTER JOIN Keyword
FULL OUTER JOIN is a type of join in SQL that keeps all records from both tables, even when one side has no match.
The FULL OUTER JOIN result includes the records that satisfy the condition in both tables (the intersection) and the records that do not (the non-intersecting part of the union). If a record exists in one table but has no match in the other, the missing columns of that record are filled with NULL.
The FULL OUTER JOIN keyword returns a row as long as there is a match in either the left table (table1) or the right table (table2).
The FULL OUTER JOIN keyword combines the results of LEFT JOIN and RIGHT JOIN.
SQL FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
- table1、table2: The two tables to be joined.
- ON table1.column_name=table2.column_name: Specifies the join condition, usually a field common to both tables.
- **column_name(s)**: Selects the required fields from both tables.

Feature
- Returns the union of the two tables: Includes all matched and unmatched records.
- Unmatched records are filled
NULL: If a record has no match in one table, its corresponding fields are filled withNULL. - Equivalence:
FULL OUTER JOINIncludesLEFT JOINandRIGHT JOIN's result.
Assume there are two tables:Students and Courses。
Students Table:
| StudentID | Name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
Courses Table:
| CourseID | StudentID | CourseName |
|---|---|---|
| 101 | 1 | Math |
| 102 | 2 | Science |
| 103 | 4 | History |
Query Using FULL OUTER JOIN
SELECT Students.StudentID, Students.Name, Courses.CourseName
FROM Students
FULL OUTER JOIN Courses
ON Students.StudentID = Courses.StudentID;
Query Output Result:
| StudentID | Name | CourseName |
|---|---|---|
| 1 | Alice | Math |
| 2 | Bob | Science |
| 3 | Charlie | NULL |
| 4 | NULL | History |
Explanation:
StudentID = 1and2is the intersection, with data in both tables.StudentID = 3exists inStudentstable, but in theCoursestable there is no match,CourseNamecolumn asNULL。StudentID = 4exists inCoursestable, but in theStudentstable there is no match,Namecolumn asNULL。
Summary
| JOIN Type | Returns |
|---|---|
INNER JOIN |
The intersection of two tables, matching records |
LEFT JOIN |
All records from the left table, plus matching records from the right table |
RIGHT JOIN |
All records from the right table, plus matching records from the left table |
FULL OUTER JOIN |
The union of two tables, including matched and unmatched records |
Demonstration Database
In this tutorial, we will use the RUNOOB sample database.
Below is the data selected from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | Runoob Tutorial | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
Below is the data from the "access_log" website access log table:
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)
SQL FULL OUTER JOIN Example
The following SQL statement selects all website access records.
MySQL does not support FULL OUTER JOIN; you can test the following example in SQL Server.
Example
SELECT Websites.name, access_log.count, access_log.date
FROM Websites
FULL OUTER JOIN access_log
ON Websites.id=access_log.site_id
ORDER BY access_log.count DESC;
**Note:** The FULL OUTER JOIN keyword returns all rows from the left table (Websites) and the right table (access_log). If a row in "Websites" has no match in "access_log", or a row in "access_log" has no match in "Websites", those rows are also listed.