Quantcast
Channel: Planet MySQL
Viewing all articles
Browse latest Browse all 18783

SQL Outer Join Example | Left, Right, and Full Outer Join Tutorial

$
0
0
SQL Outer Join Example | Left, Right, and Full Outer Join Tutorial

SQL Outer Join Example | Left, Right, and Full Outer Join Tutorial is today’s topic. If you are new to this blog in SQL, then I have previously described Cross Join, Inner Join, and Left Join. In the SQL outer JOIN, all the content of both tables is integrated either they are matched or not.

When performing the inner join, rows from either table that are unmatched in the other table are not returned in the final result set. In the outer join, the unmatched rows in one or both tables can be returned.

What is the use of SQL OUTER JOIN

In SQL Outer Join all the contents of both the tables are combined either they are matched or not. In the outer join, unmatched rows in one or both tables can be returned.

SQL Outer Join Example

The SQL OUTER JOIN returns all the rows from both the participating tables which satisfy a join condition along with rows which do not meet the join criteria. The SQL OUTER JOIN operator (+) is used only on one side of the join condition only.

Types of Outer Join

  1. LEFT JOIN returns only unmatched rows from the left table.
  2. RIGHT JOIN returns only unmatched rows from the right table.
  3. FULL OUTER JOIN returns unmatched rows from both tables.

Consider the following table

See the following tables.

CUSTOMER:
ID NAME AGE CITY
1 Rohit 20 Patna
2 Shivam 18 Jalandhar
3 Pranav 19 Dharamshala

 

ORDERS:
O_ID Cust_ID City AMOUNT
201 1 Patna 3000
202 2 Jalandhar 4000
203 4 Kolkata 1000

 

Left Outer Join

This is also known as Left Join. Left outer join returns all rows in a table which is on the left side matched with the rows of the table in right side. This gives a conclusion that the SQL left Join always contains the rows in the left table.

SYNTAX

Select column_1, column_2… from table_1 
LEFT JOIN table_2 ON CONDITION;

See the following diagram of Right Outer Join.

 

SQL LEFT OUTER JOIN example

Let’s clear this with an example. See the following query.

Select Orders.O_ID, Customer.id,customer.name, Orders.amount 
From CUSTOMER 
LEFT JOIN Orders ON Customer.ID = Orders.Cust_ID;

See the below output.

 

SQL Outer Join Example

EXPLANATION

As you can see, all the contents of the left table are displayed, whether it is matched with the right table or not. Right table contents which are matched with the left table is displayed as well and which are not matched is displayed with NULL values.

Right Outer Join

SQL right join returns all the values from the rows of the right table. It also includes the matched values from the left table, but if there is no matching in both the tables, it returns NULL values. See the following syntax.

Select column_1, column_2… 
from table_1 
RIGHT JOIN table_2 ON CONDITION;

See the following diagram of Right Outer Join.

 

right outer join example

Let’s see the following code example.

Select Orders.O_ID, Customer.id, customer.name, Orders.amount 
From CUSTOMER 
RIGHT JOIN Orders ON Customer.ID = Orders.Cust_ID;

See the output.

Right Outer Join

 

EXPLANATION

As you can see, all the contents of the right table are displayed, whether it is matched with the left table or not. Left table contents which are matched with the right table is displayed as well and which are not matched is displayed with NULL values.

FULL OUTER JOIN

SQL full outer join returns all the rows in the left table, right table and matching rows in both the tables.

SYNTAX

Select * from table1 
FULL OUTER JOIN table2 ON CONDITION;

See the following diagram of Full Outer Join.

 

SQL full outer join example

See the following example.

Select * from Customer 
FULL OUTER JOIN Orders ON Customer.ID = Orders.cust_id;

The above Statement will not work in MySQL, Because SQL full outer join returns a result set that is combined results of both SQL left join and SQL right join. So to generate the result, we have to use the UNION operator.

Now, see the following statement.

Select * from Customer 
LEFT JOIN Orders ON Customer.id = Orders.Cust_ID 
UNION 
Select * from Customer 
RIGHT JOIN Orders 
ON Customer.id = Orders.Cust_id;

See the following output.

Full outer Join

 

Explanation

As this is a full outer join all rows whether matched or not from both the tables are included in the output.

The FULL OUTER JOIN returns all the matching records from both the tables whether the other table matches or not.

So, if there are rows in the “Customers” table that do not have matches in “Orders”, table or if there are rows in the “Orders” that do not have matches in “Customers” table, those rows will be in the output as well.

Finally, SQL Outer Join Example | Left, Right, and Full Outer Join Tutorial is over.

The post SQL Outer Join Example | Left, Right, and Full Outer Join Tutorial appeared first on AppDividend.


Viewing all articles
Browse latest Browse all 18783

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>