![]() |
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 JOINTo 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:
Here,
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.
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 JOINIn 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:
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 JOINIn 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:
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 JOINIn 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:
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 JOINFULL OUTER JOIN combines or performs union on the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN.
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) Let’s see the entries: SELECT * FROM EMPLOYEE; And following data in the Department Table: INSERT INTO DEPARTMENT (DEPARTMENT_ID, DEPARTMENT_NAME) Checking the entries: SELECT * FROM DEPARTMENT;
Simple FULL OUTER JOIN:SELECT Employee.Employee_ID, Employee.Employee_Name, Department.Department_ID, Department.Department_Name Output: When we run the above query, we get the following results. 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.
Removing Outer Keyword:SELECT * Result: 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, Output: When we run the above query, we get the following results/rows. 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. ConclusionIn 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. |
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |