Understanding Joins And Joint Table Operations
-
Upload
bob-litsinger -
Category
Documents
-
view
721 -
download
0
description
Transcript of Understanding Joins And Joint Table Operations
Tables working togetherJoins and other joint table operations
ContentsUnderstanding Join and Joint Table Operations
Setting the stage (preliminary discussion and review of dataset) 3
Joins 7 Other Joint Table Operations 14
Inner Joins 7 Union 14
Types of Outer Joins 8 Except 17
Left Outer Join 9 Intercept 18
Right Outer Join 10 Merge 19
Full Outer Join 11 Conclusion 21
Cross Join 12
2
Use of multiple primary tables is covered in this presentation. The use of tables as sub-queries is saved for another discussion.
SETTING THE STAGEfor a discussion about joins.
3
A family and the relationships between them will be used to illustrate the different kinds of joins.
Building Relationships
To understand joins we’ll consider how some families are related.
JackSarahVictoriaMichael
MichaelMaryTerryLori
MarkVictoriaSally
4
Bringing them together From time to time families like to get together.
They might get together in various ways:
Michael might visit is parents
Michael’s family might visit his parents
Everyone might get together
Using joins in a database is about bringing together things in much the same way.
5
Families in the database So we’ll use the families as an example.
6
Inner Joins An INNER JOIN between two
tables returns information from rows that match on some characteristic.
An INNER JOIN is the default join type so the word INNER can be omitted and the query will return .an INNER JOIN
Michael is the only person in Michael’s Family that is also found in Jack’s Family, so if we join on names only one row should be returned.
Both queries return a single line result set:
7
Types of Outer Joins There are three types of OUTER JOINS
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
The first table mentioned in the FROM clause is on the left, and every other table mention after that is further right.
Think of LEFT, RIGHT and FULL as inclusion requests.
Basically, each says include all of these even if there is no match.
If there are no matches in the other table, the values for those show as null.
The easiest way to understand this is to look at some examples.
8
LEFT OUTER JOINVictoria’s family is on vacation, so we’ll leave them out for now.
Jack’s Family is mentioned first, so it is the “left table.”
Notice that we excluded Victoria in the WHERE clause.
But now we get Jack and Sarah because the LEFT JOIN includes all the qualified rows from the left table.
There are not matching values for Jack and Sarah in MichaelsFamily, so these are left NULL.
This is Michael visiting his Mom & Dad!9
RIGHT OUTER JOINWe won’t need to exclude Victoria if Michael’s because she is not in Michael’s family.
Michael’s Family is mentioned second, so it is the “right table.”
The only match in JacksFamily is Michal, so he is the only one with values from Jack’s Family.
As in the LEFT JOIN, where the values don’t match the fields from JacksFamily are left NULL.
Michael stays home and they call his Mom & Dad.
10
FULL OUTER JOINVictoria’s Family is still away. Because this as a full join,
excluding Victoria requires she be removed before joining the tables. This is done with an opening common table expression.
Everyone else from the two family’s is listed.
There are NULLs on one side or the other except where there is a match in both tables.
Michael’s family visits his Mom & Dad.
11
Cross Join – Introduction There is one additional join called a CROSS JOIN. This join has no equality matching, but instead joins
everyone row in one table to every row in a second table. The result set contains (the number of rows Table 1) X (the
number of rows in Table 2). CROSS JOINS can be useful, but a where condition to limit
returns is your friend.
CAUTION: A cross join between 2 tables having 1M rows each returns 1,000,000,000,000 rows (if you server does crash before it finishes)!!!!!
Two syntax statements work:
The second syntax without a where condition is a common error.
12
CROSS JOIN Even if Victoria is away, everyone in
both families is related. A cross join does not seek a match and
joins every row to every row in the other table.
Everyone is related to everyone else in this cross join between Michael’s Family and his Parent’s Family.!
13
Union & Union All
There are two variations on this, Union and Union All
The results for these are in a different orderUNION mixes the tables, sorting on the primary ID, then in table order.
UNION ALL keeps the table set together, and sorts by the primary ID within the table.
Notice that Michel and Victoria are listed twice, but that’s because of unique IDs and Family Roles.
Watch what happens if we only use the matched fields.14
Unions on just matched fields
With just fields that match, UNION drops duplicates!
But, UNION ALL still keeps the duplicates.
Now UNION only lists Michael and Victoria only once, but UNION ALL still lists them twice.
15
Important Union Requirement
The number of fields and data types must match or the union will fail.
16
ExceptTo show except, a UNION ALL is used to create a table of the results.
An EXCEPT will exclude identical rows from the 2nd table from a result.
17
IntersectIntersect will identify records that are same in two tables.
Victoria, of course, is Jack’s daughter and listed in both.
Note the same rule about fields and data types applies, so:
Finds no matches because the ID and FamilyRole do
not match!
18
Merge – Page 1 of 2 MERGE can solve the duplication
problem Michael is putting together a list of
people for the picnic. He started with is parent’s family. Unfortunately, he got his sister’s
birthday wrong! He added his family using MERGE.
Target TableTable providing new family members
Check for people already listed
Fix info if incorrect
Add anyone new
Michael’s Family Role is updated. Of course, Vicki’s birthday is still wrong,
and she is listed as a child.19
Merge – Page 2 of 2
Target TableTable providing new family members
Check for people already listed
Fix info if incorrect
Add anyone new
The second merge sees and fixes Vicki’s role and Birthday.
Notice that a MERGE script MUST end with a semicolon!
This is one of a few places where a semicolon is required!
20
Conclusion I hope you’ve found the presentation to be helpful.
Questions and comments are welcome. Please direct them to me at [email protected].
Check my LinkedIn page for additional presentations on query basics or more advanced topics.
21