Question

Why does the right outer join (of the example below) not give the full dataset of the table like the left outer join does, since outer joins always retain the rows of the corresponding table?

Create Table

create table join_test
    (id number unique not null, name varchar2(10), department varchar2(10));

Populate Table

select * from join_test;

ID NAME            DEPARTMENT   
1 stelios          sa 
2 andros           sa 
3 stav             ba  
4 mary             ba  
5 antonia          la  
6 lambros          ka  

Inner Join

select j1.name, j1.department
from join_test j1 join join_test j2
on (j1.department=j2.department and j1.name<>j2.name);

NAME         DEPARTMENT
andros          sa
steliso         sa
mary            ba
stav            ba

Left Outer Join

select j1.name, j1.department
from join_test j1 left join join_test j2
on (j1.department=j2.department and j1.name<>j2.name)

NAME       DEPARTMENT
andros     sa
steliso    sa
mary       ba
stav       ba
antonia    la
lambros    ka

Right Outer Join

select j1.name, j1.department
from join_test j1 right join join_test j2
on (j1.department=j2.department and j1.name<>j2.name)

NAME       DEPARTMENT
steliso    sa
andros     sa
stav       ba
mary       ba

Changing select list to j2

select j2.name, j2.department
from join_test j1 right join join_test j2
on (j1.department=j2.department and j1.name<>j2.name)

NAME       DEPARTMENT
andros     sa
steliso    sa
mary       ba
stav       ba
antonia    la
lambros    ka
Was it helpful?

Solution

Right and Left joins perform the same function. What's different in your examples is the tables that you are SELECTing from.

These two queries:

select j2.name,j2.department
from join_test j1 left join join_test j2
on(j1.department=j2.department and j1.name<>j2.name)

and

select j1.name,j1.department
from join_test j1 right join join_test j2
on(j1.department=j2.department and j1.name<>j2.name)

Produce the same result:

NAME    DEPARTMENT
stelios sa
andros  sa
stav    ba
mary    ba
(null)  (null)
(null)  (null)

The reason for the difference in results that you see between your left and right queries is that in the left example, you are SELECTing from the "driving" table (the left table, J1). The join is showing all rows from the driving table (J1), and matching rows (which are not displayed) from the right-hand, or non-driving table (J2).

In your right example, you are changing the join but still selecting from J1. Since J1 is now the non-driving table, you are only seeing the matched results from J1. If you add J2 columns to the select, you will see all of its rows:

NAME        DEPARTMENT   NAME       DEPT
stelios     sa           andros     sa
andros      sa           stelios    sa
stav        ba           mary       ba
mary        ba           stav       ba
(null)      (null)       antonia    la
(null)      (null)       lambros    ka

You will see this same result with a LEFT join, but the nulls would be no the other side.

In both cases, the (null) rows represent the rows from the driving table that are not matched in the non-driving table.

OTHER TIPS

This produces the correct results in SQL Fiddle (sqlfiddle.com/#!4/1e7075/2). However, I have a suspicion. The returned results are:

NAME    DEPARTMENT
stelios sa
andros  sa
stav    ba
mary    ba
(null)  (null)
(null)  (null)

I suspect that however you are returning the results (or looking at them), the rows with all NULL values are being ignored. Try choosing the columns from the j2 table and see if you get more results.

here you are making a self join and combined with a right outer join with a non-equal join conditions ;

From what I have understood: when it ask for a right join it means it will only retrieve the records from the right side of the table where there are rows meet the join conditions and combine them with the rows from the left side (main table)where the conditions are meet.

And also Because the main table is the table from the left side of the from ,so when u do a left join it will retrieve all the records from the left table(the main table) + the records from the right side table where there are rows meet the join conditions.

When it comes to a inner join, it will only retrieve the matched records(rows), where the join conditions are met from both tables. Even the main table is the left table , still the rows from the left table where the conditions are not met will not be included in the result sets.

It took me while to figure it out and I hope it explains well and can be helpful.(COZ I also had a struggle to understand it before).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top