Sunday, October 19, 2014

SQL Joining Illustrations

SQL Joining is used to combine rows of two or more tables. There are 4 general types of SQL joining which are inner, left, right and full outer.

Let’s start with inner Join. The image below shows that the returned rows with at least one match on both tables

Inner Join Sample Inner Join Code
image

SELECT <select_list>

FROM TABLE A A

INNER JOIN TABLE B B

ON A.key=B.Key;

The next is left Join where the rows from the left table as well as the rows matched rows on the right table are returned.

Left Join Sample Left Join Code
image

SELECT <select_list> FROM TABLE A A

LEFT JOIN TABLE B B

ON A.key=B.Key;

Conversely, right Join has rows from the right table as well as those that matched on the left table returned.

Right Join Sample Right Join Code
image

SELECT <select_list> FROM TABLE A A

RIGHT JOIN TABLE B B

ON A.key=B.Key

Full Join returns rows that have at least a match in one of the tables.

Full Outer Join Sample Full Outer Join Code
image

SELECT <select_list> FROM TABLE A A

FULL OUTER JOIN TABLE B B

ON A.key=B.Key

No comments:

Post a Comment