Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
PostgreSQL - FULL OUTER JOIN
Next article icon

SQL Server FULL OUTER JOIN

Last Updated : 28 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Joins in SQL are used to retrieve data from multiple tables based on a related column (or common column) between them. In this article, we will learn how to use FULL OUTER JOIN, which returns all rows from both tables being joined. It combines the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN. Instead of discarding a row with no match, it fills NULL in the cells and returns all the rows (or tuples) from the left and right tables.

FULL OUTER JOIN

To understand FULL OUTER JOIN, you must be familiar with SQL, relational databases, and how to relate two or more tables. Before jumping on the main topic, we have explained how INNER JOIN, LEFT OUTER JOIN, and RIGHT OUTER JOIN works. If you're familiar with these terms, we encourage you to read this article from the very beginning.

Now let us understand how Full Outer Join works in MS SQL Server:

The syntax for a SQL Server FULL OUTER JOIN is as follows:

SELECT table1.column1, table1.column2, table2.column3, table2.column4, ...

FROM table1

FULL OUTER JOIN table2

ON table1.column_name = table2.column_name;

Here,

  • table1.column1, table1.column2, table2.column3, table2.column4, ...: are the columns we want to retrieve from the tables.
  • table1: The left table.
  • table2: The right table.
  • ON table1.column_name = table2.column_name: is the condition for joining the tables based on a common column.

The result of a FULL OUTER JOIN includes all rows from both tables, matched where possible and with NULLs in the columns where no match is found.

Note:

- To perform any type of join, we must have a common column in both tables. The column name can be different, but the data type should be the same

- For the sake of our understanding, we treat the first table as the left table and the second table as the right table that are being joined.

Now let us understand different types of joins:

As we talked about, joins in SQL are used to retrieve data from multiple tables based on a related column. Let's have a quick look over INNER, LEFT, and RIGHT OUTER JOINS.

1. INNER JOIN

In a INNER JOIN operation, the SQL server returns only those rows that match the record from the left and the right table. If there is no match, it returns NULL.

Syntax:

SELECT * FROM table1

INNER JOIN table2 ON table1.column_name = table2.column_name;

INNER_JOIN

In the above diagram, only two rows were returned, which have matching records in the right table. To learn more about INNER JOIN refer to this article.

2. LEFT OUTER JOIN

In a Left Outer Join, the SQL server returns all the rows from the left table (table 1) and the matching records from the right table (table 2) are included. If there is no match in the right table, it fills NULL values in the columns of the right table.

Syntax:

SELECT * FROM table1

LEFT OUTER JOIN table2 ON table1.column_name = table2.column_name;

LEFT_OUTER_JOIN

Instead of discarding the first row, the SQL server fills nulls in the column for the first row and returns all the rows of the left table. To learn more about SQL left join refer to this article.

3. RIGHT OUTER JOIN

In a Right Outer Join, the SQL server returns all the rows from the right table (table 2) and the matching records from the left table (table1) are included. If there is no match in the left table, it fills NULL values in the columns of the left table.

Syntax:

SELECT * FROM table1

RIGHT OUTER JOIN table2 ON table1.column_name = table2.column_name;

RIGHT_OUTER_JOIN

It also filled the NULL value for the third row and returned all rows from the right table with matching records from the left table. To learn more about RIGHT OUTER JOIN refer to this article.

4. FULL OUTER JOIN

FULL OUTER JOIN combines or performs union on the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN.

FULL_OUTER_JOIN

Note: In the result of a FULL OUTER JOIN operation, the number of rows/tuples will be equal to the unique entry in the common column (or related column).

Implementation of FULL OUTER JOIN in MS SQL Server.

Let's consider two tables: Employees (Employee_id, Employee_name, Departement_Id) and Departments (Department_id, Department_name). The Employees table contains information about employees, while the Departments table contains information about different departments in a company.

We have inserted the following data in the Employee Table:

INSERT INTO EMPLOYEE (EMPLOYEE_ID, EMPLOYEE_NAME, DEPARTMENT_ID)
VALUES
(1, 'Priya', 2),
(2, 'Arun', 3),
(3, 'Aditya', 3),
(4, 'Rahul', 4),
(5, 'Akshay', 6);

Let's see the entries:

SELECT * FROM EMPLOYEE;

FULL_OUTER_JOIN(1)

And following data in the Department Table:

INSERT INTO DEPARTMENT (DEPARTMENT_ID, DEPARTMENT_NAME)
VALUES
(2, 'Marketing'),
(3, 'IT Services'),
(4, 'Management'),
(5, 'HR');

Checking the entries:

SELECT * FROM DEPARTMENT;

FULL_OUTER_JOIN(2)

Note: Here, we are not considering any referential integrity as we have not set the foreign key. The department_id column in Employee table can have an id that don't exist in the Department Table.

Full Outer Join is generally performed on common columns other than the foreign key.

Simple FULL OUTER JOIN:

SELECT Employee.Employee_ID, Employee.Employee_Name, Department.Department_ID, Department.Department_Name
FROM Employee
FULL OUTER JOIN Department
ON Employee.Department_ID = Department.Department_ID;

Output: When we run the above query, we get the following results.

FULL_OUTER_JOIN(3)

In this example, we retrieve the Employee_ID and Employee_Name from the Employee table and the Department_Name and Department_ID from the Department table.

The result includes all employees, along with their respective departments, whether they are assigned to a department or not. As we can see Akshay do not belong to any department and the HR department has no employee.

Note: Here, the OUTER keyword is optional. We can simply use FULL JOIN instead of FULL OUTER JOIN. Both give us the same output.

Removing Outer Keyword:

SELECT *
FROM Employee
FULL JOIN Department
ON Employee.Department_ID = Department.Department_ID;

Result:

FULL_OUTER_JOIN(4)

We get the same result.

Handling NULLs:

If we don't want to show NULL values in the result, we can handle it explicitly using the COALESCE() function. Here is how we can handle nulls:

SELECT COALESCE(Employee.Employee_Name, 'No Employee') AS Employee_Name,
COALESCE(Department.Department_Name, 'No Department') AS Department_Name
FROM Employee
FULL OUTER JOIN Department
ON Employee.Department_ID = Department.Department_ID;

Output: When we run the above query, we get the following results/rows.

FULL_OUTER_JOIN(5)

In this example, we use the COALESCE function to handle or replace NULL values. If an employee is not assigned to a department or a department has no employees, the result displays "No Employee" or "No Department," respectively.

Conclusion

In this article, we look over INNER, LEFT, and RIGHT OUTER JOINs to build an intuition on how joins work. Later, we discussed FULL OUTER JOIN in depth with an example. We also learned how to handle nulls. Understanding FULL OUTER JOIN enhances your ability to write sophisticated SQL queries for diverse data scenarios. If you face any difficulty, you can pin down the same in the comment section.


Next Article
PostgreSQL - FULL OUTER JOIN
author
arunkumar2403gg
Improve
Article Tags :
  • Geeks Premier League
  • SQL Server
  • Databases
  • Geeks Premier League 2023

Similar Reads

  • PostgreSQL - FULL OUTER JOIN
    In PostgreSQL, the FULL OUTER JOIN is a powerful feature that combines the effects of both LEFT JOIN and RIGHT JOIN. This join operation retrieves all rows from both tables involved in the join, including unmatched rows from each table. For any unmatched rows, PostgreSQL fills the result with NULL v
    4 min read
  • FULL OUTER JOIN in SQLite
    In the area of data querying and manipulation, the ability to combine information from different sources is important. SQLite, a popular embedded database engine, offers a range of join operations to fast process. One such operation FULL OUTER JOIN is particularly powerful as it allows us to merge d
    4 min read
  • SQL Outer Join
    SQL Outer Joins allow retrieval of rows from two or more tables based on a related column. Unlike inner Joins, they also include rows that do not have a corresponding match in one or both of the tables. This capability makes Outer Joins extremely useful for comprehensive data analysis and reporting,
    4 min read
  • PL/SQL Outer Join
    In SQL, joins are used to retrieve data from two or more tables based on a related column. Joins can be categorized into different types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, etc. In this article, we will learn about OUTER JOIN in PL/SQL, including its types, syntax, use cases, and ex
    6 min read
  • SQL Server ALL Operator
    In SQL Server the logical operators are used to perform conditional checks in SQL Queries. There are many different types of Logical operators like ANY, AND, LIKE, EXISTS, NOT, IN, and BETWEEN in SQL Server. These operators are very useful to filter and compare single or multiple data in SQL Queries
    3 min read
  • 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
    4 min read
  • PL/SQL Full Join
    A FULL JOIN, also called a FULL OUTER JOIN, is a type of join in PL/SQL that returns all rows from both tables, even if there is no matching data in the other table. If a row from one table doesn’t have a match in the other, it will still be included in the result, with NULL values filling in the mi
    6 min read
  • Recursive Join in SQL
    In SQL, a recursive join is a powerful technique used to handle hierarchical data relationships, such as managing employee-manager relationships, family trees, or any data with a self-referential structure. This type of join enables us to combine data from the same table repeatedly, accumulating rec
    3 min read
  • SQL Server ALIASES
    Aliases in SQL Server are the temporary names given to tables or columns to make it easy to read and maintain the data. Aliases help you to provide different names to columns and tables temporarily so that users can easily understand the data of the table and it does not change any data of the table
    3 min read
  • SQL Full Outer Join Using Where Clause
    SQL (Structured Query Language) provides powerful tools to manage and query relational databases. One of its most flexible features is the JOIN operation, which allows combining data from two or more tables based on related columns. Among the various types of joins, the FULL OUTER JOIN is particular
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences