SQL FULL JOIN
In SQL, the FULL JOIN (or FULL OUTER JOIN) is a powerful technique used to combine records from two or more tables. Unlike an INNER JOIN, which only returns rows where there are matches in both tables, a FULL JOIN retrieves all rows from both tables, filling in NULL
values where matches do not exist. We learn about SQL FULL JOIN by understanding various examples in detail.
SQL FULL JOIN
- The
FULL JOIN
orFULL OUTER JOIN
in SQL is used to retrieve all rows from both tables involved in the join, regardless of whether there is a match between the rows in the two tables. - It combines the results of both a
LEFT JOIN
and aRIGHT JOIN
. - When there is no match, the result will include NULLs for the columns of the table that do not have a matching row.
We can use FULL JOIN to combine multiple tables, by sequentially performing FULL JOIN on two tables at a time.

Syntax:
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
Explanation:
SELECT columns
: Specifies the columns to retrieve.FROM table1
: The first table to be joined.FULL JOIN table2
: Specifies the second table to join with the first table using a FULL JOIN.ON table1.column = table2.column
: Defines the condition to match rows between the two tables.
This query retrieves all records from both table1
and table2
, returning NULL where there are no matches.
Examples of SQL FULL JOIN
Let's look at some examples of the FULL JOIN in SQL and understand it's working. First, let's create a demo database and two tables on which we will perform the JOIN.
1. Books Table
This table Represents the list of books in the system
BOOK_ID | BOOK_NAME | ISSUED_ON | DUE_DATE |
---|---|---|---|
1 | RD SHARMA | 2023-01-01 | 2023-01-08 |
2 | GATE CRACKER | 2023-02-02 | 2023-02-09 |
3 | MORRIS MANO | 2023-03-03 | 2023-03-10 |
4 | NK PUBLICATIONS | 2023-04-04 | 2023-04-11 |
5 | BIG BANG THEORY | 2023-05-05 | 2023-05-12 |
2. Authors Table
This table represents the authors who have written the books.
AUTHOR_ID | AUTHOR_NAME |
---|---|
1 | Ram Kumar |
2 | Shyam Sunder |
3 | Sita Singh |
4 | Mohan Gupta |
5 | Raj Kapoor |
3. Publishers
Table
This table represents the authors who have published the books.
PUBLISHER_ID | PUBLISHER_NAME |
---|---|
1 | Pearson |
2 | Wiley |
3 | McGraw-Hill |
4 | Springer |
5 | Elsevier |
Example 1: Joining Multiple Tables with Full Join
Query:
SELECT
b.BOOK_ID,
b.BOOK_NAME,
a.AUTHOR_NAME,
p.PUBLISHER_NAME
FROM
Books b ,Author a, Publisher p
FULL JOIN Authors a ON b.BOOK_ID = a.AUTHOR_ID
FULL JOIN Publishers p ON b.BOOK_ID = p.PUBLISHER_ID;
Output:
BOOK_ID | BOOK_NAME | AUTHOR_NAME | PUBLISHER_NAME |
---|---|---|---|
1 | RD SHARMA | Ram Kumar | Pearson |
2 | GATE CRACKER | Shyam Sunder | Wiley |
3 | MORRIS MANO | Sita Singh | McGraw-Hill |
4 | NK PUBLICATIONS | Mohan Gupta | Springer |
5 | BIG BANG THEORY | Raj Kapoor | Elsevier |
Explanation:
- The Books table uses
BOOK_ID
as the primary key. - The Authors table uses
AUTHOR_ID
, which matches theBOOK_ID
. - The Publishers table uses
PUBLISHER_ID
, which also matches theBOOK_ID
.
So the relationship is:
- Each
BOOK_ID
corresponds to oneAUTHOR_ID
and onePUBLISHER_ID
.
FULL JOIN Authors a ON b.BOOK_ID = a.AUTHOR_ID
Combines all books with authors where IDs match, and retains unmatched books or authors (none in this case).
FULL JOIN Publishers p ON b.BOOK_ID = p.PUBLISHER_ID
Then joins in publishers using the sameBOOK_ID
.
Example 2: Full Join with WHERE Clause
Now, we want to filter the results from the above join based on a specific condition. We will select only books that have "Sharma" in the book name.
Query:
SELECT
b.BOOK_ID,
b.BOOK_NAME,
a.AUTHOR_NAME,
p.PUBLISHER_NAME
FROM
Books b, Author a, Publisher p
FULL JOIN Authors a ON b.BOOK_ID = a.AUTHOR_ID
FULL JOIN Publishers p ON b.BOOK_ID = p.PUBLISHER_ID
WHERE
b.BOOK_NAME LIKE '%Sharma%';
Output:
BOOK_ID | BOOK_NAME | AUTHOR_NAME | PUBLISHER_NAME |
---|---|---|---|
1 | RD SHARMA | Ram Kumar | Pearson |
Explanation:
In this example, the WHERE clause filters out all books that do not contain the word "Sharma" in their name. After applying the filter, only the record for "RD SHARMA" remains.