A step by step guide to the Left Join. I tested it on an Oracle database version 12.1, but I used the standard syntax so I'm sure it works on every database
2. Intro
Sometimes Outer Join is not so intuitive. I asked myself: how can I visually represent
the join? This presentation shows how I tried to picture the left join.
3. Outline
I¡¯ll show you step by step how the Left Outer Join of 4 tables works. In
simple way you can build the Right Outer Join and the Full Outer Join.
4. Who is Andrea (*)
¡ñ Start in October 1999 with Oracle 8.0.5 (who remember the pre-Java
installation?)
¡ñ Responsible for backup of VAS datacenter and Oracle DBA until 2002
¡ñ Developer of SQL and PL/SQL and responsible for maintenance of the
application schemas from 2002 to 2010
¡ñ Since 2010, full time DBA: installation, configuration, migration, backup
and recovery, tuning analysis of rdbms and SQL code
¡ñ RAC awere
(*) Male name in Italy
5. About me
¡ñ Member of OraPub community: http://http://www.orapub.com
¡ñ Active member of the ITOUG: http://www.itoug.it
¡ñ Writer of the OraSal blog: https://orasal.blogspot.it
¡ñ LinkedIn: https://www.linkedin.com/in/orasal
¡ñ Twitter: @oraize
¡ñ eMail: andrea.salzano@gmail.com
6. Agenda
¡ñ Building the environment
¡ñ The classic way
¡ñ A¡¡.(different) way
¡ñ The visual way
8. Create tables...
create table t1 as select rownum c1 from dba_tables where rownum <11;
create table t2 as select c1+1 c2 from t1;
create table t3 as select c2+1 c3 from t2;
create table t4 as select c3+1 c4 from t3;
10. Definitions
The Join between two tables return the rows
matching both tables. For example, if you
consider the tables T1 and T2, defined in the
previous slide, the Join (called also Inner
Join) will return the values from 2 to 10
It¡¯s the same
11. The Outer Join, force the no matching rows to be in output. Based on the
word used, Left, Right or Full, in the output of the Join will show the rows
of the left, right or both tables
Definitions
12. Definitions
This syntax ... ...is the same
Inner Join Join
Left Outer Join Left Join
Right Outer Join Right Join
Full Outer Join Full Join
13. Left Join: way 01
¡ñ The first way is a classic step by step outer join
14. T1 Left Join T2
select * from t1 left join t2 on (t1.c1=t2.c2);
C1 C2
---------- ----------
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
1
select * from t1;
C1
----------
1
2
3
4
5
6
7
8
9
10
select * from t2;
C2
----------
2
3
4
5
6
7
8
9
10
11
Joining T1 and T2 tables
T1 -> LJ -> T2
In the Left Join the
columns of the ¡°left¡±
table is mandatory
15. select * from t2 left join t3 on (t2.c2=t3.c3);
C2 C3
---------- ----------
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
2
select * from t2;
C2
----------
2
3
4
5
6
7
8
9
10
11
select * from t3;
C3
----------
3
4
5
6
7
8
9
10
11
12
T2 Left Join T3
Joining T2 and T3 tables
T2 -> LJ -> T3
In the Left Join the
columns of the ¡°left¡±
table is mandatory
16. select * from t3 left join t4 on (t3.c3=t4.c4);
C3 C4
---------- ----------
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
3
select * from t3;
C3
----------
3
4
5
6
7
8
9
10
11
12
select * from t4;
C4
----------
4
5
6
7
8
9
10
11
12
13
T3 -> LJ -> T4
In the Left Join the
columns of the ¡°left¡±
table is mandatory
T3 Left Join T4
Joining T3 and T4 tables
17. select *
from t1 left join t2
on (t1.c1=t2.c2);
C1 C2
---------- ----------
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
1
select *
from t2 left join t3
on (t2.c2=t3.c3);
C2 C3
---------- ----------
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
2
select *
from t1 left join t2 on (t1.c1=t2.c2)
left join t3 on (t2.c2=t3.c3);
(T1 LJ T2 ) -> LJ T3
C1 C2 C3
---------- ---------- ----------
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
1
2 2
T1 Left Join T2 Left Join T3
Final result
Joining (T1 and T2) and T3 tables
18. T1 Left Join T2 Left Join T3
t1 left join t2 on (t1.c1=t2.c2)
select *
from t1 left join t2
on (t1.c1=t2.c2);
C1 C2
---------- ----------
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
1
Common values
Step one: T1 LJ T2 => LJT12
20. ljt12 left join t3 on (ljt12.c2=t3.c3)
ljt12 left join t3 on (ljt12.c1=t3.c3)
Which is the difference? Is the result different?
T1 Left Join T2 Left Join T3
21. ljt12 left join t3 on (ljt12.c2=t3.c3)
ljt12 left join t3 on (ljt12.c1=t3.c3)
t1 left join t2 on (t1.c1=t2.c2)
left join t3 on (t2.c2=t3.c3)
t1 left join t2 on (t1.c1=t2.c2)
left join t3 on (t2.c2=t3.c3)
t1 left join t2 on (t1.c1=t2.c2)
left join t3 on (t2.c2=t3.c3)
T1 Left Join T2 Left Join T3
22. ljt12 left join t3 on (ljt12.c2=t3.c3)
ljt12 left join t3 on (ljt12.c1=t3.c3)
Which is the difference? Is the result different?
>>> NO <<<
T1 Left Join T2 Left Join T3