Difference between inner and outer join in SQL

Assume you have two tables A and B.

Inner join of A and B gives the intersect of A and B. Inner join requires atleast one match in both tables. (i.e., in A and B)


Outer join of A and B gives the unoin of A and B. Outer join returns all the record from the left table even though they don't have any common record.



Example:

Assume you have two tables A and B. Both tables have one column in common:

Table A Table B

A     B

ID     ID
1     6
2     1
3     5
4     2

Inner join :

It results the intersection of the two tables, i.e. the two rows they have in common.

select * from A INNER JOIN B on A.ID = B.ID;

The result will be...
1  1
2  2

Left outer join :

It will return all rows from A , plus any common rows from B.

select * from A LEFT OUTER JOIN B on A.ID = B.ID

Result :
1   1
2   2
3   null
4   null


Full outer join :

It will return the union of A and B, i.e. All the rows in A and all the rows in B.

select * from A FULL OUTER JOIN B on A.ID = B.ID;

   1      1
   2      2
   3     null
   4     null
  null    5
  null    6

1 comment: