mysqldude

FULL OUTER JOIN

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Native FULL OUTER JOIN syntax
version check
✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064✗ 1064
LEFT JOIN UNION RIGHT JOIN emulation (original)
original post
✓✓✓✓✓✓✓✓✓
Native FULL OUTER JOIN syntax version check
MySQL 5.6✗ 10645.7✗ 10648.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✗ 106410.11✗ 106411.4✗ 106411.8✗ 1064
LEFT JOIN UNION RIGHT JOIN emulation (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (2)
  • Native FULL OUTER JOIN syntax (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OUTER JOIN dept USING (deptid)' at line 1
  • Native FULL OUTER JOIN syntax (MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OUTER JOIN dept USING (deptid)' at line 1

Related errors: 1064

The original article

First published 2012-02-27. The text below is the original; the table above shows how it behaves on current versions.

Unfortunately, MySQL 5.x does not yet support FULL OUTER JOIN. While this is not ideal the post will show how we can get the same result using UNION. A FULL OUTER JOIN allows rows from one table be joined to another and regardless of whether entries exist in either table corresponding the records will be shown.

Let us imagine we have a table called depts which contains a list of departments. Another table emp contains a list of employees including a deptid column to identify the department for which they work:

mysql> create table dept (deptid int auto_increment primary key, dept varchar(100));
Query OK, 0 rows affected (0.03 sec)

mysql> create table emp(empid int auto_increment primary key, name varchar(100), deptid int);
Query OK, 0 rows affected (0.03 sec)

We now want to add information into this table:

mysql> insert into dept(dept) values ('IT'),('Accounting'),('Marketing'),('Sales');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from dept;
+--------+------------+
| deptid | dept       |
+--------+------------+
|      1 | IT         |
|      2 | Accounting |
|      3 | Marketing  |
|      4 | Sales      |
+--------+------------+
4 rows in set (0.00 sec)

mysql> insert into emp (name, deptid) values ('Tom',1),('Dick',2),('Harry',null);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from emp;
+-------+-------+--------+
| empid | name  | deptid |
+-------+-------+--------+
|     1 | Tom   |      1 |
|     2 | Dick  |      2 |
|     3 | Harry |   NULL |
+-------+-------+--------+
3 rows in set (0.00 sec)

With the information in the tables we now want to show LEFT OUTER JOIN and RIGHT OUTER JOIN results:

mysql> select empid, name, deptid, dept from emp left outer join dept using (deptid);
+-------+-------+--------+------------+
| empid | name  | deptid | dept       |
+-------+-------+--------+------------+
|     1 | Tom   |      1 | IT         |
|     2 | Dick  |      2 | Accounting |
|     3 | Harry |   NULL | NULL       |
+-------+-------+--------+------------+
3 rows in set (0.00 sec)

mysql> select empid, name, deptid, dept from emp right outer join dept using (deptid);
+-------+------+--------+------------+
| empid | name | deptid | dept       |
+-------+------+--------+------------+
|     1 | Tom  |      1 | IT         |
|     2 | Dick |      2 | Accounting |
|  NULL | NULL |      3 | Marketing  |
|  NULL | NULL |      4 | Sales      |
+-------+------+--------+------------+
4 rows in set (0.00 sec)

To get the FULL OUTER JOIN we simply UNION the 2 previous SQL statements results as follows:

mysql> select empid, name, deptid, dept from emp left outer join dept using (deptid)
    -> union
    -> select empid, name, deptid, dept from emp right outer join dept using (deptid);
+-------+-------+--------+------------+
| empid | name  | deptid | dept       |
+-------+-------+--------+------------+
|     1 | Tom   |      1 | IT         |
|     2 | Dick  |      2 | Accounting |
|     3 | Harry |   NULL | NULL       |
|  NULL | NULL  |      3 | Marketing  |
|  NULL | NULL  |      4 | Sales      |
+-------+-------+--------+------------+
5 rows in set (0.00 sec)