Yet another of those common technical interview questions that every interviewer wants to know, every candidate gets wrong and everyone looks up anyway when using them. There are four types of joins that this article focuses on Inner, Outer, Right, and Left. For perspective, the following two tables will be used.
|
| |||||||||||||||||||||||||||||||||||||
What is the difference between an Inner Join and an Outer Join?
By definition an Inner Join (also just called a Join, inner is assumed) connects the rows from both tables when there is a match between the columns compared.
SELECT tblTypes.TYPE, tblItems.NAME FROM tblItems JOIN tblTypes ON tblItems.tblTYPE_ID = tblTypes.ID
| TYPE | NAME |
|---|---|
| Animal | Ocelot |
| Vegetable | Cardoon |
| Animal | Banteng |
| Animal | Guenon |
| Mineral | Vomicine |
What is we want to get all the records regarless if there is a match in our condition. In that case, use an Outer Join. Outer is slightly different in that it is comprised of a Left or Right Join.
What is a Right Join and Left Join?
SELECT tblTypes.TYPE, tblItems.NAME FROM tblItems LEFT JOIN tblTypes ON tblItems.tblTYPE_ID = tblTypes.ID
| TYPE | NAME |
|---|---|
| Animal | Ocelot |
| Vegetable | Cardoon |
| Animal | Banteng |
| Animal | Guenon |
| Mineral | Vomicine |
| NULL | Brobee |
SELECT tblTypes.TYPE, tblItems.NAME FROM tblItems RIGHT JOIN tblTypes ON tblItems.tblTYPE_ID = tblTypes.ID
| TYPE | NAME |
|---|---|
| Animal | Ocelot |
| Vegetable | Cardoon |
| Animal | Banteng |
| Animal | Guenon |
| Mineral | Vomicine |
| TBD | NULL |
There are four major types of joins: Inner, Outer, Right, and Left. Right and Left are part of an outer join. Inner retrieves all matches from the specified join condition(s). Right retrieves from the right table regardless of a match for the left and Left, okay well you have it now.
0 comments:
Post a Comment