Joins quick reference


Basics

Employee

 Empid
 Name
 courseid
 1
 Sandeep
 10
 2
 Deepak
 20
 3
 Dharmu








Course
 id
 courseName
 duration
 10
 mysql
 1.5
 20
 java programming
 2
 30
 String programming
 1








Inner Join or Simply Join :

select * from Employee JOIN Course ON Employee.courseid = Course.id;

Output:
this will first internally do Cartesian product from each row of left table to each row in right table,
then applies the filter , to match row, which has same courseid and id.

So this will be the output, next all joins will add few more rows on this output.


 Empid
 Name
 courseid
 id
 courseName
 duration
 1
 Sandeep
 10
 10
 mysql
 1.5
 2
 Deepak
 20
 20
 java pro
 2

rest unmatching rows from both tables will not be displayed

Left join/ Left outer Join

In addition to above inner join output, it will also display all the rows from left tables(employee), even if there is no matching rows in right table, but NULL/empty value of right tables columns

i.e extra rows

 Empid
 Name
 courseid
 id
 courseName
 duration
 1
 Sandeep
 10
 10
 mysql
 1.5
 2
 Deepak
 20
 20
 java pro
 2
3
Dharmu
NULL
NULL
NULL
NULL

Effectively displays, the employees, who have not enrolled for any courses

Right Join/ Right Outer Join
reverse of Left

In addition to above inner join output, it will also display all the rows from right tables(course), even if there is no matching rows in left table, but NULL/empty value of right tables columns


 Empid
 Name
 courseid
 id
 courseName
 duration
 1
 Sandeep
 10
 10
 mysql
 1.5
 2
 Deepak
 20
 20
 java pro
 2
NULL
NULL
NULL
30
String pro
1


Effectively displays the courses, which did not find any takers i.e. the courses which are not enrolled by anyone

Full outer Join

In addition to above inner join output,
it will also display all the rows from left tables(employee), even if there is no matching rows in right table, but NULL/empty value of right tables columns
and
it will also display all the rows from right tables(course), even if there is no matching rows in left table, but NULL/empty value of right tables columns

 Empid
 Name
 courseid
 id
 courseName
 duration
 1
 Sandeep
 10
 10
 mysql
 1.5
 2
 Deepak
 20
 20
 java pro
 2
3
Dharmu
NULL
NULL
NULL
NULL
NULL
NULL
NULL
30
String pro
1




Comments

Popular posts from this blog

Oracle User vs Oracle Schemas

Having and Where Clause