Open In App

SQL FULL JOIN

Last Updated : 08 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The FULL JOIN (or FULL OUTER JOIN) in SQL returns all rows from both tables, combining matched rows and filling unmatched rows with NULL values. It is basically the combination of LEFT JOIN and RIGHT JOIN

  • Retrieves all rows from both tables.
  • Matches rows where conditions meet.
  • Fills NULLs where no match exists.
  • Combines results of LEFT JOIN + RIGHT JOIN.
  • Can be used sequentially for multiple tables.
unioun
Full Join

Syntax:

SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;

Parameters:

  • 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_IDBOOK_NAMEISSUED_ONDUE_DATE
1RD SHARMA2023-01-012023-01-08
2GATE CRACKER2023-02-022023-02-09
3MORRIS MANO2023-03-032023-03-10
4NK PUBLICATIONS2023-04-042023-04-11
5BIG BANG THEORY2023-05-052023-05-12

2. Authors Table

This table represents the authors who have written the books.

AUTHOR_IDAUTHOR_NAME
1Ram Kumar
2Shyam Sunder
3Sita Singh
4Mohan Gupta
5Raj Kapoor

3. Publishers Table

This table represents the authors who have published the books.

PUBLISHER_IDPUBLISHER_NAME
1Pearson
2Wiley
3McGraw-Hill
4Springer
5Elsevier

Example 1: FULL JOIN on Multiple Tables

In this example, we perform a FULL JOIN across the Books, Authors, and Publishers tables to combine all related records into a single result set.

Query:

SELECT 
b.BOOK_ID,
b.BOOK_NAME,
a.AUTHOR_NAME,
p.PUBLISHER_NAME
FROM Books b
FULL JOIN Authors a ON b.BOOK_ID = a.AUTHOR_ID
FULL JOIN Publishers p ON b.BOOK_ID = p.PUBLISHER_ID;

Output:

BOOK_IDBOOK_NAMEAUTHOR_NAMEPUBLISHER_NAME
1RD SHARMARam KumarPearson
2GATE CRACKERShyam SunderWiley
3MORRIS MANOSita SinghMcGraw-Hill
4NK PUBLICATIONSMohan GuptaSpringer
5BIG BANG THEORYRaj KapoorElsevier

Explanation:

  • Books uses BOOK_ID.
  • Authors uses AUTHOR_ID, which matches BOOK_ID.
  • Publishers uses PUBLISHER_ID, also matching BOOK_ID.
  • FULL JOIN ensures that all rows are included even if a match is missing.

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
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_IDBOOK_NAMEAUTHOR_NAMEPUBLISHER_NAME
1RD SHARMARam KumarPearson

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.


Article Tags :

Explore