SQL Outer Join
The SQL OUTER JOIN returns all rows from both the participating tables which satisfy the join condition along with rows which do not satisfy the join condition. The SQL OUTER JOIN operator (+) is used only on one side of the join condition only.
Pictorial Presentation of SQL Outer Join
The subtypes of SQL OUTER JOIN
- LEFT OUTER JOIN or LEFT JOIN
- RIGHT OUTER JOIN or RIGHT JOIN
- FULL OUTER JOIN
Select * FROM table1, table2 WHERE conditions [+];
Here is an example of outer join in SQL between two tables.
Sample table: foods
Sample table: company
To get company name and company id columns from company table and company id, item name, item unit columns from foods table, after an OUTER JOINING with these mentioned tables, the following SQL statement can be used:
SELECT company.company_name,company.company_id, foods.company_id,foods.item_name,foods.item_unit FROM company, foods WHERE company.company_id = foods.company_id(+);
This SQL statement would return all rows from the company table and only those rows from the foods table where the joined fields are equal.
The (+) after the foods.company_id field indicates that, if a company_id value in the company table does not exist in the foods table, all fields in the foods table will be displayed as NULL in the result set.
COMPANY_NAME COMPANY_ID COMPANY_ID ITEM_NAME ITEM_UNIT --------------- ---------- ---------- --------------- ---------- Akas Foods 16 16 Chex Mix Pcs Jack Hill Ltd 15 15 Cheez-It Pcs Jack Hill Ltd 15 15 BN Biscuit Pcs Foodies. 17 17 Mighty Munch Pcs Jack Hill Ltd 15 15 Pot Rice Pcs Order All 18 18 Jaffa Cakes Pcs sip-n-Bite. 19
Key points to remember
Click on the following to get the slides presentation -
Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.
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.
SQL: Tips of the Day
How to avoid the "divide by zero" error in SQL?
Select Case when divisor=0 then null Else dividend / divisor End ,,,
Select dividend / NULLIF(divisor, 0) ...
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook