What is Full Outer Join in SQL?
SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;
|table1, table2||Name of the tables participating in joining.|
|column_name||Column of the participating tables.|
Let’s combine the same two tables using a full join.
SELECT * FROM table_A FULL OUTER JOIN table_B ON table_A.A=table_B.A;
Because this is a full join, all rows (both matching and nonmatching) from both tables are included in the output. There is only one match between table table_A and table table_B, so only one row of output displays values in all columns. All remaining rows of output contain only values from table table_A or table table_B, with the remaining columns set to missing values
only one row of output displays values in all columns explain below -
Here is an example of full outer join in SQL between two tables.
Sample table : foods
Sample table : company
As we know the FULL OUTER JOIN is the combination of the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN, so, here we are going to describe how FULL OUTER JOIN perform internally.
Here is the SQL statement which returns all rows from the 'foods' table and 'company' table using "FULL OUTER JOIN" clause.
SELECT a.company_id AS "a.ComID", a.company_name AS "C_Name", b.company_id AS "b.ComID", b.item_name AS "I_Name" FROM company a FULL OUTER JOIN foods b ON a.company_id = b.company_id;
FULL OUTER JOIN using WHERE clause
We can include a WHERE clause with a FULL OUTER JOIN to get return only those rows where no matching data between the joining tables are exists.
The following query returns only those company that have no matching food product in foods , as well as those food product in foods that are not matched to the listed company.
SELECT a.company_id AS "a.ComID", a.company_name AS "C_Name", b.company_id AS "b.ComID", b.item_name AS "I_Name" FROM company a FULL OUTER JOIN foods b ON a.company_id = b.company_id WHERE a.company_id IS NULL OR b.company_id IS NULL ORDER BY company_name;
FULL OUTER JOIN using UNION clause
A UNIOIN clause can be used as an alternate to get the same result as FULL OUTER JOIN
Here is the example:
Code : using FULL OUTER JOIN
SELECT table_a.A,table_a.M,table_b.A,table_b.N FROM table_A FULL OUTER JOIN table_B ON table_A.a=table_b.A ORDER BY table_A.A;
FULL OUTER JOIN using LEFT and RIGHT OUTER JOIN and UNION clause
The following code is, the combination of LEFT OUTER JOIN and RIGHT OUTER JOIN and combined by, using UNION clause
SELECT table_a.A,table_a.M,table_b.A,table_b.N FROM table_A LEFT OUTER JOIN table_B ON table_A.a=table_b.A UNION SELECT table_a.A,table_a.M,table_b.A,table_b.N FROM table_A RIGHT OUTER JOIN table_B ON table_A.a=table_b.A;
Note: Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.
Key points to remember
Click on the following to get the slides presentation of all JOINS -
Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.